Data structure - linked list operation (create, output, find, insert, delete, etc.) C language source code
#include<stdio.h> #include<stdlib.h> typedef char datatype; typedef struct node { datatype data; struct node *next; }linklist; linklist *head,*p; //Establishing single chain table by head insertion linklist *Creatlistf() { char ch; linklist *head,*s; head=NULL; ch=getchar(); printf("Please enter sequence table element(Data to*End): \n"); while(ch!='*') { s=(linklist *)malloc(sizeof(linklist)); s->data=ch; s->next=head; head=s; ch=getchar(); } return head; } //Find nodes by sequence number linklist *Get(linklist *head,int i) { int j; linklist *p; p=head;j=0; while((p->next!=NULL)&&(j<i)) { p=p->next; j++; } if(i==j) return p; } //Find nodes by value linklist *Locate(linklist *head,datatype key) { int pos=0; p=head->next; printf("The location of the search node is:"); while(p!=NULL) { if(p->data!=key) { p=p->next; pos++; } else { pos++; break; } } return p; } void Insertafter(linklist *p,datatype x) { linklist *s; s=(linklist *)malloc(sizeof(linklist)); s->data=x; s->next=p->next; p->next=s; printf("Insert success"); } //Forward insertion node void Insertbefore(linklist *p,datatype x) { linklist *s; s=(linklist *)malloc(sizeof(linklist)); s->next=p->next; s->data = p->data; p->data=x; p->next=s; } //Delete Vertex linklist *Deleteafter(linklist *head) { int i; linklist *r,*p; printf("Please enter the node location to delete:"); scanf("%d",&i); if(i==0){ printf("No location.\n"); } else if(i==1) { r=head; head=head->next; } else { p=Get(head,i-1); r=p->next; p->next=r->next; } free(r); return head; } //Output single chain table void output(linklist *p) { while(p->next!=NULL) { printf("%c",p->data); p=p->next; } printf("\n"); } main() { linklist *head; int k,i,pos; char x; datatype key; printf("Operation of single chain table:\n"); printf("\t1.Establishing single chain table by head insertion\n"); printf("\t2.Output single chain table\n"); printf("\t3.Find nodes by sequence number\n"); printf("\t4.Find nodes by value\n"); printf("\t5.Forward insertion node\n"); printf("\t6.Delete Vertex\n"); printf("\t7.Sign out\n"); do { printf("Select the required function: "); scanf("%d",&k); switch(k) { case 1:head=Creatlistf(); break; case 2:printf("The single chain table is:\n"); output(head); break; case 3:printf("Please enter the node number to find: "); scanf("%d",&i); getchar(); printf("The value of the node is:%c\n",Get(head,i-1)->data); break; case 4:printf("Please enter the node value to find: "); scanf("%c",&key); getchar(); printf("%d",Locate(head,key)); printf("\n"); break; case 5: printf("Enter the node location to insert (insert from position 1):"); scanf("%d",&pos); if(pos==0) { printf("No location.\n");break; } getchar(); printf("Please enter the inserted node value:"); scanf("%c",&x); getchar(); Insertbefore(Get(head,pos-1),x); printf("Insert success\n"); break; case 6: head=Deleteafter(head); break; case 7:printf("Sign out"); exit(0); default:printf("Input error\n"); exit(0); } }while(1); }
Look, that's the point! I want to see that there are only two kinds of students here: those who copy the code and those who copy the code.
However, when copying away, you need to think about one thing. After copying away the code, you have an egg to use. It is the most important thing for you to understand.
OK, it's soy sauce.
Old fellow, if this is not acceptable, I can not tell you!!! Oh, by the way, you look so good, pay attention to it...
Finally, I said to myself:
Every misfortune you encounter now comes from a time when you refused to work hard.