|
发表于 2014-6-5 17:18:22
|
显示全部楼层
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- #define LEN sizeof(struct student)
- struct student *creat(); // 创建链表
- struct student *del(struct student *head, int num); // del函数用于删除节点
- struct student *insert(struct student *head, struct student *stu_2); /*插入一个节点, 第一个形参,待插入的链表, 第二个形参,待插入的结构的地址*/
- void print(struct student *head); // 打印链表, 可以不用参数
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- int n;
- void main()
- {
- struct student *stu, *p, *stu_2;
- int del_num;
- printf("input records!\n");
- stu=creat();
- p = stu;
- print(p);
- printf("\n");
- printf("please input the num to delete: "); //删除
- scanf("%d", &del_num);
- while(del_num!=0) // 循环删除,0 退出
- {
- p=del(p,del_num);
- print(p);
- printf("please input the num to delete: ");
- scanf("%d", &del_num);
- }
-
- printf("\nplease input the record to insert: \n"); //插入
- stu_2=(struct student * )malloc(LEN);
- printf("please input the num : ");
- scanf("%d",&stu_2->num);
- printf("please input the score : ");
- scanf("%f",&stu_2->score);
- while(stu_2->num!=0)
- {
- p=insert(stu,stu_2);
- print(p);
- printf("\nplease input the record to insert: \n");
- stu_2=(struct student * )malloc(LEN);
- printf("please input the num : ");
- scanf("%d",&stu_2->num);
- printf("please input the score : ");
- scanf("%f",&stu_2->score);
- }
-
- printf("\n");
- system("pause");
- }
- struct student *creat() //创建链表
- {
- struct student *head;
- struct student *p1, *p2;
- p1=p2=(struct student * )malloc(LEN); // LEN 是结构体 student 的长度大小
- printf("please input the num : ");
- scanf("%d",&p1->num);
- printf("please input the score : ");
- scanf("%f",&p1->score);
- printf("\n");
-
- head=NULL;
- n=0;
- while(p1->num!=0)
- {
- n=n+1;
- if(n==1)
- {
- head=p2;
- }
- else
- {
- p2->next=p1;
- }
- p2=p1; // p2 指向表尾, p1为新建单元
- p1=(struct student * )malloc(LEN);
- printf("please input the num : ");
- scanf("%d",&p1->num);
- printf("please input the score : ");
- scanf("%f",&p1->score);
- printf("\n");
- }
- p2->next=NULL;
- return head;
- }
- void print(struct student *head) //打印链表
- {
- struct student *p;
- printf("\nThere are %d records!\n\n",n);
- p=head;
- if(head!=NULL)
- {
- do
- {
- printf("学号为 %d 的成绩为 %f \n", p->num, p->score);
- p=p->next;
- }while(p!=NULL);
- }
- }
- struct student *del(struct student *head, int num) // 删除链表节点
- {
- struct student *p1, *p2;
- if(head==NULL)
- {
- printf("\nList null\n");
- goto end;
- }
- p1=head;
- while(num!=p1->num && p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(num==p1->num)
- {
- if(p1==head) //要删的是头结点的时候
- {
- head=p1->next;
- }
- else
- {
- p2->next=p1->next;
- }
- printf("delete: No. %d succeed!\n", num);
- n=n-1; //n是一个全局变量,记录存放了多少链表单元
- }
- else
- {
- printf("%d not been found! \n", num);
- }
- end:
- return head;
- }
- struct student *insert(struct student *head, struct student *stu_2) //插入函数
- {
- struct student *p0,*p1, *p2;
- p1=head;
- p0=stu_2;
- if(head==NULL)
- {
- head=p0;
- p0->next=NULL;
- }
- else
- {
- while( (p0->num > p1->num) && (p1->next!=NULL) ) // 两种情况退出 while
- {
- p2=p1;
- p1=p1->next;
- }
- if(p0->num <= p1->num)
- {
- if(head==p1)
- {
- head=p0;
- }
- else
- {
- p2->next=p0;
- }
- p0->next=p1;
- }
- else //p0的num最大,插在尾部
- {
- p1->next=p0;
- p0->next=NULL;
- }
- n=n+1;
- return head;
- }
- }
复制代码 c语言,第十章第五节, 结构体与共用体,共057.
作业, 实现多次插入和多次删除。
|
|