Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查c代码链表中的可用座位_C_Singly Linked List - Fatal编程技术网

如何检查c代码链表中的可用座位

如何检查c代码链表中的可用座位,c,singly-linked-list,C,Singly Linked List,我想在链表中显示座位的可用性,但不知何故,chekcavability()中的逻辑不起作用 第一个输入是正常的,第二个输入是当我输入相同的值时,它显示座椅已被占用,并返回到特定的功能 但当我再次输入可用的不同值时,它会给我一个错误 请帮忙,谢谢 #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define A 20 struct passeng

我想在链表中显示座位的可用性,但不知何故,
chekcavability()
中的逻辑不起作用

第一个输入是正常的,第二个输入是当我输入相同的值时,它显示座椅已被占用,并返回到特定的功能

但当我再次输入可用的不同值时,它会给我一个错误

请帮忙,谢谢

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

#define A 20

struct passenger
{
char name[20];
char booking_id[20];
char passno[20];
char seatno[20];
struct passenger *next;
}*start,*curr;



void datainput(), savefile(), loadfile(), checkavailability(),   validationseat();

int main()
{
void reserve_seat(), cancel_seat(), modify_seat(), display_layout(), exit();
int choice;
start=curr=NULL;


do
{

    system("cls");
    printf("\n\n*************************************************");
    printf("\n\n********AIRLINE RESERVATION MENU*****************");
    printf("\n\n*************************************************");
    printf("\n\n\t\t* 1. Reserve seat       *");
    printf("\n\n\t\t* 2. Modify seat        *");
    printf("\n\n\t\t* 3. Cancel seat        *");
    printf("\n\n\t\t* 4. Display seat layout*");
    printf("\n\n\t\t* 5. Exit               *");
    printf("\n\n*************************************************");
    printf("\n\n\n\n\t\t Enter your choice: ");
    scanf("%d",&choice);fflush(stdin);
    switch (choice)
    {
    case 1:
        reserve_seat();
        break;

    case 2:
        modify_seat();
        break;

    case 3:
        cancel_seat();
        break;

    case 4:
        display_layout();
        break;
    case 5:
        {
        exit();
        break;
        }
    default:
        printf("invalid choice!!, please try again");
    }
    getch();

}while (choice != 5);

}

void datainput()
{
printf("\n\t\t enter your booking ID: ");
gets(curr->booking_id); fflush(stdin);
printf("\n\t\t enter your seat number: ");
gets(curr->seatno); fflush(stdin);
printf("\n\t\t Enter Name: ");
gets(curr->name); fflush(stdin);
printf("\n\t\t Enter Passport Number: ");
gets(curr->passno); fflush(stdin);

}

void reserve_seat()
{
curr=start;
checkavailability();
if(start==NULL)
    {
        start=curr=(struct passenger *)malloc(sizeof(struct passenger));
        datainput();
        curr->next=NULL;
        printf("\n\t data has been recorded");
        return;
    }
    while(curr->next=NULL)
        curr=curr->next;
    curr->next=(struct passenger *)malloc(sizeof(struct passenger));
    curr=curr->next;
    datainput();
    curr->next=NULL;
    printf("\n\t data has been recorded");

void checkavailability()
{
int i;
char cmp3[20];
printf("select your seat(1-20)");
gets(cmp3);fflush(stdin);
while(curr)
{
    if (strcmp(curr->seatno, cmp3)==0)
    {
        printf("Seat has been taken\n");
        checkavailability();
    }
    else
    {
        break;
    }
}
printf("seat available");
return;
}
#包括
#包括
#包括
#包括
#定义一个20
结构乘客
{
字符名[20];
char booking_id[20];
charpassno[20];
char-seatno[20];
结构乘客*next;
}*开始,*当前;
void datainput()、savefile()、loadfile()、checkavailability()、validationseat();
int main()
{
作废预留座位()、取消座位()、修改座位()、显示布局()、退出();
智力选择;
start=curr=NULL;
做
{
系统(“cls”);
printf(“\n\n****************************************************************”);
printf(“\n\n**********航空公司预订菜单*******************”);
printf(“\n\n****************************************************************”);
printf(“\n\n\t\t*1.预留座位*”;
printf(“\n\n\t\t*2.修改座位*”;
printf(“\n\n\t\t*3.取消座位*”;
printf(“\n\n\t\t*4.显示座椅布局*”;
printf(“\n\n\t\t*5.Exit*”;
printf(“\n\n****************************************************************”);
printf(“\n\n\n\n\t\t输入您的选择:”);
scanf(“%d”,&choice);fflush(标准输入法);
开关(选择)
{
案例1:
预留座位();
打破
案例2:
修改_seat();
打破
案例3:
取消座位();
打破
案例4:
显示_布局();
打破
案例5:
{
退出();
打破
}
违约:
printf(“无效选择!!,请重试”);
}
getch();
}while(选项!=5);
}
void datainput()
{
printf(“\n\t\t输入您的预订ID:”);
获取(curr->booking_id);fflush(stdin);
printf(“\n\t\t输入您的座位号:”);
获取(curr->seatno);fflush(stdin);
printf(“\n\t\t输入名称:”);
获取(curr->name);fflush(stdin);
printf(“\n\t\t输入护照号码:”);
获取(curr->passno);fflush(stdin);
}
空座
{
curr=开始;
检查可用性();
if(start==NULL)
{
start=curr=(结构乘客*)malloc(尺寸(结构乘客));
数据输入();
当前->下一步=空;
printf(“\n\t数据已记录”);
返回;
}
while(curr->next=NULL)
当前=当前->下一步;
curr->next=(结构乘客*)malloc(结构乘客的大小);
当前=当前->下一步;
数据输入();
当前->下一步=空;
printf(“\n\t数据已记录”);
void checkavailability()
{
int i;
char-cmp3[20];
printf(“选择您的座位(1-20)”;
获取(cmp3);fflush(stdin);
while(curr)
{
如果(strcmp(当前->座位号,cmp3)=0)
{
printf(“座位已被占用”);
检查可用性();
}
其他的
{
打破
}
}
printf(“座位可用”);
返回;
}

您的代码有很多错误,最糟糕的是他无法编译(除非conio声明一个没有参数的退出函数)

首先,您需要激活一个合适的编译器选项。至少-Wall-Wextra(取决于编译器)

在main函数中,这是:
exit();
退出函数需要一个参数。请修复此问题

modify_seat();
cancel_seat();
display_layout();
这些函数没有实现,所以不应该调用它们。也要解决这个问题

不要使用gets!它既危险又不安全。 不要乱动(stdin)!这是未定义的行为

“A”的定义是无用的,也不是真正明确的。它应该是什么?“数组”

为了您的安全,尽量不要使用全局变量。 我建议定义一个结构并在main中声明一个变量

在“预订座位”中: while(curr->next=NULL)

您在这里犯了一个错误:是while(curr->next!=NULL) 如果将NULL指定给curr->next,可能这就是链接列表被破坏的原因

对我来说,这是一个肮脏的代码,因为它严重缺乏严格的编码。 例如,检查函数调用是否失败(如malloc)。
我建议你重写代码。

慢慢编辑你的帖子,然后发布。好的。纠正汤姆指出的所有错误。然后发布我的错误行为,实际上我没有发送完整的代码,因为太长了,我只发送具体的问题,基本上我只需要检查可用性的逻辑非常感谢您的回答和建议是的,但为了我们的利益,请给我们一个MVC(最小、完整和可验证)例如,特别是因为您使用全局变量!坦率地说,您的代码就像用半自动手枪玩俄罗斯轮盘赌。我应该用完整的代码问同样的问题吗?顺便问一下,我应该把它放在哪里?在MVC中,有M(最小值),所以不,您不应该放完整的代码。