鱼C论坛

 找回密码
 立即注册
查看: 745|回复: 2

[已解决]对于链表的疑惑

[复制链接]
发表于 2020-12-17 12:28:43 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
    1.假设数组里有10个学生成绩,用函数完成建立链表,用数组名做形参,返回链表的头指针;对链表按成绩从高到低排序;实现可以删除成绩为某个特定值的结点;插入一个学生成绩。
    2.用结构体数组模拟链表。分为指针域和下标域,用不交换数据的方式,也就是改下标来实现一组学生成绩的从大到小排列。
    想了一上午还是各种错误,头大了,唉╯﹏╰
最佳答案
2020-12-17 15:30:30
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>//头文件
  4. #define LEN sizeof(struct data)//student结构的大小
  5. struct data *creat();//创建链表
  6. void output(struct data *head);//打印链表
  7. void sort(struct data *head);//排序
  8. struct data *insert(struct data *,struct data *);//插入结点
  9. struct data *remove(struct data*head,int n);//删除节点
  10. int m;
  11. struct data{
  12.         int n;
  13.         struct data *next;
  14. }; //定义结构数组
  15. int N;//全局变量,用来记录存放了多少数据。
  16. int main()//主函数
  17. {
  18.         int remove_n;//定义存放需要删除的数值的变量·
  19.         struct data *stu,node;//声明结构指针变量
  20.         stu=creat();//函数返回链表第一个节点的地址
  21.         output(stu);//打印输出链表
  22.         printf("\n");//换行
  23.         system("pause");
  24.         sort(stu);//排序
  25.         printf("The result of ascending sorting:\n");
  26.         output(stu);//升序输出
  27.         printf("\nPlease enter the number to insert\n");
  28.         scanf("%d",&node.n);
  29.         stu=insert(stu,&node);
  30.         output(stu);
  31.         printf("\nPlease enter the number to delete\n");
  32.         scanf("%d",&remove_n);
  33.         stu=remove(stu,remove_n);
  34.         output(stu);
  35.         return 0;
  36. }
  37. struct data *creat()//创建链表函数
  38. {
  39.         struct data *head;
  40.         struct data *p1,*p2;
  41.         p1=p2=(struct data*)malloc(LEN);//开辟一个长度为LEN的内存区
  42.     p1->n=0;
  43.         head=NULL;//初始化头指针
  44.         N=0;//初始化结点个数
  45.         printf("Stop typing when -1 is entered\n");
  46.         while(p1->n!=-1)
  47.         {
  48.                 N++;
  49.                 if(1==N)
  50.                 {
  51.                         head=p1;
  52.                 }
  53.                 else
  54.                 {
  55.                         p2->next=p1;//把p1所指的结点连接在p2所指的结点后面
  56.                 }
  57.                 p2=p1;
  58.                 p1=(struct data*)malloc(LEN);
  59.                 printf("Please enter a number:");
  60.                 scanf("%d",&p1->n);
  61.         }
  62.         p2->next=NULL;//结束
  63.         return head;//使函数返回头指针
  64. }
  65. void output(struct data *head)//定义打印输出链表函数
  66. {
  67.         struct data *p;//在函数中定义struct data类型的变量p
  68.         p=head->next;//使p指向第一个结点
  69.         if(NULL!=head)//若不是空表
  70.         {
  71.                 do
  72.                 {
  73.                         printf("%d ",p->n);//输出一个结点中的数值
  74.                         p=p->next;//p指向下一个结点
  75.                 }while(p!=NULL);//当p不是空地址
  76.         }
  77. }
  78. void sort(struct data *head)//定义排序函数
  79. {
  80.         struct data *p,*q,*r,*t;
  81.         int flag = 1;
  82.         while(flag){
  83.                 p = head;
  84.                 q = head->next;
  85.                 flag = 0;

  86.                 while(q != NULL){//循环终止条件
  87.                         r = q->next;
  88.                         if(r == NULL){//循环终止条件
  89.                                 break;
  90.                         }else if(q->n > r->n){
  91.                                 t = r->next;
  92.                                 q->next = t;
  93.                                 r->next = q;
  94.                                 p->next = r;
  95.                                 //重新排序
  96.                                 p = r;
  97.                                 q = p->next;
  98.                                 r = q->next;
  99.                                 //指向整体后移便于接下来排序
  100.                                 flag = 1;
  101.                         }else{
  102.                                 p = q;
  103.                                 q = p->next;//指向后移
  104.                         }
  105.                 }
  106.         }
  107. }
  108. struct data *insert(struct data *head,struct data *stud)//定义插入结点函数
  109. {
  110.         struct data *p0,*p1,*p2;
  111.         p1=head;
  112.         p0=stud;
  113.         if(head==NULL)
  114.         {
  115.                 head=p0;p0->next=NULL;
  116.         }
  117.         else
  118.         {
  119.                 while((p0->n>p1->n)&&(p1->next!=NULL)){
  120.                         p2=p1;
  121.                         p1=p1->next;
  122.                 }
  123.                 if(p0->n<=p1->n)
  124.                 {
  125.                         if(head==p1)
  126.                         head=p0;
  127.                         else
  128.                         p2->next=p0;
  129.                         p0->next=p1;
  130.                 }
  131.                 else
  132.                 {p1->next=p0;p0->next=NULL;
  133.                 }
  134.         }
  135.         m=m+1;
  136.         return(head);
  137. }
  138. struct data *remove(struct data*head,int n)//定义删除结点函数
  139. {
  140.         struct data *p1,*p2;
  141.         if(head==NULL)
  142.         {
  143.                 printf("\nlist null! \n");
  144.                 return(head);
  145.         }
  146.         p1=head;
  147.         while(n!=p1->n&&p1->next!=NULL)
  148.         {
  149.                 p2=p1;p1=p1->next;
  150.         }
  151.         if(n==p1->n)
  152.         {
  153.                 if(p1==head)head=p1->next;
  154.                 else p2->next=p1->next;
  155.                 printf("delete:%d\n",n);
  156.                 n=n-1;
  157.         }
  158.         else printf("%d not been found!\n",n);
  159.         return(head);
  160. }
复制代码

前几天学校有个类似的作业,你看看这个行不?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-17 15:30:30 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>//头文件
  4. #define LEN sizeof(struct data)//student结构的大小
  5. struct data *creat();//创建链表
  6. void output(struct data *head);//打印链表
  7. void sort(struct data *head);//排序
  8. struct data *insert(struct data *,struct data *);//插入结点
  9. struct data *remove(struct data*head,int n);//删除节点
  10. int m;
  11. struct data{
  12.         int n;
  13.         struct data *next;
  14. }; //定义结构数组
  15. int N;//全局变量,用来记录存放了多少数据。
  16. int main()//主函数
  17. {
  18.         int remove_n;//定义存放需要删除的数值的变量·
  19.         struct data *stu,node;//声明结构指针变量
  20.         stu=creat();//函数返回链表第一个节点的地址
  21.         output(stu);//打印输出链表
  22.         printf("\n");//换行
  23.         system("pause");
  24.         sort(stu);//排序
  25.         printf("The result of ascending sorting:\n");
  26.         output(stu);//升序输出
  27.         printf("\nPlease enter the number to insert\n");
  28.         scanf("%d",&node.n);
  29.         stu=insert(stu,&node);
  30.         output(stu);
  31.         printf("\nPlease enter the number to delete\n");
  32.         scanf("%d",&remove_n);
  33.         stu=remove(stu,remove_n);
  34.         output(stu);
  35.         return 0;
  36. }
  37. struct data *creat()//创建链表函数
  38. {
  39.         struct data *head;
  40.         struct data *p1,*p2;
  41.         p1=p2=(struct data*)malloc(LEN);//开辟一个长度为LEN的内存区
  42.     p1->n=0;
  43.         head=NULL;//初始化头指针
  44.         N=0;//初始化结点个数
  45.         printf("Stop typing when -1 is entered\n");
  46.         while(p1->n!=-1)
  47.         {
  48.                 N++;
  49.                 if(1==N)
  50.                 {
  51.                         head=p1;
  52.                 }
  53.                 else
  54.                 {
  55.                         p2->next=p1;//把p1所指的结点连接在p2所指的结点后面
  56.                 }
  57.                 p2=p1;
  58.                 p1=(struct data*)malloc(LEN);
  59.                 printf("Please enter a number:");
  60.                 scanf("%d",&p1->n);
  61.         }
  62.         p2->next=NULL;//结束
  63.         return head;//使函数返回头指针
  64. }
  65. void output(struct data *head)//定义打印输出链表函数
  66. {
  67.         struct data *p;//在函数中定义struct data类型的变量p
  68.         p=head->next;//使p指向第一个结点
  69.         if(NULL!=head)//若不是空表
  70.         {
  71.                 do
  72.                 {
  73.                         printf("%d ",p->n);//输出一个结点中的数值
  74.                         p=p->next;//p指向下一个结点
  75.                 }while(p!=NULL);//当p不是空地址
  76.         }
  77. }
  78. void sort(struct data *head)//定义排序函数
  79. {
  80.         struct data *p,*q,*r,*t;
  81.         int flag = 1;
  82.         while(flag){
  83.                 p = head;
  84.                 q = head->next;
  85.                 flag = 0;

  86.                 while(q != NULL){//循环终止条件
  87.                         r = q->next;
  88.                         if(r == NULL){//循环终止条件
  89.                                 break;
  90.                         }else if(q->n > r->n){
  91.                                 t = r->next;
  92.                                 q->next = t;
  93.                                 r->next = q;
  94.                                 p->next = r;
  95.                                 //重新排序
  96.                                 p = r;
  97.                                 q = p->next;
  98.                                 r = q->next;
  99.                                 //指向整体后移便于接下来排序
  100.                                 flag = 1;
  101.                         }else{
  102.                                 p = q;
  103.                                 q = p->next;//指向后移
  104.                         }
  105.                 }
  106.         }
  107. }
  108. struct data *insert(struct data *head,struct data *stud)//定义插入结点函数
  109. {
  110.         struct data *p0,*p1,*p2;
  111.         p1=head;
  112.         p0=stud;
  113.         if(head==NULL)
  114.         {
  115.                 head=p0;p0->next=NULL;
  116.         }
  117.         else
  118.         {
  119.                 while((p0->n>p1->n)&&(p1->next!=NULL)){
  120.                         p2=p1;
  121.                         p1=p1->next;
  122.                 }
  123.                 if(p0->n<=p1->n)
  124.                 {
  125.                         if(head==p1)
  126.                         head=p0;
  127.                         else
  128.                         p2->next=p0;
  129.                         p0->next=p1;
  130.                 }
  131.                 else
  132.                 {p1->next=p0;p0->next=NULL;
  133.                 }
  134.         }
  135.         m=m+1;
  136.         return(head);
  137. }
  138. struct data *remove(struct data*head,int n)//定义删除结点函数
  139. {
  140.         struct data *p1,*p2;
  141.         if(head==NULL)
  142.         {
  143.                 printf("\nlist null! \n");
  144.                 return(head);
  145.         }
  146.         p1=head;
  147.         while(n!=p1->n&&p1->next!=NULL)
  148.         {
  149.                 p2=p1;p1=p1->next;
  150.         }
  151.         if(n==p1->n)
  152.         {
  153.                 if(p1==head)head=p1->next;
  154.                 else p2->next=p1->next;
  155.                 printf("delete:%d\n",n);
  156.                 n=n-1;
  157.         }
  158.         else printf("%d not been found!\n",n);
  159.         return(head);
  160. }
复制代码

前几天学校有个类似的作业,你看看这个行不?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-17 18:39:22 From FishC Mobile | 显示全部楼层
我们也是,太感谢了,我这块学的不扎实,还要多练练。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 15:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表