鱼C论坛

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

[已解决]求助,关于单链表的题

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

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

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

x
题目:
用单链表存储键盘输入的整数。对输入的整数进行降序排序(冒泡法),排序完成后输出链表中的所有数据。实现插入的功能。每次从键盘输入一一个整数,将这个整数插入链表中。要求插入以后仍然保持链表中的整数处于降序排列的状态,需要在插入完成后打印链表中的所有数据。
最佳答案
2020-12-23 22:59:05
  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.                                 p = r;
  96.                                 q = p->next;
  97.                                 r = q->next;//指向整体后移
  98.                                 flag = 1;
  99.                         }else{
  100.                                 p = q;
  101.                                 q = p->next;//指向整体后移便于接下来排序
  102.                         }
  103.                 }
  104.         }
  105. }
  106. struct data *insert(struct data *head,struct data *stud)//定义插入结点函数
  107. {
  108.         struct data *p0,*p1,*p2;
  109.         p1=head;
  110.         p0=stud;
  111.         if(head==NULL)
  112.         {
  113.                 head=p0;p0->next=NULL;
  114.         }
  115.         else
  116.         {
  117.                 while((p0->n>p1->n)&&(p1->next!=NULL)){
  118.                         p2=p1;
  119.                         p1=p1->next;
  120.                 }
  121.                 if(p0->n<=p1->n)
  122.                 {
  123.                         if(head==p1)
  124.                         head=p0;
  125.                         else
  126.                         p2->next=p0;
  127.                         p0->next=p1;
  128.                 }
  129.                 else
  130.                 {p1->next=p0;p0->next=NULL;
  131.                 }
  132.         }
  133.         m=m+1;
  134.         return(head);
  135. }
  136. struct data *remove(struct data*head,int n)//定义删除结点函数
  137. {
  138.         struct data *p1,*p2;
  139.         if(head==NULL)
  140.         {
  141.                 printf("\nlist null! \n");
  142.                 return(head);
  143.         }
  144.         p1=head;
  145.         while(n!=p1->n&&p1->next!=NULL)
  146.         {
  147.                 p2=p1;p1=p1->next;
  148.         }
  149.         if(n==p1->n)
  150.         {
  151.                 if(p1==head)head=p1->next;
  152.                 else p2->next=p1->next;
  153.                 printf("delete:%d\n",n);
  154.                 n=n-1;
  155.         }
  156.         else printf("%d not been found!\n",n);
  157.         return(head);
  158. }
复制代码
之前写的升序的,降序自己改改就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-23 22:59:05 | 显示全部楼层    本楼为最佳答案   
  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.                                 p = r;
  96.                                 q = p->next;
  97.                                 r = q->next;//指向整体后移
  98.                                 flag = 1;
  99.                         }else{
  100.                                 p = q;
  101.                                 q = p->next;//指向整体后移便于接下来排序
  102.                         }
  103.                 }
  104.         }
  105. }
  106. struct data *insert(struct data *head,struct data *stud)//定义插入结点函数
  107. {
  108.         struct data *p0,*p1,*p2;
  109.         p1=head;
  110.         p0=stud;
  111.         if(head==NULL)
  112.         {
  113.                 head=p0;p0->next=NULL;
  114.         }
  115.         else
  116.         {
  117.                 while((p0->n>p1->n)&&(p1->next!=NULL)){
  118.                         p2=p1;
  119.                         p1=p1->next;
  120.                 }
  121.                 if(p0->n<=p1->n)
  122.                 {
  123.                         if(head==p1)
  124.                         head=p0;
  125.                         else
  126.                         p2->next=p0;
  127.                         p0->next=p1;
  128.                 }
  129.                 else
  130.                 {p1->next=p0;p0->next=NULL;
  131.                 }
  132.         }
  133.         m=m+1;
  134.         return(head);
  135. }
  136. struct data *remove(struct data*head,int n)//定义删除结点函数
  137. {
  138.         struct data *p1,*p2;
  139.         if(head==NULL)
  140.         {
  141.                 printf("\nlist null! \n");
  142.                 return(head);
  143.         }
  144.         p1=head;
  145.         while(n!=p1->n&&p1->next!=NULL)
  146.         {
  147.                 p2=p1;p1=p1->next;
  148.         }
  149.         if(n==p1->n)
  150.         {
  151.                 if(p1==head)head=p1->next;
  152.                 else p2->next=p1->next;
  153.                 printf("delete:%d\n",n);
  154.                 n=n-1;
  155.         }
  156.         else printf("%d not been found!\n",n);
  157.         return(head);
  158. }
复制代码
之前写的升序的,降序自己改改就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-23 22:59:57 | 显示全部楼层
运行结果
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-4 05:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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