1

everyone. I'm new to C. My code for queue by C language is not working, but I cannot understand where lays the cause. I got a segmentation fault error. What is the problem? Anyone, please help me. My english is bad, sorry.

I refered to this page.
I added the result of debug, I'm thinking about it...

thanks

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

typedef struct element {
    char v;
    struct element *p;
} ELEM;

typedef struct {
    ELEM *front;
    ELEM *rear;
} queue;

queue *Q;
Q = (queue *)malloc(sizeof(queue));
Q->front = Q->rear = NULL;

int empty_q (){
    return (Q == NULL);
}

void enqueue(char x){
    ELEM *e;
    e = (ELEM *)malloc(sizeof(ELEM));
    if (e != NULL) {
        e->v = x;
        if (Q->front == NULL)
            Q->front = e;
        if (Q->rear != NULL)
            Q->rear->p = e;
        Q->rear = e;
        Q->rear->p = NULL;
    }
    return;
}

char dequeue(){
    char r = 0;
    if(!empty_q()){
        ELEM *e;
        e = Q->front;
        r = e->v;
        Q->front = Q->front->p;
        free(e);
    }
    return r;
}

int main(void){
    enqueue('a');
    enqueue('b');
    enqueue('c');
    while (!empty_q()) {
        printf("%c \n", dequeue());
    }
    free(Q);
    return 0;
}

Error

aa.c:20:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
Q = (queue *)malloc(sizeof(queue));
^
aa.c:20:1: error: redefinition of 'Q' with a different type: 'int' vs 'queue *'
aa.c:15:8: note: previous definition is here
queue *Q;
       ^
aa.c:21:1: error: unknown type name 'Q'
Q->front = Q->rear = NULL;
^
aa.c:21:2: error: expected identifier or '('
Q->front = Q->rear = NULL;
 ^
1 warning and 3 errors generated.
4
  • did you try a debugger. you will see where the failure is immediately. you cannot learn to program without using the debugger. Commented May 7, 2020 at 15:47
  • thanks for your advice. I tried a debugger now, and I added the error. Commented May 7, 2020 at 15:59
  • You need to move the lines Q = (queue *)malloc(sizeof(queue)); Q->front = Q->rear = NULL; in main(). Commented May 7, 2020 at 16:06
  • thanks Jean-Marc, I did it and got "abc" with Segmentation fault: 11. Commented May 7, 2020 at 16:10

2 Answers 2

1

You code does not compile but I suspect your error is here: Code crashes here when all chained items have been dequeed

char dequeue(){
    char r = 0;
    if(!empty_q()){
        ELEM *e;
        e = Q->front; // e is null after a,b,c have been removed
        r = e->v; // crash
        Q->front = Q->front->p;
        free(e);
    }
    return r;
}

Proposed solution:

char dequeue(){
    ELEM *e =  Q->front;
    if (e == NULL) return 0;
    char r = e->v;
    Q->front = Q->front->p;
    free(e);
    return r;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help. I confirmed your solution works. But I have a question, why did I need to move the lines Q = (queue *)malloc(sizeof(queue)); Q->front = Q->rear = NULL; in main()?
because you cannot execute code outside of a function. It needs to be either in main or in a dedicated function
1

To solve your segmentation fault after the output, you need to use Q->front when you check if the queue is empty:

int empty_q (){
    return (Q->front == NULL); 
}

1 Comment

I really appreciate your advice, and I confirmed it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.