【问题标题】:Segmentation fault - Implementing Stack using Queues分段错误 - 使用队列实现堆栈
【发布时间】:2021-05-04 13:46:27
【问题描述】:

我正在使用两个队列实现一个堆栈。算法我比较熟悉,在push操作成本高的地方准备了如下代码:

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

struct queue_struct{
    int ele;
    struct queue_struct *next;
};

struct Queue{
    struct queue_struct *front, *rear;
};

struct Stack{
    struct Queue *q1, *q2;
};

void enqueue(struct Queue *q, int x){
    struct queue_struct *temp=malloc(sizeof(*temp));
    temp->ele=x;
    temp->next=NULL;
    if(q->rear!=NULL){
        q->rear->next=temp;
    }
    q->rear=temp;
    if(q->front==NULL)
        q->front=temp;
    //printf("The item %d has been enqueued into the queue\n", x);
}

void dequeue(struct Queue *q){
    struct queue_struct *temp=q->front;
    if(temp==NULL){
        //printf("The queue is already empty. No more elements can be removed!\n");
        return;
    }
    printf("The item %d has been popped from the stack\n", temp->ele);
    q->front=temp->next;
    if(q->rear==temp)
        q->rear=NULL;
    free(temp);
}

void push(struct Stack *s, int x){
    enqueue(s->q2, x);
    while(!(s->q1->front==NULL)){
        enqueue(s->q2, s->q1->front->ele);
        dequeue(s->q1);
    }
    struct Queue *q=s->q1;
    s->q1=s->q2;
    s->q2=q;
    printf("The item %d has been pushed into the stack\n", x);
}

void pop(struct Stack *s){
    if(s->q1->front==NULL){
        printf("The stack is already empty. No more elements can be removed!\n");
        return;
    }
    dequeue(s->q1);
}

void display(struct Stack *s){
    struct queue_struct *temp=s->q1->front;
    printf("The contents of the queue are:\n");
    if(temp==NULL){
        printf("Nothing to be shown, the queue is empty.\n");
        return;
    }
    for(int i=0;temp!=NULL;temp=temp->next){
        if(i){
            printf(" ------ \n");
        }
        printf("|  %d  |\n", temp->ele);
        i=1;
    }
}

int main()
{
    int choice, element;
    struct Stack *s=malloc(sizeof(*s));
    s->q1->front=s->q1->rear=NULL;
    s->q2->front=s->q2->rear=NULL;
    printf("LET'S START WITH AN EMPTY STACK\n\n");
    while(1){
        printf("MENU\n");
        printf("----\n");
        printf("\t1. Push an element\n");
        printf("\t2. Pop an element\n");
        printf("\t3. Display stack\n");
        printf("\t4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice){
            case 1: printf("Enter the element to be pushed: ");
                    scanf("%d", &element);
                    push(s, element);
                    break;
            case 2: pop(s);
                    break;
            case 3: display(s);
                    break;
            case 4: printf("Program terminated successfully!");
                    return 0;
            default: printf("Invalid input");
        }
    }
}

但是,我遇到以下行的分段错误:s->q1->front=s->q1->rear=NULL;。我不太确定它为什么会发生以及如何解决它。任何帮助表示赞赏。

【问题讨论】:

  • 看起来 q1 和 q2 的成员 Stack 在此时未初始化。
  • 你只分配了s,但是s-&gt;q1,但你正在尝试取消引用它
  • @FredLarson 但我在struct Stack 中提到它们是s 的一部分。这还不够吗?
  • s-&gt;q1 是一个指针。它会指向哪里?
  • @FredLarson 哦,现在我明白了,我也必须为 q1 和 q2 分配内存!谢谢

标签: c struct stack queue


【解决方案1】:

您通过使用通过malloc() 分配但未初始化的缓冲区中的值调用了未定义的行为。

您必须在取消引用之前初始化 s-&gt;q1 和 s-&gt;q2。

struct Stack *s=malloc(sizeof(*s));
if (s == NULL) return 1; /* check if allocation succeeded */
s->q1 = malloc(sizeof(*s->q1)); /* allocate for s->q1 */
s->q2 = malloc(sizeof(*s->q2)); /* allocate for s->q2 */
if (s->q1 == NULL || s->q2 == NULL) return 1; /* check if allocations succeeded */
s->q1->front=s->q1->rear=NULL;
s->q2->front=s->q2->rear=NULL;

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 2013-10-05
    • 2021-01-17
    • 2014-12-23
    • 2010-10-15
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多