//CSDN共享博文,兵主出品 #include<stdio.h> #include<stdlib.h> #include<string.h> struct student{ char name[20];// int number; struct student* next; }; int c; struct student *creat(){ struct student *head=NULL; struct student *end,*renew; char buf[256]; c=0; end=renew=(struct student*)malloc(sizeof(struct student)); printf("name: number:\n"); fgets(buf,sizeof(buf),stdin); sscanf(buf,"%s %d",&renew->name,&renew->number); while(renew->number!=0){ c++; if(c==1){ renew->next=NULL; end=renew; head=renew; } else{ renew->next=NULL; end->next=renew; end=renew; } renew=(struct student*)malloc(sizeof(struct student)); printf("name: number:(exit 0 )\n"); fgets(buf,sizeof(buf),stdin); sscanf(buf,"%s %d",&renew->name,&renew->number); } free(renew); return head; } void print(struct student *head){ if(head==NULL){ printf("NULL!"); exit(1); } struct student *temp; int index=1; temp=head; while(temp!=NULL){ printf("index:%d\t",index++); printf(" name : %s\t",temp->name); printf(" number : %d\n",temp->number); temp=temp->next; } } struct student *insert(struct student *head,int index){ struct student *renew,*pre,*temp; int i; char buf[256]; renew=(struct student*)malloc(sizeof(struct student)); printf("name: number:\n"); scanf("%s",&renew->name); scanf("%d",&renew->number); temp=head; if(index==1){//将新插入节点作为首节点 renew->next=head; head=renew; } else{//find data(index) for(i=1;i<index&&i<c;i++){//if donot have "&&i<c" what will happen pre=temp; temp=temp->next;// temp point to next data } if(index<=c){ pre->next=renew; renew->next=temp; } else {//make new data insert into the end temp->next=renew; renew->next=NULL; } } c++;//donot miss,else you canot make change() work return head; } struct student* Delete(struct student *head,int index){ struct student *temp,*pre; int i; temp=head; if(index==1){ head=head->next;//make next data as point free(temp); } else{ for(i=1;i<index;i++){ pre=temp; temp=temp->next; } pre->next=temp->next;// free(temp);//free the memory of temp; } c--; //donot miss,else you canot make change() work return head; } struct student *change(struct student *head,int i,int j ){ struct student *temp,*temp_i,*temp_j,*pre_i,*pre_j; int k; temp_i=head; temp_j=head; for(k=1;k<i;k++){//find data(index_i) pre_i=temp_i; temp_i=temp_i->next; } for(k=1;k<j;k++){//finde data(index_j) pre_j=temp_j; temp_j=temp_j->next; } if(i==1){ if(c==2){ temp_j->next=temp_i; head=temp_j; temp_i->next=NULL; } else if(j==c){ temp_j->next=temp_i->next; head=temp_j; pre_j->next=temp_i; temp_i->next=NULL; } else if(j==i+1){ temp_i->next=temp_j->next; temp_j->next=temp_i; head=temp_j; } else{ temp=head; for(k=1;k<j+1;k++) temp=temp->next; temp_j->next=temp_i->next; head=temp_j; pre_j->next=temp_i; temp_i->next=temp; } } else if(j==c){ if(i==j-1){ pre_i->next=temp_j; temp_j->next=temp_i; temp_i->next=NULL; } else{ temp=head; for(k=1;k<i+1;k++) temp=temp->next; pre_i->next=temp_j; pre_j->next=temp_i; temp_i->next=NULL; temp_j->next=temp; } } else if(j==i+1){ temp=head; for(k=1;k<j+1;k++) temp=temp->next; pre_i->next=temp_j; temp_j->next=temp_i; temp_i->next=temp; } else{ temp=head; for(k=1;k<i+1;k++) temp=temp->next; pre_i->next=temp_j; pre_j->next=temp_i; temp_i->next=temp_j->next; temp_j->next=temp; } return head; } struct student *search(struct student *head,int n){ int i; struct student *temp; temp=head; for(i=1;i<n;i++) temp=temp->next; return temp; } main(){ struct student *head; struct student *temp; char buf[256]; int index,index_i=0,index_j=0; head=creat(); print(head); printf("insert: input 0 to exit\n"); fgets(buf,sizeof(buf),stdin); sscanf(buf,"%d",&index); while(index){ head=insert(head,index); print(head); printf("insert: input 0 to exit\n"); scanf("%d",&index); }
printf("delete: input 0 to exit\n"); scanf("%d",&index); while(index){ head=Delete(head,index); print(head); printf("delete: input 0 to exit\n"); scanf("%d",&index); } printf("change: index_i:,index_j\n"); scanf("%d,%d",&index_i,&index_j); while(index_i!=0&&index_j!=0){ if(index_i>=index_j){ printf("shoulde i<j,input again\n"); exit(0); } head=change(head,index_i,index_j); print(head); printf("change: input index_i:,index_j:(index_i<index_j,input 0 to exit)\n"); fgets(buf,sizeof(buf),stdin); sscanf(buf,"%d,%d",&index_i,&index_j); } printf("search: input index(input 0 to exit):\n"); scanf("%d",&index); printf("index=%d:",index); while(index){ printf("index:%d\t",index); temp=search(head,index); printf("name:%s\t",temp->name); printf("number:%d\n",temp->number); printf("search: input index(input 0 to exit):\n"); fgets(buf,sizeof(buf),stdin); sscanf(buf,"%d",&index); } return 0; }
|