鱼C论坛

 找回密码
 立即注册
查看: 1182|回复: 10

[已解决]各位大佬想问一下,我这个元素不存在的时候为什么没办法正常运行

[复制链接]
发表于 2020-9-17 19:17:50 | 显示全部楼层 |阅读模式

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

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

x
//4.屏幕提示后,在以DLList1为头指针的线性表中搜索这个元素key,并给出相应的位置或不存在
void search(Node *DLList1,int key)
{
        Node *p;
        int i=0;
        int flag=1;
       
        p=DLList;
       
        while(p)
        {
                p=p->next;
                i++;
                if(p->data == key)
                {
                        flag=0;
                        break;
                }
        }
       
        if(flag)
        {
                printf("元素不存在!");
                wait();
                return;
        }
        else{
                printf("元素所在位置为:%d",i);
                wait();
        }
       
}
最佳答案
2020-9-17 22:07:44
Christopher. 发表于 2020-9-17 20:09
对,调试可以通过,但是输一个链表里不存在的,无法正常运行出结果

你把search那个函数的while循环条件改为 while(p->next) 就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-17 19:27:58 | 显示全部楼层
贴一下完整代码看看,感觉是死循环了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-17 19:31:49 | 显示全部楼层
孟婆汤 发表于 2020-9-17 19:27
贴一下完整代码看看,感觉是死循环了

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

使用道具 举报

 楼主| 发表于 2020-9-17 19:33:16 | 显示全部楼层
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node{
                int data;
                Node *next;
                };

Node Head;       //头结点
Node *DLList;    //头指针
void init(Node *DLList);
void display(Node *DLList);
void insert(Node *DLList1,int,int);
void search(Node *DLList1,int);
void del(Node *DLList1,int);

int main()
{
        char choice;
        int wz,key;
    key=0;
        DLList=&Head;     //使头指针指向头结点
        Head.next=NULL;

        while (1)
        {
                system("cls");
   
                printf("\n\n\n\n");                             
                printf("\t\t              单链表操作  \n");
                printf("\t\t======================================");
                printf("\n\n");
                printf("\t\t             1:初始化      \n");
                printf("\t\t             2:显示        \n");
                printf("\t\t             3:单个插入    \n");
                printf("\t\t             4:查找        \n");
                printf("\t\t             5:删除        \n");
                printf("\t\t             0:退出        \n");
                printf("\n");
                printf("\t\t请选择:" );

                choice = getch();
                system("cls");
               
                switch(choice)
                {
                        case '1':
                            //调用实现函数
                            init(DLList);
                                break;
                        case '2':
                            //调用实现函数
                            display(DLList);
                                break;
                        case '3':
                //位置wz,需插入的数key
                       
                                break;
                        case '4':
                                //请输入需要查找的数key
                            //调用实现函数
                                break;
                        case '5':
                                //请输入需要删除的数
                            //调用实现函数
                                break;
                        case '0':
                                exit(0);
                }
        }
        return 0;
}

//公用的等待函数
void wait()
{
        printf("\n\n请按任意键继续\n");
        getch();
}

void init(Node *DLList)
{
        int length;
        Node *p,*q;
        while (1)
        {
                printf("输入元素个数(0- 10000 ):\n");
                scanf("%d",&length);
                if (length >= 0 && length <= 10000)
                        break;
                printf("\n");
        }

    int i;
        while (1)
        {
                printf("输入随机数种子(0-32767):\n");
                scanf("%d",&i);
                if (i >= 0 && i <= 32767)
                        break;
                printf("\n");
        }

    //从线性表中删除并释放原有的结点,使其成为空表
        p=DLList;
        while (p->next!=NULL)
        {
                q=p->next;
                p->next=q->next;
                free(q);
        }

        srand(i);  //指定随机数种子,相同的种子将产生相同的数据序列
        rand();  

        //向线性表插入length个新结点
    for (int j=1;j<=length;j++)   
    {
                p=new Node;
                p->next=DLList->next;
                DLList->next=p;
                p->data=rand() % 10000;
    }
}

void display(Node *DLList)
{
        Node *p;
        int count=0;
        p=DLList;
        p=p->next;            //头结点未初始化,值自动为0,需要跳过头结点
       
        while(p)
        {
                printf("%d ",p->data);
                p=p->next;
                count++;
        }
       
        printf("\n");
        printf("元素个数:%d",count);
       
        wait();
}


//4.屏幕提示后,在以DLList1为头指针的线性表中搜索这个元素key,并给出相应的位置或不存在
void search(Node *DLList1,int key)
{
        Node *p;
        int i=0;
        int flag=1;
       
        p=DLList;
       
        while(p&&)
        {
                p=p->next;
                i++;
                if(p->data == key)
                {
                        flag=0;
                        break;
                }
        }
       
        if(flag)
        {
                printf("元素不存在!");
                wait();
                return;
        }
        else{
                printf("元素所在位置为:%d",i);
                wait();
        }
       
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-17 19:53:37 | 显示全部楼层
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <process.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. typedef struct Node {
  8.         int data;
  9.         Node *next;
  10. }Node;

  11. Node Head;       //头结点
  12. Node *DLList;    //头指针
  13. void init(Node *DLList);
  14. void display(Node *DLList);
  15. void insert(Node *DLList1, int, int);
  16. void search(Node *DLList1, int);
  17. void del(Node *DLList1, int);

  18. int main()
  19. {
  20.         char choice;
  21.         int wz, key;
  22.         key = 0;
  23.         DLList = &Head;     //使头指针指向头结点
  24.         Head.next = NULL;

  25.         while (1)
  26.         {
  27.                 system("cls");

  28.                 printf("\n\n\n\n");
  29.                 printf("\t\t              单链表操作  \n");
  30.                 printf("\t\t======================================");
  31.                 printf("\n\n");
  32.                 printf("\t\t             1:初始化      \n");
  33.                 printf("\t\t             2:显示        \n");
  34.                 printf("\t\t             3:单个插入    \n");
  35.                 printf("\t\t             4:查找        \n");
  36.                 printf("\t\t             5:删除        \n");
  37.                 printf("\t\t             0:退出        \n");
  38.                 printf("\n");
  39.                 printf("\t\t请选择:");

  40.                 choice = getchar();
  41.                 system("cls");

  42.                 switch (choice)
  43.                 {
  44.                 case '1':
  45.                         //调用实现函数
  46.                         init(DLList);
  47.                         break;
  48.                 case '2':
  49.                         //调用实现函数
  50.                         display(DLList);
  51.                         break;
  52.                 case '3':
  53.                         //位置wz,需插入的数key

  54.                         break;
  55.                 case '4':
  56.                         //请输入需要查找的数key
  57.                 {
  58.                         int c = 0;
  59.                         printf("请插入需要查找的数字: ");
  60.                         scanf_s("%d", &c);
  61.                         search(DLList, c);
  62.                         break;
  63.                 }
  64.                 //调用实现函数
  65.                         break;
  66.                 case '5':
  67.                         //请输入需要删除的数
  68.                 //调用实现函数
  69.                         break;
  70.                 case '0':
  71.                         exit(0);
  72.                 }
  73.         }
  74.         return 0;
  75. }

  76. //公用的等待函数
  77. void wait()
  78. {
  79.         printf("\n\n请按任意键继续\n");
  80.         getchar();
  81.         getchar();
  82. }

  83. void init(Node *DLList)
  84. {
  85.         int length;
  86.         Node *p, *q;
  87.         while (1)
  88.         {
  89.                 printf("输入元素个数(0- 10000 ):\n");
  90.                 scanf_s("%d", &length);
  91.                 if (length >= 0 && length <= 10000)
  92.                         break;
  93.                 printf("\n");
  94.         }

  95.         int i;
  96.         while (1)
  97.         {
  98.                 printf("输入随机数种子(0-32767):\n");
  99.                 scanf_s("%d", &i);
  100.                 if (i >= 0 && i <= 32767)
  101.                         break;
  102.                 printf("\n");
  103.         }

  104.         //从线性表中删除并释放原有的结点,使其成为空表
  105.         p = DLList;
  106.         while (p->next != NULL)
  107.         {
  108.                 q = p->next;
  109.                 p->next = q->next;
  110.                 free(q);
  111.         }

  112.         srand(i);  //指定随机数种子,相同的种子将产生相同的数据序列
  113.         rand();

  114.         //向线性表插入length个新结点
  115.         for (int j = 1; j <= length; j++)
  116.         {
  117.                 p = new Node;
  118.                 p->next = DLList->next;
  119.                 DLList->next = p;
  120.                 p->data = rand() % 10000;
  121.         }
  122. }

  123. void display(Node *DLList)
  124. {
  125.         Node *p;
  126.         int count = 0;
  127.         p = DLList;
  128.         p = p->next;            //头结点未初始化,值自动为0,需要跳过头结点

  129.         while (p)
  130.         {
  131.                 printf("%d ", p->data);
  132.                 p = p->next;
  133.                 count++;
  134.         }

  135.         printf("\n");
  136.         printf("元素个数:%d", count);

  137.         wait();
  138. }


  139. //4.屏幕提示后,在以DLList1为头指针的线性表中搜索这个元素key,并给出相应的位置或不存在
  140. void search(Node *DLList1, int key)
  141. {
  142.         Node *p;
  143.         int i = 0;
  144.         int flag = 1;

  145.         p = DLList;

  146.         while (p != NULL)
  147.         {
  148.                 p = p->next;
  149.                 i++;
  150.                 if (p != NULL && p->data == key)
  151.                 {
  152.                         flag = 0;
  153.                         break;
  154.                 }
  155.         }

  156.         if (flag)
  157.         {
  158.                 printf("元素不存在!");
  159.                 wait();
  160.                 return;
  161.         }
  162.         else {
  163.                 printf("元素所在位置为:%d", i);
  164.                 wait();
  165.         }

  166. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-17 19:54:12 | 显示全部楼层
vs2017 调试通过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-17 20:09:19 | 显示全部楼层

对,调试可以通过,但是输一个链表里不存在的,无法正常运行出结果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-17 22:07:44 | 显示全部楼层    本楼为最佳答案   
Christopher. 发表于 2020-9-17 20:09
对,调试可以通过,但是输一个链表里不存在的,无法正常运行出结果

你把search那个函数的while循环条件改为 while(p->next) 就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-17 23:19:26 | 显示全部楼层
喝多了,不便回复,你们聊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-17 23:52:40 | 显示全部楼层
sunrise085 发表于 2020-9-17 22:07
你把search那个函数的while循环条件改为 while(p->next) 就可以了

对的,我后来改出来了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-18 15:09:52 | 显示全部楼层
Christopher. 发表于 2020-9-17 23:52
对的,我后来改出来了

没问题啊,我调试的就是不存在的一个值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-13 01:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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