Operation and test of single linked list (C language)
- "Single linklist. H" header file
- 1. Create a single chain table of leading nodes (insert from header)
- 2. Create a single chain table of leading nodes (insert from the end of the table)
- 3. Find the length of the table
- 4. Search element nodes by sequence number
- 5. Search (locate) by value
- 6. Insert nodes by serial number
- 7. Delete nodes by serial number
- 8. Release single chain table
- 9. Print the linked list
- 10. Flip the list
- Mode.cpp
"Single linklist. H" header file
#ifndef _SINGLE_LL #define _SINGLE_LL #include<stdlib.h> #include<stdio.h> #define flag -100 typedef int dataType; //Node of single chain table typedef struct lnode{ dataType data; struct lnode *next; }Lnode,*LinkList;
1. Create a single chain table of leading nodes (insert from header)
//Create a single chain table with leading nodes LinkList Creat_LinkList1( ){ //Insert node from header (with header node) LinkList L; Lnode *s; int x; L=(Lnode*)malloc(sizeof(Lnode)); //Create head node L->next=NULL; //The pointer field of the last initialization node is NULL scanf("%d",&x); while(x!=flag) { s=(Lnode*)malloc(sizeof(Lnode)); s->data=x; s->next=L->next; L->next=s; scanf("%d",&x); } return L; }
2. Create a single chain table of leading nodes (insert from the end of the table)
Note: use an R pointer to point to the last node of the current linked list
//Create a single chain table with leading nodes LinkList Creat_LinkList2( ){ //Insert a node from the end of the table (with a head node) LinkList L; Lnode *s,*R; int x; L=(Lnode*)malloc(sizeof(Lnode)); L->next=NULL; R=L; //R points to the last node of the current linked list scanf("%d",&x); while(flag!=x) { s=(Lnode*)malloc(sizeof(Lnode)); s->data=x; R->next=s; R=s; scanf("%d",&x); } R->next=NULL; //Set the next pointer of the table end node to null return L; }
3. Find the length of the table
//Find table length int Length_LinkList(LinkList L){ int l=0; Lnode *p=L; if(p->next) //Determine whether the successor node exists, then l+1 { p=p->next; l++; } return l; }
4. Search element nodes by sequence number
//Find element nodes by sequence number Lnode* Get_LinkList(LinkList L,int i){ int j=0; Lnode *p=L; if(i<0) { printf("Illegal serial number value, search failed!\n"); return NULL; } while(p->next && j<i) { p=p->next; j++; } if(j==i)return p; //Judge whether the node is found else NULL; }
5. Search (locate) by value
//Find (locate) by value Lnode* Locate_LinkList(LinkList L,dataType x){ Lnode *p=L; while(p->next && p->data!=x) p=p->next; if(p->data==x) return p; //It is similar to searching by sequence number to determine whether a node with data=x has been found else return NULL; }
6. Insert nodes by serial number
//Insert node by sequence number int Insert_LinkList( LinkList L,int i,dataType x){ Lnode *p,*s; p=Get_LinkList(L,i-1); if(!p) return 0; else { s=(Lnode*)malloc(sizeof(Lnode)); s->data=x; s->next=p->next; p->next=s; } return i; }
7. Delete nodes by serial number
//Delete node by S / n int Delete_LinkList( LinkList L,int i,dataType *x){ Lnode *p,*s; p=Get_LinkList(L,i-1); if(!p) return 0; else { s=p->next; p->next=p->next->next; *x=s->data; free(s); } return i; }
8. Release single chain table
Do not forget the header node
//Release single linked list int Free_LinkList(LinkList L){ Lnode *p=L->next; Lnode *s; free(L); while(p) { s=p; p=p->next; free(s); } return 10; }
9. Print the linked list
//Print linked list int display_LinkList(LinkList L){ Lnode *p=L->next; if(!p) { printf("Table empty\n"); return -1; } else while(p->next) { printf("%-3d",p->data);//Can only print to the penultimate node p=p->next; } printf("%-3d\n",p->data); //Print table footer return 10; }
10. Flip the list
Note: in order to clear the way of thinking in this flipping operation, it is necessary to flexibly split a linked list into two linked lists at the beginning of the pointer
//Flip list LinkList reverse(LinkList L){ Lnode *p=L->next,*s; L->next=NULL; //Set the head pointer to null to become the head pointer of the new linked list while(p) { s=p; p=p->next; s->next=L->next; L->next=s; } } /* * Note: in order to clear the way of thinking in this flipping operation, it is necessary to flexibly split a linked list into two linked lists at the beginning of the pointer */ #endif
Mode.cpp
#include"single_LinkList.h" #include<stdio.h> int main(){ LinkList L; int x,f1,f2,f3; L=Creat_LinkList2(); //Create a table if(L!=NULL) display_LinkList(L); printf("Insert element at node 3: 78\n"); f1=Insert_LinkList(L,3,78); if(f1==3) display_LinkList(L); printf("Delete node 8\n"); f2=Delete_LinkList(L,8,&x); if(f2==8) display_LinkList(L); f3=Free_LinkList(L); if(f3>0) printf("Release the list, it's over!"); }
supplement
: Xiaobai on the road, the first CSDN, for learning, haha