季小寞 2022-10-09 12:29 采纳率: 75%
浏览 10
已结题

请问一下,遍历完链表程序就退出了这是为什么

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

typedef struct Lnode {
char name[65];
char loacation[41];
double price;
Lnode* next;
}Lnode;

void menu(void);
Lnode* creatlist(void);
void addnode(Lnode * head);
void shownode(Lnode* head);

int main(int argc, char* argv[]) {
printf("Welcome to use The Tourist Attraction information system by OldWifi\n");
Lnode* Head = creatlist();
char choose;
do {
menu();
scanf(" %c", &choose);
switch (choose)
{
case '1' :
addnode(Head);
break;
case '2':
shownode(Head);
break;
case '3':
printf("3 Success!\n");
break;
case '4':
printf("4 Success!\n");
break;
case '5':
printf("5 Success!\n");
break;
case '0':
printf("0 Success!\n");
break;

    default:
        printf("Input error\n");
        break;
    }

} while (choose!= '0');
return 0;

}

void menu(void) {

printf("Enter a number to perform the opeartion\n");
printf("1.Input information\n");
printf("2.Show all information\n");
printf("3.Lookup information\n");
printf("4.Modify information\n");
printf("5.Delete information\n");
printf("0.Quit\n");

}

Lnode* creatlist(void) {
Lnode* head = (Lnode*)malloc(sizeof(Lnode));
if (!head) {
printf("Error ! no data\n");
return NULL;
}
else {
head->next = NULL;
printf("Data had loaded successfully!\n");
return head;
}

}

void addnode(Lnode* head) {
Lnode* spot = (Lnode*)malloc(sizeof(Lnode));
if (!spot) {
printf("insert error!");

}

printf("Enter a name of scenic spot : ");
scanf("%s", spot->name);
printf("Enter the location of scenic spot:  ");
scanf("%s", spot->loacation);
printf("Enter the price for scenic spot:  ");
scanf("%lf", &spot->price);
spot->next = head->next;
head->next = spot;

printf("%s  Add successfully!\n", spot->name);

}

void shownode(Lnode* head) {
printf("Name Location Price\n");
Lnode* Pnode = head->next;
while (Pnode!=head) {
printf("%s %s %f\n", Pnode->name, Pnode->loacation, Pnode->price);
Pnode = Pnode->next;
}
}

img

img

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-10-09 12:50
    关注

    程序崩溃了,肯定没遍历完遇到异常节点了
    while (Pnode!=head) {
    改成
    while (Pnode!=NULL) {
    你又不是循环链表,怎么判断不等于head呢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月19日
  • 已采纳回答 10月11日
  • 创建了问题 10月9日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)