在C中使用链接列表时出现分段错误

4xrmg8kj  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(100)


我是c的新手。问题是使用Linkedlist实现一个Reastuarent管理软件。用户应该能够输入他们的订单并查看他们的订单。为快速交货支付额外金额的用户应该出现在订单列表的顶部。
我的代码在执行时没有显示错误,但在运行时,当我尝试输入第二个条目或尝试显示列表时,它显示分段错误。

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct items{
    char *item_name;
    float cost;
};
struct items array[] = {
    {"Pizza",49.9},{"Apples",22.0},{"Oranges",10.5},{"Grapes",3.5},{"Parotta",4.5}
};
struct bill_item{
    int bill_no;
    char* customer_name;
    int order_item_no;
    int quantity;
    float total;
    struct bill_item *next;
};
struct bill_item* head;
int count=0;

void insert_at_start(struct bill_item* node);
void insert_at_end(struct bill_item* node);
void book();
void display_list();

void main(){
    int choice;
    head = (struct bill_item*)malloc(sizeof(struct bill_item));
    head->next = NULL;
    while(choice!=3){
        printf("\n----------------------------------------------");
        printf("\n1.Book\t2.Check Orders\t3.Exit\nEnter option: ");
        scanf("%d",&choice);
        printf("\n------choice %d",choice);
        switch(choice){
            case 1: book();
                    printf("\nItem Purchased!!");
                    break;
            case 2: display_list();
                    break;
            case 3: break;
            default: printf("\nEnter the correct option");
                    break;
        }
    }
}

void insert_at_start(struct bill_item* node){
    if(head->next == NULL){
        head->next = node;
    }        
    else{
        node->next = head;
        head->next = node;
    }
}
void insert_at_end(struct bill_item* node){
    struct bill_item* ptr = head;
    if(head->next==NULL)
        head->next = node;
    else{
        while(ptr->next!=NULL){
            ptr = ptr->next;
        }
        ptr->next = node;
    }
}
void book(){
    char c;
    struct bill_item* node = (struct bill_item*)malloc(sizeof(struct bill_item));
    int i=0,choice;
    printf("\nMenu");
    printf("\n-----------------");
    for (i=0;i<5;i++){
        printf("\n%d %s : %.2f",i+1,array[i].item_name,array[i].cost);
    }
    printf("\nEnter choice: ");
    scanf("%d",&choice);
    printf("\nEnter quantity: ");
    scanf("%d",&node->quantity);
    node->next = NULL;
    count++;
    node->bill_no = count;
    node->total = array[choice-1].cost*node->quantity;
    fflush(stdin);
    printf("\nEnter customer name: ");
    fgets(node->customer_name,30,stdin);
    fflush(stdin);
    printf("\nPurchase amount: %.2f \nNeed fast delivery(Extra 100rs will be charges)(y/n)?",node->total);
    c = getc(stdin);
    fflush(stdin);
    if(c=='Y' || c=='y'){
        node->total +=100;
        printf("\nFast delivery applied\nTotal %.2f",node->total);
        insert_at_start(node);
    }
    else{
        printf("\nTotal: %.2f",node->total);
        insert_at_end(node);
    }
}
void display_list(){
    printf("\nBill No\t\tOrder Item\t\tCname\t\tQ\t\tTotal");
    struct bill_item* ptr = head;
    ptr = ptr->next;
    while(ptr->next!=NULL){
        printf("\n%d\t\t%d\t\t%s\t\t%d\t\t%.2f",ptr->bill_no,ptr->order_item_no,ptr->customer_name,ptr->quantity,ptr->total);
        ptr = ptr->next;
    }
}

我试着找出错误。但是无法指出任何错误。错误在函数**insert_at_end()insert_at_start()**的某处

biswetbf

biswetbf1#

display_list()中,如果head->nextNULL,你试图读取ptr-〉next而不先检查它是否为空:

ptr = ptr->next;
while(ptr->next != NULL) {
// ...
}

这将导致segfault,因为ptr为空。

相关问题