1, Definition and understanding of linear table
- Linear table: a finite sequence of zero or more data elements.
- If the linear table has N elements, the ith element, i-1, is called the direct precursor element of this i element, and i+1 is called the direct successor element of this i element. Each element has and only has one direct precursor and one direct successor. When the element in the linear table is 0, it becomes an empty table.
- Each element in a linear table can be composed of multiple data items. For example, there can be multiple data items in a structure.
struct people // data elements { int year; //data item string name; // data item people *next; // data item }
- A linear table can be composed of multiple linear elements, that is, an array or linked list composed of multiple data elements.
2, Abstract data type of linear table
Operatioon : T InitList(*L);// Initialize the linear table, create an empty linear table L, and return the header node bool ListEmpty(L); // Judge whether a linear representation is empty. If it is empty, return true; otherwise, return false void ClearList(*L); // Empty linear table bool GetList(L,i,*e);//Returns the ith element on the linear table to e int LocateElem(L,e); //Find the element equal to the fixed value e. success is the returned position, otherwise 0 is returned; T ListInsert(*L,i,e);//Insert element e at the ith position on Table L and return the header node; void ListDelete(*L,i,*e);//Delete the element at position i on Table L and give the value of the deleted element to e; int ListLength(L); //Returns the length of linear table L, that is, the number of elements;
3, Storage structure of linear table
1. Sequential storage
- The sequential storage structure of linear table refers to the sequential storage of data elements of linear table with a continuous storage unit. That is to store data elements with a continuous address.
- Sequential storage: array storage,
- The length of the array is the storage space occupied by the array divided by the space of each data element. The length of the linear table is the number of data elements.
- The subscript of array storage starts from 0, and each storage unit has its own number.
#define Max 20 typedef struct { int data[Max]; int length; }SqList;
(1) . obtain the elements in the linear table:
bool GetElem(SqList L,int t,ElemType *e) { if(L.length==0||i<1||i>L.length) return false; else *e = L.data[i]; return true; }
(2) , insert elements in linear table:
bool ListInsert(SqList *L,int i,ElemType e) { int k; if(L->length==Max) // Determine whether the maximum limit of the array has been reached return false; if(i<i||i>L->Length+1) // Judge whether i is within the scope; return false; if(i<=L->length) //Judge whether it is at the last point. If not, step back and give way. { if(k=L->Length—1;k>=i-1;k--) L->data[k+1]=L->data[k]; } L->data[i-1]==e; L->Length++; return true; }
(3) . delete elements in linear table:
bool ListDelete(SqList *L,int i,ElemType *e); { int k; if(L->length==0) return false; if(i<1||i>L->Length) return false; *e=L->data[i-1]; if(i<L->Length) { for( k=i;k<L->Length;k++) L->data[k-1]=L->data[k]; } L->length--; return true; }
Advantages and disadvantages of linear table sequential storage
- advantage:
- 1. There is no need to add additional storage space for the logical relationships of elements in the table
2. You can quickly get elements anywhere in the table
- Disadvantages:
- 1. Insert and delete operations require moving a large number of elements
2. When the linear table changes too much, it is difficult to determine the storage space.
3. Generate storage space fragments.
2. Linked list storage (linked list structure storage)
- There are n elements in the linked list storage. For the ith element, this element not only stores the information of the ith element itself, but also stores the position of the next element. The field in which an element stores its own information is called the data field, and the field in which the direct subsequent position is stored is called the pointer field. Such an element is called a node.
- The storage location of the first node in the linked list is called the head pointer. Sometimes, for convenience, a node is attached in front of the first node in the single linked list, which is called the head node,.
- Header pointer:
1. The pointer of the linked list to the first node. If the linked list has a head node, it refers to the pointer to the head node.
2. The head pointer has the function of identification, so it is often preceded by the name of the linked list.
3. Whether the linked list is empty or not, the header pointer is not empty. The header pointer is a necessary element of the linked list. - Header node:
1. It is set up for the unity and convenience of operation and is placed in front of the node of the first element.
2. Data fields are generally meaningless and can also be used to represent the length of linked lists.
3. The head node is convenient for inserting and deleting the first node before the linked list. The head node is not necessarily a necessary element of the linked list.
Code representation of node:
Create a structure node and a new chain header pointer;
typedef struct Node { ElemType data; struct Node *next; }Node; Node *LinkList;
(1) . obtain the elements in the linear table:
bool GetElem(SqList L,int t,ElemType *e) { int j; LinkList p; // Declaration pointer P p = L->next; //p points to the first node of the linked list L j = 1; // Counter while(p&&j<i) { p=p->next; ++j; } if(!p||j>i) return false; *e = p->data; return true; }
(2) , insert elements in linear table:
bool ListInsert(SqList *L,int i,ElemType e) { int j; LinkList p,s; p = *L; j=1; while(p&&j<i) { p=p->next; +j; } if(!p||j>i) return false; s = (LinkList)malloc(sizeof(Node)); s->data = e; // The value of e is assigned to the data field of s s->next = p->next; // The insertion node s assigns the next address of p to the pointer field of S; Order cannot be changed!!! p->next = s; // Change the pointer field of P to point to s; return true; }
(3) . delete elements in linear table:
bool ListDelete(SqList *L,int i,ElemType *e); { int j; LinkList p,q; p = *L; j = 1; while(p->next && j < i) { p = p->next; ++j; } if(!(p->next)|| j>i) return false; q = p->next; // Use q to store the location of the node to be deleted p->next = q->next; // Update p's node pointer field *e = q->data; free(q); // Release q return true; }
- advantage:
1: The non contact storage mode is adopted to reduce the requirements for memory.
2: The storage is convenient, and the performance of insertion and deletion of sequential structure has been greatly improved;
- Disadvantages:
1: The search performance is weak, which is not as efficient as sequential storage.
3. Static linked list
- The linked list described by array is called static linked list;
#define Max 1000 typedef struct { ElemType data; // Data domain int index; // Pointer field, used to record the subscript of the array and point to the subscript of the next element }
(1) Static linked list lookup:
The same as the search of linear storage, traverse and search at the same time;
(2) Insertion of static linked list:
int Malloc_SSL(StaticLinkList space) { int i = space[0].index; if(space[0].index) space[0].index = space[i].index; return i; } bool ListInsert(SqList *L,int i,ElemType e) { int j,k,l; k = Max-1; // k is the subscript of the last element if(i<1||i>ListLength(L) +1) // If i the condition is not met, an error is returned: return false; j = Malloc_SSL(L); // Gets the subscript of the free component if(j) // There are also free components; { L[i].data = e; for(l=1;l<=i-1;l++) //Find the previous position of the ith element. k = L[k].index; L[i].index = L[k].index; // Assign the position index before the ith element to the position of the new element. L[k].index = j; // Assign the subscript of the new element to the pointer field of the ith element; return true; } return false; }
(3) To delete an element from a linear table:
void Free_SSL(StaticLinkList space, int k) { space[K].index = space[0].index; space[0].cur = k; } bool ListDelete(SqList *L,int i,ElemType *e); { int j,k; if(i<1||i>ListLength(L)) return false; k = Max-1; for(j = 1;j<=i-1;j++) k = L[k].index; j =L[k].index; L[k].index = L[j].index; Free_SSL(L,j); return true; }
- advantage:
- During insert and delete operations, you only need to modify the cursor without moving elements;
- Disadvantages:
- It does not solve the problems caused by continuous storage and loses the characteristics of sequential storage;