# Basic concepts

1. Data element: the basic unit of data. A data element consists of several data items, which is the smallest unit of data.

2. Data type: a set of data elements with the same nature and a set of operations on this set.

3. Data structure

a. Generalized: a batch of data organized according to a certain logical relationship, which is applied to the computer language and stored in the computer memory according to a certain storage representation, on which a set of operations is defined.

The data structure includes three aspects, namely, the logical structure of the data, the storage structure and the operation (operation) applied to the data, as shown in the figure below. The logical structure of data describes the data from the logical relationship. It is inherent in the data itself. It has nothing to do with the storage of data and is independent of the computer.

The storage structure of data is the realization of logical structure in computer language, which depends on computer.

The operation of data is defined in the logical structure and implemented in the storage structure.

The most commonly used operations are query, insert, delete, edit, sort, etc

b. Narrow sense: refers to the organizational form of data, i.e. logical structure (specific 4 categories: set, linear structure, tree structure and graphic structure).

(1) Set: there is no other relationship between data elements in the structure except that they belong to the same set.
(2) Linear structure: there is a one-to-one relationship between data elements, that is, there is a sequential relationship between elements.
(3) Tree structure: there is a one to many relationship between data elements, that is, there is a hierarchical relationship between elements.
(4) Graph structure or mesh structure: there is a many to many relationship between data elements, that is, any two elements are connected
Maybe it does 4. Algorithm

(1) Definition: an algorithm is a finite sequence composed of several instructions. It is a description of the solving steps of a specific problem

(2) Algorithm principle:

1. Finiteness     An algorithm must always end after executing a finite step, and each step must be completed in a finite time.

2 certainty     Every instruction in the algorithm must have an exact meaning without ambiguity.

3 feasibility     An algorithm is feasible, which means that the operations described by the algorithm can be realized through the basic functions that have been realized
The operation is implemented a finite number of times, that is, an algorithm must be completed in a limited time.

four   input         An algorithm has zero or more inputs as the object processed by the algorithm.

5 Output         An algorithm has one or more outputs, which often have some specific relationship with the input.

(3) Difference between algorithm and program

The algorithm must be finite, and a program does not necessarily satisfy the finiteness.

The instructions in the program must be executable by the machine, and the instructions in the algorithm have no such limitation.

An algorithm is a program if it can be written in a machine executable language.

(4) Analysis of time complexity and space complexity of the algorithm

Assuming that the time required to execute each statement is unit time, the time consumption of an algorithm is the sum of the frequencies of all statements in the algorithm, that is, time frequency(   T(n)).

The input of the algorithm to solve the problem is called the scale (or size) of the problem.

If there is an auxiliary function f(n) Denoted as T(n)=O(f(n))

Then O (f(n)) is called the progressive time complexity of the algorithm, which is called time complexity for short

Example: if T (n) = n (n+1) / 2   Then there So the time complexity is Inference (the formula is difficult to type without pulling the process of derivation)

When there are several loop statements, the time complexity of the algorithm is determined by the frequency f(n) of the innermost statement in the loop statements with the most nested layers.

(5) Algorithm analysis

Objective: to analyze the efficiency of the algorithm for improvement

Main aspects: spatial complexity and temporal complexity

# Linear table

(1) Definition: a linear table is a finite sequence consisting of n (n > = 0) data elements (nodes), A1, A2... An

(2)   characteristic:

(1) If there is and only one node (a1) without direct forward trend, it is called the start node.
(2) If there is only one node (an) without direct successor, it is called terminal node.
(3) Except for the start node, any other node a(2 ≤ i ≤ n) in the linear table has and only has a direct forward trend ai-1
(4) Except for the terminal node, any other node a(1 ≤ i ≤ n-1) in the linear table has and only has ai+1 in a direct successor algorithm

Sequence table

1. Sequence table is a random access structure.

3. Definition of sequence table:

Definition of sequence table node

The code in the text needs to be re entered in English mode before it can run, because this is the scanned text

```typedef int ElemType;                 //Data types in linear tables

#define MAXSIZE 1024 / / maximum length setting of linear table

typedef struct{
ElemType elem[MAXSIZE];             //The linear table is a vector storage, and the first node is elem
int length;                        //Table length
}SqList;```

Define sequence table (two ways)

`SqList L;`

Where, l represents the sequence table composed of one-dimensional array elem and length, the ith node in L is represented as l.elem [i-1], and the length of L is represented as L.length

`SqList *L;`

L is the pointer to the SqList sequence table type composed of one-dimensional array elem and length. The ith node in L represents elem[i-1] or (* L).elem[i-1], and the length represents L - > length or (* L).length

4. Basic operation of sequence table

initialization

Set the table length to 0

```void InitList(SqList *L){

L->length=0;                      //Empty table, length 0

}```

Locate (find by value)

```int Locate (SqList L, ElemType item){
int i;

for(i=0;i<L.length; i++)
{
if(L.elem[i]==item)
return (i+1);
}

printf("The value cannot be found!");
return FALSE;
}
```

insert

Since the physical order of the nodes must be consistent with the logical order of the nodes, it is necessary to move all elements after the insertion position back to a storage space to vacate the storage space of the insertion position. You can insert directly only when the insert is at the end.

```int Insert (SqList *L, int i, ElemType e)     //Insert the new node e into position 1 of the sequence table L
{
int j;
if(L->length==MAXSIZE){
printf("Table full, overflow!");
return false;
}
else if(i<1||i>L->length){
printf("Illegal insertion position!");
return false;
}
else{
for(j=L->length-1;j>=i-1;j--)
L->elem[j+1]=L->elem[j];
L->elem[i-1]=e;
L->length++;
return true;
}

}```

delete

The deletion principle is basically the same as that of insertion, except that the deleted position can be directly overwritten with the following elements. Subtract 1 from the table length

(1) There is no need to add additional storage space to represent the logical relationship between nodes.
(2) You can easily access any node in the table at random.

(1) The insertion or deletion operation is inconvenient. Except for the position at the end of the table, a large number of nodes must be moved when inserting or deleting in other positions of the table, which is inefficient.
(2) Because the sequential table requires continuous storage space, storage allocation can only be carried out in advance (static allocation). So,
When the table length changes greatly, it is difficult to determine the appropriate storage scale. If the table space is pre allocated according to the maximum possible length,
It may cause part of the space to be vacant for a long time and can not be fully utilized: if the table length is not estimated in advance, the insertion operation is required
Overflow may occur because the table length exceeds the pre allocated space.

1. Node structure of single linked list:

 data next

Data is the data field, which is used to store the value of the node. next is the pointer field (chain field), which is used to store the direct successor address of the node.

A linked list in which a node has only one chain domain is called a single linked list. 2. Definition of single linked list:

```typedef int ElemType;
typedef struct LNode/Node type definition
{
ElemType data;
struct LNode *next;

LNode x; //x is a structure node variable
LinkList L,p;      //50. P is the structure pointer variable. Another way to write it is LNode *L,*p;```

When performing single linked list operations, we often need to use a temporary node. Because this node is generated when we need it, it becomes a dynamic node variable. Can be generated by standard functions

`p=(LinkList)malloc(sizeof(LNode));`

When it is no longer needed after use, the node variable space is released through the standard function free()

`free(p);`

3. Establishment of single linked list

Establishing single linked list by tail interpolation

```void CreateListR(LinkList L)
{
int x;
p=L;
scanf("%d",&x);
while(x!=-999)
{
p=p->next;
p->data=x:
scanf("d",&x);
}
p->next=NULL;
}```

```void CreateListF (LinkList L)
{
int x;
L->next=NULL;
scanf("%d", &x);
while(x!=-999)
{
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
}```

4. Single linked list search by value

```LinkList Locate(LinkList L,ElemType item)//Find the node with the value of item
{
p=L->next;
while(p&&p->data!=item)
p=p->next;
if(!p)
return NULL;
else
return(p);

}```

Average time complexity O(n)

5. Insertion and deletion

insert

```int Insert(LinkList L,int i,ElemType item)//Insert the element item before the ith node on the single linked list L
{
p=L;
int j=0;
while(p&&j<i-1)
{
p=p->next;
++j;
}
if(!p||j>i-1)
return false;
s->data=item;
s->next=p->next;
p->next=s;
return true;

}
```

delete

```int Delete(LinkList L,int i)//Delete the p node itself in the single linked list L
{
int j;
q=L;
j=0;
while(q&&j<i-1)
{
q=q->next;
++j;
}

if(!q||j>i-1)
{
return false;
}
p=q->next;
q->next=p->next;
free(p);
return true;

}```

Tags: C data structure

Posted on Fri, 19 Nov 2021 00:32:17 -0500 by kpetsche20