鱼C论坛

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

[已解决]C语言 链表

[复制链接]
发表于 2020-3-31 22:34:34 | 显示全部楼层 |阅读模式

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

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

x
菜单第1项完成:创建一个仅含头结点的空链表,如果创建成功,输出创建空链表成功,返回头结点的指针,否则输出创建空链表失败,退出程序运行。
菜单第2项完成:依次输入一组整数,以-1作为结束的标志,创建由这些输入数据所形成的带头结点的单链表,输出链表的数据信息,返回头结点的指针。
若输入:45,45, 23,45,54,25, -1。则生成链表:
#include<stdio.h>
#include<stdlib.h>

int Menu(void);
struct link *Num1();
struct link *Num2(struct link *head);

struct link
        {
                int data;
                struct link *next;
}*b;

void main()
{
        int choice;
        struct link *head1;
        while(1)
        {
                choice=Menu();
                switch(choice)
                {
                        case 1:head1=Num1();break;
                        case 2:Num2(head1);break;
                        /*case 3:Num3();break;
                        case 4:Num4();break;
                        case 5:Num5();break;*/
                        case 6:
                                printf("End of program!\n");
                                exit(0);
                        default:
                                printf("Input error!!!\n");
                }
        }
}

int Menu()
{
        int choice;
        printf("1、任务一\n");
        printf("2、任务二\n");
        printf("3、任务三\n");
        printf("4、任务四\n");
        printf("5、任务五\n");
        printf("6、退出程序\n");
        printf("Please input your choice:");
        scanf("%d",&choice);
        return choice;
}

struct link *Num1()
{
        struct link *head=NULL;
        b = (struct link *)malloc(sizeof(struct link));
        head=b;
        if(b==NULL)
        {
                printf("创建空链表失败\n");
                exit(1);
        }
        else
                printf("创建空链表成功\n");
        free(b);
        return head;
}

struct link *Num2(struct link *head)
{
        struct link *temp;
        printf("输入一组数:");
        scanf("%d",b->data);
        if(head!=NULL)
        {
                temp=head;
                head=b;
                b->next=temp;
        }
        else
        {
                head=b;
                b->next=NULL;
        }
        return head;
}
菜单第一项创建一个空链表都不确定对不对,希望能帮我看看,再帮我写一下菜单二,能对着我的代码更改就最好了嘿嘿,对链表比较陌生,谢谢谢谢。
最佳答案
2020-4-1 11:17:45
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int Menu(void);
  4. struct link *Num1();
  5. struct link *Num2(struct link *head);

  6. struct link
  7. {
  8.     int data;
  9.     struct link *next;
  10. }*link;

  11. int main()
  12. {
  13.     int choice;
  14.     struct link *head;
  15.     while(1)
  16.     {
  17.         choice=Menu();
  18.         switch(choice)
  19.         {
  20.             case 1:head=Num1();break;
  21.             case 2:Num2(head);break;
  22.           /*case 3:Num3();break;
  23.             case 4:Num4();break;
  24.             case 5:Num5();break;*/
  25.             case 6:
  26.                  printf("End of program!\n");
  27.                  exit(0);
  28.             default:
  29.                  printf("Input error!!!\n");
  30.                  break;
  31.          }
  32.     }

  33.     getchar();
  34.     return 0;
  35. }

  36. int Menu()
  37. {
  38.         int choice;
  39.         printf("1、任务一\n");
  40.         printf("2、任务二\n");
  41.         printf("3、任务三\n");
  42.         printf("4、任务四\n");
  43.         printf("5、任务五\n");
  44.         printf("6、退出程序\n");
  45.         printf("Please input your choice:");
  46.         scanf("%d",&choice);
  47.         return choice;
  48. }

  49. struct link *Num1()
  50. {
  51.         struct link *head=NULL;
  52.         head = (struct link*)malloc(sizeof(struct link));
  53.         head->next = NULL;
  54.         if(head==NULL)
  55.         {
  56.             printf("创建空链表失败\n");
  57.             exit(1);
  58.         }
  59.         else
  60.             printf("创建空链表成功\n");
  61.         return head;
  62. }

  63. struct link *Num2(struct link *head)
  64. {
  65.     struct link *temp, *p;
  66.     p = head;
  67.     if(p==NULL)
  68.     {
  69.         printf("链表不存在!\n");
  70.         exit(1);
  71.     }
  72.     else
  73.     {
  74.         while (1)
  75.         {
  76.             temp = (struct link*)malloc(sizeof(struct link));
  77.             if(temp==NULL)
  78.             {
  79.                 printf("内存分配失败!\n");
  80.                 exit(1);
  81.             }
  82.             printf("输入一个数(-1退出): ");
  83.             scanf("%d",&temp->data);
  84.             if (temp->data== -1)
  85.                 break;


  86.             p->next = temp;
  87.             temp->next = NULL;
  88.             p = temp;
  89.         }
  90.         p->next = NULL;
  91.     }

  92.     for (p=head->next; head; p=p->next)
  93.     {
  94.         printf("%d ", p->data);
  95.     }
  96.     printf("\n");
  97.    
  98.     return head;
  99. }
复制代码


基本实现了,还存在一些问题,自己参考吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-1 11:17:45 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int Menu(void);
  4. struct link *Num1();
  5. struct link *Num2(struct link *head);

  6. struct link
  7. {
  8.     int data;
  9.     struct link *next;
  10. }*link;

  11. int main()
  12. {
  13.     int choice;
  14.     struct link *head;
  15.     while(1)
  16.     {
  17.         choice=Menu();
  18.         switch(choice)
  19.         {
  20.             case 1:head=Num1();break;
  21.             case 2:Num2(head);break;
  22.           /*case 3:Num3();break;
  23.             case 4:Num4();break;
  24.             case 5:Num5();break;*/
  25.             case 6:
  26.                  printf("End of program!\n");
  27.                  exit(0);
  28.             default:
  29.                  printf("Input error!!!\n");
  30.                  break;
  31.          }
  32.     }

  33.     getchar();
  34.     return 0;
  35. }

  36. int Menu()
  37. {
  38.         int choice;
  39.         printf("1、任务一\n");
  40.         printf("2、任务二\n");
  41.         printf("3、任务三\n");
  42.         printf("4、任务四\n");
  43.         printf("5、任务五\n");
  44.         printf("6、退出程序\n");
  45.         printf("Please input your choice:");
  46.         scanf("%d",&choice);
  47.         return choice;
  48. }

  49. struct link *Num1()
  50. {
  51.         struct link *head=NULL;
  52.         head = (struct link*)malloc(sizeof(struct link));
  53.         head->next = NULL;
  54.         if(head==NULL)
  55.         {
  56.             printf("创建空链表失败\n");
  57.             exit(1);
  58.         }
  59.         else
  60.             printf("创建空链表成功\n");
  61.         return head;
  62. }

  63. struct link *Num2(struct link *head)
  64. {
  65.     struct link *temp, *p;
  66.     p = head;
  67.     if(p==NULL)
  68.     {
  69.         printf("链表不存在!\n");
  70.         exit(1);
  71.     }
  72.     else
  73.     {
  74.         while (1)
  75.         {
  76.             temp = (struct link*)malloc(sizeof(struct link));
  77.             if(temp==NULL)
  78.             {
  79.                 printf("内存分配失败!\n");
  80.                 exit(1);
  81.             }
  82.             printf("输入一个数(-1退出): ");
  83.             scanf("%d",&temp->data);
  84.             if (temp->data== -1)
  85.                 break;


  86.             p->next = temp;
  87.             temp->next = NULL;
  88.             p = temp;
  89.         }
  90.         p->next = NULL;
  91.     }

  92.     for (p=head->next; head; p=p->next)
  93.     {
  94.         printf("%d ", p->data);
  95.     }
  96.     printf("\n");
  97.    
  98.     return head;
  99. }
复制代码


基本实现了,还存在一些问题,自己参考吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-2 15:40:09 | 显示全部楼层
chxchxkkk 发表于 2020-4-1 11:17
基本实现了,还存在一些问题,自己参考吧

使用了malloc函数,你后边没有释放空间,如果添加了free有影响吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 00:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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