Data Structure and algorithm-C implementation of single-chain table, single-chain data structure and Algorithm
1. difference between the header knot and the header pointer 2. structure pointer description single-chain table 3. obtain element I 4. insert element at position I 5. delete element I 6. create a single-chain table by using the header insertion method. create a single-chain table by using the plug-in method
1. difference between head knots and head pointers head pointer: pointer to the first node of the linked list. If the linked list has a head node, it points to the head node. no matter whether the linked list is empty or not, the header pointer is not empty, the header pointer is a required element of the linked list. It is set up to ensure the unification and convenience of operations. Before the first node, the data domain is generally meaningless, with the first node, the insert and delete operations on the first node are consistent with those on other nodes. structure pointer description single-chain table
#include <stdio.h>
#include <stdlib.h>
Typedef int ElemType;
Typedef struct Node{
ElemType data;//data field
Struct Node *next;// pointer field
} Node;
Typedef struct Node *LinkList
3. Obtain the I-th Element
//p is a pointer to node, p->next points to the next node: the data field of the following node is p->next->data, the pointer field is p->next->next
/ / pointers and structures access member variables, the structure is used. And the pointer uses ->
Int GetElem(LinkList L, int i, ElemType *e){
Int j;
LinkList p;
p = L->next; / / where L represents the pointer, L-> next may be the head node, or the first node
j = 1;
While (p && j < i) {//iteration traversal
p = p->next;
j++;
}
If (!p || j > i) { //p=null or more than i, not found
Return 0;
}
*e = p->data;
Return 1;
}
4. insert an element at position I
Int ListInsert(LinkList *L,int i, ElemType e){
Int j = 1;
LinkList p,s;
What is the difference between p = *L; / / L -> next?
While (p && j < i) {
p = p->next;
j++;
}
If (!p || j > i) {
Return 0;
}
s = (LinkList) malloc(sizeof(Node));
S->data = e;
S->next = p->next;
P->next = s;
Return 0;
}
5. Delete element I
Int ListDelete(LinkList *L, int i, ElemType *e){
Int j = 1;
LinkList p,s;
p = *L;
While (p && j < i) {
p = p->next;
j++;
}
If (!p || j > i) {
Return 0;
}
s = p->next;//First save the i-th node to release
P->next = s->next;//s->next is p->next->next
*e = s->data;
Free(s);
Return 1;
}
6. Create a single-chain table by using the header Insertion Method
Void CreateListHead(LinkList *L, int n){
LinkList p;
Int i;
*L = (LinkList) malloc (sizeof (Node)); / / head node --> header
(*L)->next = NULL;//empty list
For (i = 0; i < n; i++) {
p = (LinkList) malloc(sizeof(Node));//Generate new nodes
P->data = rand() %100 + 1;
P->next = (*L)->next;
(*L)->next = p;
}
}
7. Create a single-chain table using the plug-in method
//r as a dynamic pointer, move down in turn
Void CreateListTail(LinkList *L,int n){
LinkList p,r;
Int i;
*L = (LinkList) malloc (sizeof (Node)); / / head node --> header
r = *L; / / r points to the head node
For (i = 0; i < n; i++) {
p = (LinkList) malloc(sizeof(Node));//new node
P->data = rand() % 100 + 1;
R->next = p;//The head node points to the newly created p node
r = p; / / move r pointer point, there is r = * L head node, become r = p point to p node
}
R->next = NULL;//equivalent to p->next=NULL
}