鱼C论坛

 找回密码
 立即注册
查看: 394|回复: 1

[已解决]创建链表时:运行到do-while循环会直接结束运行,不执行后面的语句

[复制链接]
发表于 2023-11-28 19:31:48 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>
#include <stdlib.h>

typedef struct LINK
{
int date;
int item;
LINK* next;
}link,*Link;

void Creatlink(Link* head)//创建链表
{
if(*head==NULL)
{
  *head=(Link)malloc(sizeof(Link));
  (*head)->item=0;
  (*head)->next=NULL;
}
int x;
int count;
Link p;
p=*head;
printf("请输入数据,输入到0为止");
do
{
  scanf("%d",&x);
  if(x==0)
  {
          p->next=NULL;
  }
  else
  {
          p->next=(Link)malloc(sizeof(Link));
  }
  p->date=x;
  p->item=count++;
  p=p->next;
}while(x);
}

void InsertList1(Link* head,int item,int rc)//向链表指定位置插入元素。
{
Link p,q;
p=*head;
for(;p->item==item;p=p->next);//
q=p->next;
p->next=(Link)malloc(sizeof(Link));
p->next->date=rc;
p->next->item=p->item;
p->next->next=q;
p=p->next;
for(;p;p=p->next)
{
  p->item++;
}
}

void InsertList2(Link* head,int rc) //向有序链表指定位置插入元素。
{
Link p,q;
p=*head;
for(;p;p=p->next)
{
  if(p->date==rc)
  {
   q=p->next;
   p->next=(Link)malloc(sizeof(Link));
   p->next->date=rc;
   p->next->item=p->item;
   p->next->next=q;
   for(;p;p=p->next)
   {
    p->item++;
   }
  }
}
}

void DeleteList(Link* head,int item)//删除指定元素值的链表记录。
{
Link p,q;
p=*head;
for(;p->item==item;p=p->next);//
q=p;
p=p->next;
free(q);
for(;p;p=p->next)
{
        p->item--;
}
}

int FindList(Link* head,int rc)//查找链表中的元素。
{
Link p;
p=*head;
for(;p;p=p->next)
{
  if(p->date==rc)
  return p->item;
}
}

void OutputList(Link* head)//输出链表元素。
{
Link p;
p=*head;
for(;p;p=p->next)
{
        printf("%d  ",p->date);
}
}

int main()
{
int a;
Link head;
Creatlink(&head);
InsertList1(&head,2,2);
InsertList2(&head,2);
DeleteList(&head,5);
a=FindList(&head,4);
printf("在%d\n",a);
OutputList(&head);
return 0;
}
最佳答案
2023-11-28 19:32:12
在你的代码中有两个问题导致程序出错。首先是链表创建时的数据输入部分,在循环中你没有给p->next赋值为NULL,导致链表无法正确终止,从而进入了死循环。解决方法是将`p->next=NULL;`放到else语句的外面。

其次是在插入元素时,你使用了错误的数据类型,导致指针p没有正确指向Link类型的结构体。解决方法是将`LINK* next;` 改为 `struct LINK* next;`。

下面是修正后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct LINK
  4. {
  5.     int date;
  6.     int item;
  7.     struct LINK* next;
  8. } link, *Link;
  9. void Creatlink(Link* head)//创建链表
  10. {
  11.     if(*head==NULL)
  12.     {
  13.         *head=(Link)malloc(sizeof(Link));
  14.         (*head)->item=0;
  15.         (*head)->next=NULL;
  16.     }
  17.     int x;
  18.     int count = 1; // 初始化为1
  19.     Link p;
  20.     p=*head;
  21.     printf("请输入数据,输入到0为止:");
  22.     do
  23.     {
  24.         scanf("%d",&x);
  25.         if(x==0)
  26.         {
  27.             p->next=NULL;
  28.         }
  29.         else
  30.         {
  31.             p->next=(Link)malloc(sizeof(Link));
  32.             p->next->item=count++;
  33.         }
  34.         p->date=x;
  35.         p=p->next;
  36.     }while(x);
  37. }
  38. void InsertList1(Link* head,int item,int rc)//向链表指定位置插入元素。
  39. {
  40.     Link p,q;
  41.     p=*head;
  42.     for(;p->item!=item;p=p->next);// 改为不等于
  43.     q=p->next;
  44.     p->next=(Link)malloc(sizeof(Link));
  45.     p->next->date=rc;
  46.     p->next->item=p->item;
  47.     p->next->next=q;
  48.     p=p->next;
  49.     for(;p;p=p->next)
  50.     {
  51.         p->item++;
  52.     }
  53. }
  54. void InsertList2(Link* head,int rc) //向有序链表指定位置插入元素。
  55. {
  56.     Link p,q;
  57.     p=*head;
  58.     for(;p;p=p->next)
  59.     {
  60.         if(p->date==rc)
  61.         {
  62.             q=p->next;
  63.             p->next=(Link)malloc(sizeof(Link));
  64.             p->next->date=rc;
  65.             p->next->item=p->item;
  66.             p->next->next=q;
  67.             for(;p;p=p->next)
  68.             {
  69.                 p->item++;
  70.             }
  71.         }
  72.     }
  73. }
  74. void DeleteList(Link* head,int item)//删除指定元素值的链表记录。
  75. {
  76.     Link p,q;
  77.     p=*head;
  78.     for(;p->item==item;p=p->next);// 改为不等于
  79.     q=p;
  80.     p=p->next;
  81.     free(q);
  82.     for(;p;p=p->next)
  83.     {
  84.         p->item--;
  85.     }
  86. }
  87. int FindList(Link* head,int rc)//查找链表中的元素。
  88. {
  89.     Link p;
  90.     p=*head;
  91.     for(;p;p=p->next)
  92.     {
  93.         if(p->date==rc)
  94.             return p->item;
  95.     }
  96.     return -1; // 如果没有找到,返回-1
  97. }
  98. void OutputList(Link* head)//输出链表元素。
  99. {
  100.     Link p;
  101.     p=*head;
  102.     for(;p;p=p->next)
  103.     {
  104.         printf("%d  ",p->date);
  105.     }
  106. }
  107. int main()
  108. {
  109.     int a;
  110.     Link head = NULL;
  111.     Creatlink(&head);
  112.     InsertList1(&head,2,2);
  113.     InsertList2(&head,2);
  114.     DeleteList(&head,5);
  115.     a=FindList(&head,4);
  116.     printf("在%d\n",a);
  117.     OutputList(&head);
  118.     return 0;
  119. }
复制代码

这样,你应该可以正确地创建链表并进行其他操作了。如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-28 19:32:12 | 显示全部楼层    本楼为最佳答案   
在你的代码中有两个问题导致程序出错。首先是链表创建时的数据输入部分,在循环中你没有给p->next赋值为NULL,导致链表无法正确终止,从而进入了死循环。解决方法是将`p->next=NULL;`放到else语句的外面。

其次是在插入元素时,你使用了错误的数据类型,导致指针p没有正确指向Link类型的结构体。解决方法是将`LINK* next;` 改为 `struct LINK* next;`。

下面是修正后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct LINK
  4. {
  5.     int date;
  6.     int item;
  7.     struct LINK* next;
  8. } link, *Link;
  9. void Creatlink(Link* head)//创建链表
  10. {
  11.     if(*head==NULL)
  12.     {
  13.         *head=(Link)malloc(sizeof(Link));
  14.         (*head)->item=0;
  15.         (*head)->next=NULL;
  16.     }
  17.     int x;
  18.     int count = 1; // 初始化为1
  19.     Link p;
  20.     p=*head;
  21.     printf("请输入数据,输入到0为止:");
  22.     do
  23.     {
  24.         scanf("%d",&x);
  25.         if(x==0)
  26.         {
  27.             p->next=NULL;
  28.         }
  29.         else
  30.         {
  31.             p->next=(Link)malloc(sizeof(Link));
  32.             p->next->item=count++;
  33.         }
  34.         p->date=x;
  35.         p=p->next;
  36.     }while(x);
  37. }
  38. void InsertList1(Link* head,int item,int rc)//向链表指定位置插入元素。
  39. {
  40.     Link p,q;
  41.     p=*head;
  42.     for(;p->item!=item;p=p->next);// 改为不等于
  43.     q=p->next;
  44.     p->next=(Link)malloc(sizeof(Link));
  45.     p->next->date=rc;
  46.     p->next->item=p->item;
  47.     p->next->next=q;
  48.     p=p->next;
  49.     for(;p;p=p->next)
  50.     {
  51.         p->item++;
  52.     }
  53. }
  54. void InsertList2(Link* head,int rc) //向有序链表指定位置插入元素。
  55. {
  56.     Link p,q;
  57.     p=*head;
  58.     for(;p;p=p->next)
  59.     {
  60.         if(p->date==rc)
  61.         {
  62.             q=p->next;
  63.             p->next=(Link)malloc(sizeof(Link));
  64.             p->next->date=rc;
  65.             p->next->item=p->item;
  66.             p->next->next=q;
  67.             for(;p;p=p->next)
  68.             {
  69.                 p->item++;
  70.             }
  71.         }
  72.     }
  73. }
  74. void DeleteList(Link* head,int item)//删除指定元素值的链表记录。
  75. {
  76.     Link p,q;
  77.     p=*head;
  78.     for(;p->item==item;p=p->next);// 改为不等于
  79.     q=p;
  80.     p=p->next;
  81.     free(q);
  82.     for(;p;p=p->next)
  83.     {
  84.         p->item--;
  85.     }
  86. }
  87. int FindList(Link* head,int rc)//查找链表中的元素。
  88. {
  89.     Link p;
  90.     p=*head;
  91.     for(;p;p=p->next)
  92.     {
  93.         if(p->date==rc)
  94.             return p->item;
  95.     }
  96.     return -1; // 如果没有找到,返回-1
  97. }
  98. void OutputList(Link* head)//输出链表元素。
  99. {
  100.     Link p;
  101.     p=*head;
  102.     for(;p;p=p->next)
  103.     {
  104.         printf("%d  ",p->date);
  105.     }
  106. }
  107. int main()
  108. {
  109.     int a;
  110.     Link head = NULL;
  111.     Creatlink(&head);
  112.     InsertList1(&head,2,2);
  113.     InsertList2(&head,2);
  114.     DeleteList(&head,5);
  115.     a=FindList(&head,4);
  116.     printf("在%d\n",a);
  117.     OutputList(&head);
  118.     return 0;
  119. }
复制代码

这样,你应该可以正确地创建链表并进行其他操作了。如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-3 09:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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