Why does the program keeps giving me segmentation fault when I'm trying to make a user defined queue?

Harshit Singh

Please help me find the mistake.

I was trying to make a circular queue in which the size of the queue as well as the elements inside of the queue are user defined. The code works fine when I am not using loop but as soon as I use the loop it starts showing segmentation fault. enter image description here

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

struct node{
    int data;
    struct node *next;
};
typedef struct node node;

typedef struct{
    node *head;
    node *tail;
} queue;

void createqueue(queue *q){
    q->head = NULL;
    q->tail = NULL;
    return;
}
int enqueue(queue *q, int value){
    node *newnode = malloc(sizeof(node));
    if (newnode==NULL) return 0;
    newnode->data = value;
    newnode->next = NULL;
    if(q->tail!=NULL){
        q->tail->next = newnode;
    }
    q->tail = newnode;
    q->tail->next = q->head;
    if(q->head == NULL){
        q->head=newnode;
    }
    return 1;
}

int dequeue(queue *q){
    if (q->head==NULL) return 0;
    q->head = q->head->next;
    q->tail->next = q->head;
    return 1;
}

int display(queue *q){
    node *printval;
    printval = q->head;
    if(printval==NULL) return 0;
    printf("\n");
    do
    {
        printf("%d-->",printval->data);
        printval = printval->next;
    } while (printval!=q->head);
    
}

int main(){
    queue *root;
    createqueue(root);
    printf("Enter the size of the queue");
    int size;
    scanf("%d",&size);
    for(int i=0;i<size;i++){
        int value;
        printf("Enter the value of the %d element",i);
        scanf("%d",&value);
        enqueue(root,value);
    }
    display(root);

}
SokolovVadim

In a function createqueue you use a pointer to an object for which memory was not allocated. You need to call malloc first and then set head and tail to a null.

queue* createQueue()
{
    queue* q = malloc(sizeof(queue));
    if (q != NULL) {
        q->head = NULL;
        q->tail = NULL;
    }
    return q;
}

// ...

int main()
{
    queue* q = createQueue();
    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is this program giving a segmentation fault?

Why is this giving me a segmentation fault?

Why does my program keeps giving me an ArrayIndexOutOfBoundsException even tough I haven't got one

Why is this giving me a segmentation fault in c?

Why It's Giving Me Segmentation Fault?

why does vfork() giving segmentation fault

Why does my program return "Segmentation fault (core dumped)" when I remove a conditional in "else if"?

Why is my hybrid (C++, asm) program giving me a segmentation fault?

Why do I get a segmentation fault when trying to write a graph?

Why am I getting a segmentation fault when trying to double iterate?

Why is it not giving segmentation fault?

i am trying to print words from a sentence in c++ but its giving me Segmentation Fault(core dump)

Why it gives me "segmentation fault" error when I change this code?

Why does this program cause a segmentation fault?

Why is this simple linked list program giving segmentation fault?

strcpy giving me segmentation fault

Why am I getting segmentation fault in this program?

Why this code is giving segmentation fault?

Why am I getting a segmentation fault error whenever I'm trying to run this LinkedList delete function?

Why I'm having a segmentation fault

My code to change the current directory in C++ keeps giving me the Segmentation Fault: 11 error

Segmentation Fault happening when trying to pass objects by reference in qt program

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

Why does this segmentation fault?

It keeps on giving me a name error when I try to make several branches

Why am i getting segmentation fault when trying IPC using shared memory in c linux

Why am I reaching a segmentation fault when trying to add vertices to the adjacency list?

Why do I get a segmentation fault when trying to compare character arrays?

Why I am getting segmentation fault when trying to run the find function even though accessing memory properly?