c: 链表的增删改查的操作
#include<stdio.h> #include<stdlib.h> #define LEN sizeof(struct student) struct student{ int num; double score; struct student *next; }; //创建一个链表 struct student * create(void){ struct student *p1,*p2,*head; int n = 0; p1 = p2 = (struct student *)malloc(LEN); scanf("%d%lf",&(p1->num),&(p1->score)); while(p1->num!=0){ n++; if(n==1){ head = p1; }else{ p2 = p1; } p1 = (struct student *)malloc(LEN); scanf("%d%lf",&(p1->num),&(p1->score)); p2->next = p1; } p2->next = NULL; return head; } struct student *delw(struct student *start,int num){ struct student *p1,*p2; //链表为空 if(start==NULL){ printf("\nlist null!\n"); return NULL; } p1 = start; p2 = NULL; //链表不为空 //链表只有一个元素,且即为所要找的元素 /*if(p1->next==NULL&&p1->num == num){ printf("there is only one element and that is it!"); return NULL; }*/ while(p1->next!=NULL&&p1->num!=num){ p2 = p1; p1 = p1->next; } if(num == p1->num){ if(p1 == start){ return start->next; }else{ p2->next = p1->next; } }else{ printf("number not found!"); } return start; } struct student *del(struct student *head,long num){ struct student *p1, *p2; //链表为空 if(head == NULL){ printf("\nlist null!\n"); return head; } //链表不为空 p1 = head; while(p1->next!=NULL&&num!=p1->num){ p2 = p1; p1 = p1->next; } if(num == p1->num){ if(p1 == head){ head = p1->next; }else{ p2->next = p1->next; printf("delete:%ld\n",num); } }else printf("%ld not been found!\n",num); return head; } //删除一个节点 struct student * deleteNode(struct student *start,int num){ struct student *p1, *p2,*before,*after; //空表 if(start==NULL){ printf("the linktable is null"); return NULL; } p1 = p2 = start; //只有一个节点 if(start->next==NULL){ if(start->num==num){ return NULL; } } //链表不为空(两个以上的节点) //1:链表的第一个即为所要找的 if((start->num == num)&&(start->next!=NULL)){ return start->next; } while(p1!=NULL){ if(p1->num==num){ before = p2; after = p1->next; } p2 = p1; p1 = p1->next; } before->next = after; return start; } struct student * insert(struct student *head,struct student *stu){ struct student *p1,*p2, *p0; p1 = head; p0 = stu; //链表为空 if(head == NULL){ head = p0; p0->next = NULL; printf("the link is null\n"); return NULL; //链表不为空,比较num,(如22, 33, 55),44应插入至33后面 }else{ //一个元素 //printf("head.next is not null"); //两个以上元素: 22 55 88 while(p1->num<p0->num&&p1->next!=NULL){ p2 = p1; p1 = p1->next; } if(p1->num>p0->num){// if(p1==head){//p0排到最前 head = p0; p0->next = p1; }else{ //p0排到中间 p2->next = p0; p0->next = p1; } }else{//p0排到最后 p1->next = p0; p0->next = NULL; } return head; } } void printLink(struct student *p){ struct student *p_afterDeal = p; p_afterDeal = p; while(p_afterDeal!=NULL){ printf("num = %d, score = %lf\n",p_afterDeal->num,p_afterDeal->score); p_afterDeal = p_afterDeal->next; } } int main(void){ struct student * p_std, * p_afterDel,*p,*p_afterInsert; struct student newstd = {44,44.4,NULL}; //创建一个链表 printf("创建一个链表:\n"); p_std = create(); printf("创建的链表如下:\n"); printLink(p_std); //插入一个节点 printf("插入一个节点:44\n"); p = &newstd; p_afterInsert = insert(p_std,p); printf("插入一个节点后的列表如下:\n"); printLink(p_afterInsert); //删除一个节点 printf("删除一个节点:44\n"); p_afterDel = deleteNode(p_afterInsert,44); printf("删除一个节点后的列表如下:\n"); printLink(p_afterDel); system("pause"); }