【问题标题】:How to display a queue in C with Linked list如何在 C 中使用链表显示队列
【发布时间】:2019-05-20 16:46:32
【问题描述】:

我想用链表实现显示队列的当前状态,printqueue 尝试这样做。

我上面所说的printqueue 试图做到这一点,但我无法编辑它的内部代码或任何函数或结构。所以我唯一的解决方案是改变我调用它的方式(使用不同的参数初始化)。当我使用空队列运行此代码时,我会出现段错误或非空队列我什么都没有。


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PLATE_LENGTH 9

struct node
{
    char data[PLATE_LENGTH];
    struct node *next;
};
typedef struct node *PTR;

void enqueue(char obj[],PTR *pf,PTR *pr)
{
    PTR newnode;
    newnode=(PTR*)malloc(sizeof(PTR)); 
    assert(newnode!=NULL);
    strcpy(newnode->data,obj);
    newnode->next=NULL;
    if((*pf)==NULL)
    {
        *pf=newnode;
        *pr=newnode;
    }
    else
    {
        (*pr)->next=newnode;
        *pr=newnode;
    }

    printf("Insertion Completed!\n");

}


void dequeue(PTR *pf,PTR *pr)
{
    PTR p;
    if((*pf)==NULL)
        printf("\nQueue empty. No elements to delete.\n");
    else
    {
        p=*pf;
        *pf=(*pf)->next;
        if((*pf)==NULL) *pr=*pf;
        printf("%s has been deleted...\n",p->data);
        free(p);
    }
}

void printqueue(PTR p,PTR pr)
{

    while(p!=NULL)
    {
        printf("\n\t\t%s",p->data);
        p=p->next;
    }
}


int edisplay_menu()
{
    int input=0;
    printf("MENU\n======\n1.Car Arrival\n2.Car Departure\n3.Queue State\n0.Exit\n");
    printf("Choice?");
    scanf("%d",&input);
    return input;

}


PTR *pf=NULL,*pr=NULL;


int main(int argc, char** argv) 
{
    int input=1;
    char *plate=(char*) malloc(PLATE_LENGTH*sizeof(char));
    PTR front,rear;

    if (plate==NULL)
    {
         printf("Out of memory!\n");
         return (1);
    }

   input=edisplay_menu();
   while(input!=0)
   {
        if(input==1) //in case of car arrival
        {
            printf("Give the car's plate:");
            scanf("%s",plate);
            enqueue(plate,&pf,&pr);  //insert car plate to queue
        }
        if(input==2) //in case of departure
        {
            dequeue(&pf,&pr); //delete car plate from queue
        }
        if(input==3)
        {
         front=*pf;
         printqueue(front,rear); //display all car plates in queue
        }
        if(input==0)
        {
            printf("Bye!!!");
            exit(1);
        }

   input=edisplay_menu();

   }

    return (EXIT_SUCCESS);
}

【问题讨论】:

    标签: c queue


    【解决方案1】:

    您似乎对要使用的变量感到困惑。

    你有全局变量:

    PTR *pf=NULL,*pr=NULL;
    

    main你有:

    PTR front,rear;
    

    然后使用pfpr 进行排队,使用frontrear 进行打印。

    解决方案:去掉全局变量(注意目前它们也有错误的类型)

    顺便说一句:frontrear 在您调用打印函数时未初始化,这可能会导致崩溃。

    【讨论】:

    • 你的意思是把全局变量放在 main 里面还是删除它们?
    • @JHk1821 我的意思是:删除全局变量当您将指针作为函数参数传递时,它们不是必需的。并且记得在main中初始化front和rear
    • 但在enqueuedequeue 中,我都在使用pfprfrontrear,它只是PTR 的类型,其他的是struct PTR
    • 我很困惑enqueue 只出现为struct node *PTR)分配了足够的空间,而不是struct node。当然newnode=(PTR*)malloc(sizeof(PTR)); 应该是newnode=(PTR)malloc(sizeof(*newnode));
    • @IanAbbott 不错——我没注意到。正确的分配是:newnode=malloc(sizeof *newnode); 这是“不要 typedef 指针”的一个很好的例子 ;-)
    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2018-05-16
    相关资源
    最近更新 更多