Data structure (C language version) --- linear table chain storage representation

1. Single-link list: chain storage of linear list.

1) Features: Use a set of arbitrary storage units to store data elements (storage units can be continuous or discontinuous), and the storage locations of logically adjacent elements are not necessarily adjacent.

2) The node includes two fields: a data field (storing data element information) and a pointer field (storing the direct successor storage location)

3) Pointer or chain: the information stored in the pointer field.

2. Head pointer: The storage location of the first node in the linked list. Access to the entire linked list must start from the head pointer.

3. Head node: A node is attached before the first node of the singly linked list, and its data field may not store information, but may store additional information. If the pointer field is empty, the linear table is an empty table.

      The advantages of introducing a head node:

      1) Make the operation at the first position of the linked list consistent with the operation at other positions of the list.

      2) The processing of empty tables and non-empty tables is consistent.

4. Types of nodes in singly linked lists

typedef struct LNode {
 int data;
 struct LNode * next;
}LNode,* Linklist;

5.
 Singly linked list LNode * s, * p, * q;
 Linklist L;
 int i, temp;
 1) Insert the code fragment of the node, the time complexity is O (n).
 p = GetElem (L, i-1);
 s-> next = p-> next;
 p-> next = s;
 2) Insert the * s node into the code fragment before * p, the time complexity is O ( 1).
 s-> next = p-> next;
 p-> next = s;
 temp = p-> data;
 p-> data = s-> data;
 s-> data = temp;
 3) Delete the code fragment of the node, , The time complexity is O (n).
 p = GetElem (L, i-1);
 q = p-> next;
 p-> next = q-> next;
 free (q);
 4) Delete the node * p, the time complexity is O (1).
 q = p-> next;
 p-> data = p-> next-> data;
 p-> next = q-> next;
 free (q);

6. Use the head insertion method to establish a singly linked list, and reversely establish a singly linked list L from the end of the table to the head of the table, inserting elements after the head node each time, the time complexity is O (n).
Linklist list_headinsert (Linklist & L)
{
 LNode * s;
 int x;
 L = (Linklist) malloc (sizeof (LNode));
 L-> data = NULL;
 scanf ("% d", & x);
 while (x! = 9999 )
 {
  s = (LNode *) malloc (sizeof (LNode));
  s-> data = x;
  s-> next = L-> next;
  L-> next = s;
  scanf ("% d", & x);
 }
 return L;
}
7. Use tail interpolation to create a singly-linked list. From the end of the table to the head of the table, a singly-linked list is established in the forward direction. Each time, an element is inserted at the end of the table, and the time complexity is O (n).
Linklist List_tailinsert (Linklist & L)
{
 int x;
 L = (Linklist) malloc (sizeof (LNode));
 LNode * s, * r = L; // r is the end-of-table pointer.
 scanf ("% d", & x);


  s = (LNode *) malloc (sizeof (LNode));
  s-> data = x;
  r-> next = s;
  r = s;
  scanf ("% d", & x);
 }
 r-> next = NULL;
 return L;
}
8. Search for the value of the node according to the serial number, and take out the node pointer at the i-th position in the singly linked list L. The time complexity is O (n).
LNode * GetElem (Linklist L, int i)
{
 int j = 1;
 LNode * p = L-> next;
 if (i == 0)
 {
  return L;
 }
 if (i <1)
 {
  return NULL;
 }
 while ( p && j <i)
 {
  p = p-> next;
  j ++;
 }
 return p;
}
9. Look up the table node by value, find the node pointer of the data field value in the singly linked list L equal to e, the time complexity is O (n ).
LNode * LocateElem (Linklist L, int e)
{
 LNode *p = L->next;
 while (p!=NULL&&p->data!=e)
 {
  p = p->next;
 }
 return p;
}

10. Find the table length and time complexity is O (n).
int listlength (LNode * p)
{
 int len ​​= 0;
 while (p-> next! = NULL)
 {
  len ++;
  p = p-> next;
 }
 return len;
}

11. Circular singly linked list: the pointer of the last node in the table points to the head node.

1) Judgment of empty conditions: the pointer of the head node points to the head pointer.

2) Set the head pointer to operate on the end of the table, the time complexity is O (n).

3) Set the tail pointer to operate on the tail and head of the table, and the time complexity is O (1).

12. The double-linked list node contains two pointer fields, one pointing to the precursor node and one pointing to the succeeding node.

13. Double-linked list node type
typedef struct DNode {
 int data;
 struct DNode * prior, * next;
} DNode, * DLinklist;

14. Double linked list

 DNode * m, * k;
 1) Insert an operation segment with a time complexity of O (1).
 m-> next = k-> next;
 k-> next-> prior = m;
 m-> prior = k;
 k-> next = m;
 2) Delete the operation segment, the time complexity is O (1).
 m-> next = k-> next;
 k-> next-> prior = m;
 free (k);

15. Static linked list: Use the array to describe the chained storage structure of the linear table. The pointer is the relative address of the node (array subscript), also known as the cursor.

16. Structure type of static linked list

#define maxsize 50
typedef struct {
 int data;
 int next;
}slinklist[maxsize];

Guess you like

Origin www.cnblogs.com/xqy1874/p/12721139.html