鱼C论坛

 找回密码
 立即注册
查看: 1199|回复: 3

[已解决]大神求救

[复制链接]
发表于 2020-11-20 16:26:12 | 显示全部楼层 |阅读模式

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

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

x
编写程序,实现链表的创建和遍历。输入a,b,c,d,e,f六个字符作为结点的数据,创建具有六个数据元素的链表,然后实现输出链表结点中的数据元素值。要求有交互界面,输入1为创建链表,输入0为退出程序,输入2为显示链表中的数据元素。               不会写这个,希望有大神能帮忙解决
最佳答案
2020-11-20 18:30:16
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct student)

  5. struct student *creat();//创建链表
  6. void print(struct student *head);//打印链表
  7. struct student
  8. {
  9.         char num;
  10.         struct student *next;
  11. };
  12. int n;//用来记录存放了多少数据
  13. int main()
  14. {
  15.         int i,j;
  16.         struct student *stu,*p;
  17.                
  18.         while(1)
  19.         {
  20.                 printf("请输入数字1创建链表:");
  21.                 scanf("%d",&i);
  22.                 if(i==1)
  23.                         break;
  24.         }
  25.         while(i==1)
  26.         {
  27.                 printf("请输入内容:(每个内容已回车隔开): \n");
  28.                 stu=creat();
  29.                 break;
  30.         }
  31.     while(1)
  32.         {
  33.                 printf("请输入数字2显示链表:");
  34.                 scanf("%d",&j);
  35.                 if(j==2)
  36.                         break;
  37.         }
  38.         while(j==2)
  39.         {
  40.                 p=stu;
  41.             print(p);
  42.                 break;
  43.         }
  44.         
  45.         
  46.         printf("\n\n");
  47.         system("pause");
  48. }
  49. struct student *creat()
  50. {
  51.         struct student *head;
  52.         struct student *p1,*p2;
  53.         p1=p2=(struct student *)malloc(LEN);
  54.         p1->num=getchar();   
  55.         head=NULL;
  56.         n=0;
  57.         while(p1->num!='0')
  58.         {
  59.                 n++;
  60.                 if(n==1)
  61.                 {
  62.                         head=p1;
  63.                 }
  64.                 else
  65.                 {
  66.                         p2->next=p1;
  67.                 }
  68.                 p2=p1;
  69.                 p1=(struct student *)malloc(LEN);
  70.             p1->num=getchar();
  71.         }
  72.         p2->next=NULL;
  73.         return head;        
  74. }
  75. void print(struct student *head)
  76. {
  77.         struct student *p;
  78.         p=head;
  79.         if(head!=NULL)
  80.         {
  81.                 do
  82.                 {
  83.                         printf("%3c",p->num);
  84.                         p=p->next;
  85.                 }while(p);
  86.         }
  87. }
复制代码


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-20 16:56:19 | 显示全部楼层
输入0退出程序是指结束对链表的输入吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-20 17:02:35 | 显示全部楼层
a327190489 发表于 2020-11-20 16:56
输入0退出程序是指结束对链表的输入吗?

嗯是的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-20 18:30:16 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct student)

  5. struct student *creat();//创建链表
  6. void print(struct student *head);//打印链表
  7. struct student
  8. {
  9.         char num;
  10.         struct student *next;
  11. };
  12. int n;//用来记录存放了多少数据
  13. int main()
  14. {
  15.         int i,j;
  16.         struct student *stu,*p;
  17.                
  18.         while(1)
  19.         {
  20.                 printf("请输入数字1创建链表:");
  21.                 scanf("%d",&i);
  22.                 if(i==1)
  23.                         break;
  24.         }
  25.         while(i==1)
  26.         {
  27.                 printf("请输入内容:(每个内容已回车隔开): \n");
  28.                 stu=creat();
  29.                 break;
  30.         }
  31.     while(1)
  32.         {
  33.                 printf("请输入数字2显示链表:");
  34.                 scanf("%d",&j);
  35.                 if(j==2)
  36.                         break;
  37.         }
  38.         while(j==2)
  39.         {
  40.                 p=stu;
  41.             print(p);
  42.                 break;
  43.         }
  44.         
  45.         
  46.         printf("\n\n");
  47.         system("pause");
  48. }
  49. struct student *creat()
  50. {
  51.         struct student *head;
  52.         struct student *p1,*p2;
  53.         p1=p2=(struct student *)malloc(LEN);
  54.         p1->num=getchar();   
  55.         head=NULL;
  56.         n=0;
  57.         while(p1->num!='0')
  58.         {
  59.                 n++;
  60.                 if(n==1)
  61.                 {
  62.                         head=p1;
  63.                 }
  64.                 else
  65.                 {
  66.                         p2->next=p1;
  67.                 }
  68.                 p2=p1;
  69.                 p1=(struct student *)malloc(LEN);
  70.             p1->num=getchar();
  71.         }
  72.         p2->next=NULL;
  73.         return head;        
  74. }
  75. void print(struct student *head)
  76. {
  77.         struct student *p;
  78.         p=head;
  79.         if(head!=NULL)
  80.         {
  81.                 do
  82.                 {
  83.                         printf("%3c",p->num);
  84.                         p=p->next;
  85.                 }while(p);
  86.         }
  87. }
复制代码


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-9 06:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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