This program is written for the exercise linked list. It creates, inserts, deletes, traverses, and searches a single-chain table without adding the sorting function.
#include "stdio.h"#include "malloc.h"#include "stdlib.h"#include "conio.h"struct stu{int num;char name[20];struct stu* next;};struct stu* linklist_create(void);struct stu* linklist_insert(struct stu *head);void linklist_visit(struct stu* head);void linklist_delete(struct stu* head);void linklist_find(struct stu* head);int main(void){char choose;struct stu *head;head=NULL;while(1){printf("\n**************** Command Menu ****************\n");printf("**************** 1:creat a new list ****************\n");printf("**************** 2:insert a new element ****************\n");printf("**************** 3:delete an element ****************\n");printf("**************** 4:print the whole list ****************\n");printf("**************** 5:search an element ****************\n");printf("**************** 6:exit ****************\n\n");choose=getch();switch(choose){case '1':{if(head!=NULL){printf("A linklist exists already!\n\n");break;}elsehead=linklist_create();break;}case '2':{if(head==NULL){printf("No list has been created!Choose again!\n\n");break;}elsehead=linklist_insert(head);break;}case '3':{if(head==NULL){printf("No list has been created!Choose again!\n\n");break;}elselinklist_delete(head);break;}case '4':{if(head==NULL){printf("No list has been created!Choose again!\n\n");break;}elselinklist_visit(head);break;}case '5':{if(head==NULL){printf("No list has been created!Choose again!\n\n");break;}elselinklist_find(head);break;}case '6':{printf("Are you sure to exit?N to cancel or the program will exit.\n");if(getch()=='N'){printf("Exit request cancelled!\n");break;}else {printf("Program end!\n");return 0;}}default:printf("Warning!Error choose!\n\n");break;}}free(head);head=NULL;return 0;}struct stu* linklist_create(void){int n;struct stu* head=NULL;struct stu* pf=NULL;struct stu* pb=NULL;printf("How many elements do you want to creat?\n");scanf("%d",&n);for(int i=0;i<n;i++){printf("Input the ID and name.\n");pb=(struct stu*)malloc(sizeof(struct stu));scanf("%d %s",&pb->num,pb->name);if(i==0){head=pb;pf=pb;}else{pf->next=pb;pf=pb;}}pb->next=NULL;return head;}struct stu* linklist_insert(struct stu *head){struct stu *newstu;struct stu *pf=head;struct stu *pb=head;newstu=(struct stu*)malloc(sizeof(struct stu));printf("Input the ID and name.\n");scanf("%d %s",&newstu->num,newstu->name);while(pb->num<=newstu->num){pf=pb;pb=pb->next;if(pb==NULL)break;}pf->next=newstu;newstu->next=pb;return head;}void linklist_visit(struct stu* head){do{printf("ID and name:%d%s\n",head->num,head->name);}while(head=head->next);}void linklist_delete(struct stu* head){int num;struct stu* pf;struct stu* pb;pf=head;pb=head;printf("Which element do you want to delete?");printf("Input the ID:");scanf("%d",&num);while(pb->num!=num){pf=pb;pb=pb->next;if(pb==NULL){printf("The inputed ID doesn't exist!\n");return ;}}pf->next=pb->next;printf("delete OK!\n");}void linklist_find(struct stu* head){int n;printf("Input the ID to find ...\n");scanf("%d",&n);while(head->num!=n){head=head->next;if(head==NULL){printf("the inputed ID dosen't exist!");return ;}}if(head->num==n)printf("ID and name:%d%s\n",head->num,head->name);}