|
5鱼币
C 语言结构指针,请大牛帮忙看看链表的创建和增删功能的代码。
帮忙看看问题出在什么问题,老卡死。
另外,帮忙看看,是不是还可以再优化?
在此深表感激!
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>#define LEN sizeof(struct stu)int n=0;//*****************************************************************
- //定义一个学生结构stu,参数只包含了学号和成绩,num,score
- //还有一个指向下一个结点的next指针
- //*****************************************************************struct stu
- {
- int num;
- int score;
- struct stu *next;
- };//*****************************************************************
- //声明主要用到的函数,创建链表,及对链表的增删改操作的函数
- //*****************************************************************
- struct stu *create(); //创建
- void print(struct stu *head);//输出
- struct stu *del(struct stu *head,int num); //删除节点
- struct stu *inser(struct stu *head,struct stu *stu_2);//插入节点
- //*****************************************************************
- //主函数MAIN()
- //*****************************************************************
- void main()
- {
- //定义一个结构指针,用来接收创建的链表
- struct stu *T;
- int num;
- T = create(); //打印输出链表
- print(T); //删除节点
- printf("Please input the num that You want to deleted:");
- scanf("%d",&num);
- T = del(T,num);
- print(T); //插入节点
-
- printf("Please input the data of insert!\n"); struct stu *stu_2; stu_2 = (struct stu *)malloc(LEN); printf("Please input the data of num:");
- scanf("%d",&stu_2->num);
- printf("Please input the data of score:");
- scanf("%d",&stu_2->score);
- T = inser(T,stu_2);
- print(T);
-
-
- system("pause");
- }
- //*****************************************************************
- //创建表
- //*****************************************************************
- struct stu *create()
- {
- struct stu *head;//头指针
- struct stu *p1,*p2;//动态指针,p2跟随p1,保存p1的足迹,以备所需 //用户输入数据前,要手动为结构开辟一个结构空间,并将p1,p2指向此结构地址
- p1 = p2 = (struct stu *)malloc(LEN);
- //提示用户输入数据
- printf("Please input the data of num:");
- scanf("%d",&p1->num);
- printf("Please input the data of score:");
- scanf("%d",&p1->score); head = NULL;
- while(0 != p1->num)
- {
- n++;
- if(1 == n)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
-
- }
- p2 = p1;//此处注意:必须先将p1指向下一个结点,再把p2跟过来,最后开辟新的内存空间
-
- //开避下一个节点内存空间
- p1 = (struct stu *)malloc(LEN); printf("Please input the data of num:");
- scanf("%d",&p1->num);
- printf("Please input the data of score:");
- scanf("%d",&p1->score);
- }
- p2->next = NULL;
- return head;
- }//*****************************************************************
- //打印表
- //*****************************************************************
- void print(struct stu *head)
- {
- //定义指针结构变量来接收链表
- struct stu *p;
- p = head; printf("共有%d条记录被统计\n",n); if(NULL != head)
- {
- while (NULL != p)
- {
- printf("\n该学生学号是:%d,成绩是:%d \n",p->num,p->score);
- p = p->next;
- };
- }
- }//*****************************************************************
- //删除节点
- //*****************************************************************
- struct stu *del(struct stu *head,int num)
- {
- struct stu *p1,*p2; if(NULL != head)
- {
- p1 = head; while(num != p1->num && NULL != p1->next)
- {
-
- p2 = p1;
- p2->next = p1;
- } if(num == p1->num)
- {
- if(head = p1)
- {
- head = p1->next;
- }
- else
- {
- p2->next = p1->next;
- }
- printf("Delete No.%d SUCCEDD!\n",num);
- }
- else
- {
- printf("No.%d is not found!",num);
- }
- n = n - 1;
- }
- else
- {
- printf("This is a Null table!");
- head = NULL;
- }
-
- return head;
- }//*****************************************************************
- //插入节点
- //*****************************************************************
- struct stu *inser(struct stu *head,struct stu *stu_2)
- {
- struct stu *p0;
- struct stu *p1,*p2; p0 = stu_2;
- p1 = head; if(NULL == head)
- {
- head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
- p0->next = NULL;
- }
- else
- {
- while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
- {
-
- p2 = p1;
- p2->next = p1;
- } if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
- {
- if(head == p1)
- {
- head = p0;
- }
- else //中间插入
- {
-
- p2->next = p0;
-
- }
- p0->next = p1;
- }
- else //表尾插入
- {
- p1->next = p0;
- p0->next = NULL;
- }
- }
- n = n + 1;
- return head;
- }
复制代码
|
|