链表的增删改查

该程序为数据结构中单链表的增删改查操作。 具体实现: 将头节点声明为一个全局变量, 这样就很容易实现各个操作。 #include<stdio.h> #include<malloc.h> structstudent { intgrade; intnumber; structstudent*next; }; structstudent*head=NULL; voidcreate() { inti; head=(structstudent*)malloc(sizeof(student)); head->next=NULL; printf("please input grade and number:"); scanf("%d %d",&head->number,&head->grade); structstudent*p=head; structstudent*q=NULL; for(i=0;i<3;i++) { q=(structstudent*)malloc(sizeof(structstudent)); printf("please input grade and number:"); scanf(" %d %d",&q->number,&q->grade); p->next=q; p=q; } p->next=NULL; } voiddisplay() { structstudent*p=NULL; for(p=head;p!=NULL;p=p->next) { printf("%d %d\n",p->number,p->grade);

} } voidinsert(intnum,intgra) { structstudent*p=head; structstudent*q=NULL; while(p!=NULL) { if(p->grade<gra) { if(p->next==NULL) { structstudent*s=NULL; s=(structstudent*)malloc(sizeof(structstudent)); s->number=num; s->grade=gra; p->next=s; s->next=NULL; break; } q=p; p=p->next; } else { structstudent*s=NULL; s=(structstudent*)malloc(sizeof(structstudent)); s->number=num; s->grade=gra; if(p==head) { s->next=p; head=s; break; } else {

s->next=p; q->next=s; break; } } } } voiddel(intgra) { structstudent*p=head; structstudent*q=NULL; while(p) { if(p->grade!=gra) { q=p; p=p->next; } else { if(p==head) { head=p->next; break; } if(p->next==NULL) { q->next=NULL; break; } else { q->next=p->next; break; } } } }

intmain() { intnum,gra,grad; create(); display(); printf("please input the number and grade you want to insert:"); scanf("%d %d",&num,&gra); insert(num,gra); display(); printf("please input the number you want to delete:"); scanf("%d",&grad); del(grad); display(); return0; }


相关文档

链表的操作(增删改查)及逆置链表
c++课程设计《链表的实现-增删改查》
C语言指针链表(增、删、改、查)
NOIP初赛之链表的创建和增删改查
双链表增删改查
c言语链表的增删查改(最简单)
C语言链表实现增删改查
双向循环链表的增删改查
对表的增删改查
单链表的增删查改功能
电脑版