/*============================================================= 目的:动态链表的综合操作 算法分析:1、构造第一个结构体作为头 2、以P2和P1为游码在结构体移动 3、 p1负责存储新创立的结构体,p2负责指向新创立的结构体 4、creat函数返回链表的头 ============================================================== 作者:最后的村长 时间:2009年12月21日 工具:DEV C++ 4.9.9.2 version:1.0 ==============================================================*/ #include <stdio.h> #include <stdlib.h> //#define NULL 0 #define LEN sizeof(struct student) struct student { long num; int score; struct student *next; };//学生结构体定义,包含两个数据:学号和成绩 int n;//n为全局变量,实现链表中元素个数的统计 struct student *create(void)//开辟动态链表函数 { struct student *head; struct student *p1,*p2; n=0; long int stu_id=1100; p1=p2=(struct student *)malloc(LEN);// p1->num=stu_id; printf("请输入1个学生的成绩:\n"); scanf("%d",&(p1->score));//第一个学生结构体创立结束 head=NULL; while(p1->score!=0) { n=n+1;//统计链表中结构体元素的个数 if(n==1)head=p1;//头指针赋值 else p2->next=p1;//前一个结构体的next指向下一个结构体 //!注意上面语句的执行顺序 p2=p1;//p2移向下一个结构体 p1=(struct student *)malloc(LEN);//新开辟一个结构体内存单元 stu_id=stu_id+2; p1->num=stu_id;//对新开辟的结构体内元素初始化 printf("请输入%d个学生的成绩:\n",n+1); printf("成绩:"); scanf("%d",&(p1->score)); } p2->next=NULL; return head; } /*=============================================================*/ void print(struct student *head) { struct student *p; printf("\n一共是 %d个学生成绩如下 :\n",n); p=head; if(head!=NULL) do { printf("%ld,%d\n",p->num,p->score); p=p->next; }while(p!=NULL); } /*=============================================================*/ struct student*del(struct student *head,long int delnum) { struct student *p1,*p2; p2=p1=head; if(head->num==delnum)//如果要删除的学生是第一个的话 { printf("删除的学生是第一个\n"); head=head->next; n--;//学生总数减一 } else//删除的学生不是第一个 { while(p1->num!=delnum)//遍历链表寻找要删除的学生 { p2=p1; p1=p1->next; } if((p1->next==NULL)&&(p1->num!=delnum))//链表中无此要删除的学生 printf("此链表中无要删除的学生\n"); else if((p1->next==NULL)&&(p1->num==delnum))//要删除的学生是最后一个 { p2->next=NULL; n--;//学生总数减一 } else if(p1->num==delnum)//要删除的学生是链表中的其中一个 { p1=p1->next; p2->next=p1; n--;//学生总数减一 } } return head; } /*=============================================================*/ struct student *insert(struct student *head,struct student *p0) { struct student *p1,*p2; p2=p1=head;//p1和p2指向头指针 if((p0->num)<(head->num))//如果插入学生的学号小于第一个学生的学号 { head=p0; p0->next=p1; } else //如果插入学生的学号大于第一个学生的学号 { while((p0->num)>(p1->num))//遍历链表对学生的的学号进行对比 { p2=p1; p1=p1->next; } printf("p1当前指向的学生的序号是:%ld,成绩是:%d,指向的下一个学生的地址是:%d\n\n",p1->num,p1->score,p1->next); if(p1->next==NULL)//链表遍历结束也没有找到比插入学生的学号大的 { p1->next=p0; p0->next=NULL; } else if((p0->num)<=(p1->num))// { p2->next=p0; p0->next=p1; } } n++; return (head); } int main() { struct student *p,*head; //======================创建并输出链表 head=create();//创建链表 print(head);//打印创建后的链表 for(int i=0;i<10;i++) {printf("========");} puts("\n"); //===========================删除一个学生 unsigned int delnum;//定义要删除的学生号 printf("请输入要删除的学生号:\n"); scanf("%u",&delnum);//输入要删除学生号 head=del(head,delnum);//删除学生号 printf("输出删除学号:%ld的学生后的链表\n",delnum); print(head);//打印删除后的学生号 for(int i=0;i<10;i++) {printf("========");} puts("\n"); //=====================插入一个学生 struct student stu; //初始化待插入的学生的学号和成绩 puts("请输入待插入学生的学号:"); scanf("%ld",&stu.num); puts("请输入待插入学生的成绩:"); scanf("%d",&stu.score); head=insert(head,&stu); printf("输出插入学号:%ld,成绩:%d的学生后的链表\n",stu.num,stu.score); print(head); for(int i=0;i<10;i++) {printf("========");} puts("\n"); //=================== system("PAUSE"); return 0; } |