1

During compilation this code gives no error, but the code stops abruptly. According to me the problem is with the createq function where q->front=q->rear=NULL is declared. It does have to be initialised. Is anything wrong with that?

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
    struct node *next;
    int data;
};

struct queue
{
    struct node *front;
    struct node *rear;
};

struct queue *q;

void createq(struct queue *);
struct queue *insert(struct queue *);
struct queue *delete_q(struct queue *);
struct queue *display(struct queue *);

int main()
{
    int option;
    printf("\tMAIN MENU\n");
    printf("\n1. Create\n2. Display\n3. Insert\n4. Delete\n5. Exit\n");
    while(option!=5)
    {
        printf("\nEnter a choice:");
        scanf("%d",&option);
        switch(option)
        {
        case 1:
            createq(q);
            break;

        case 2:
            q=display(q);
            break;

        case 3:
            q=insert(q);
            break;

        case 4:
            q=delete_q(q);
            break;
        }
    }
    return 0;
}

void createq(struct queue *q)
{
    q->rear=NULL;
    q->front=NULL;
    printf("q intialized");
}

struct queue *insert(struct queue *q)
{
    struct node *newnode;
    int val;
    newnode=(struct node *)malloc(sizeof(struct node));
    printf("Enter the value to be inserted:");
    scanf("%d",&val);
    newnode->data=val;
    if(q->front==NULL)
    {
        q->front=newnode;
        q->rear=newnode;
        q->front->next=q->rear->next=NULL;
    }
    else
    {
        q->rear->next=newnode;
        q->rear=newnode;
        q->rear->next=NULL;
    }
    return q;
}

struct queue *delete_q(struct queue *q)
{
    struct node *ptr;
    if(q->front==NULL)
    {
        printf("Queue Empty\n");
    }
    else
    {
        ptr=q->front;
        q->front=q->front->next;
        printf("Element being deleted is %d\n",ptr->data);
        free(ptr);
    }
    return q;
}

struct queue *display(struct queue *q)
{
    struct node *ptr;
    ptr=q->front;
    if(q->front==NULL)
    printf("Queue Empty!!\n");
    else
    {
        while(ptr!=q->rear)
        {
            printf("%d\t",ptr->data);
            ptr=ptr->next;
        }
        printf("%d\t",ptr->data);
            printf("\n");
    }
    return q;
}
2
  • 2
    Where is the code "stopping"? Is there a segmentation fault? Is it just not running? Is there an error being displayed? Commented Oct 8, 2014 at 16:08
  • Yes it is a segmentation fault. Commented Oct 8, 2014 at 16:35

2 Answers 2

5

You declare a pointer to a queue structure in the following way:

struct queue *q;

Note that you do not allocate memory for the structure here. Next, in your main() function you call:

createq(q);

Then you access rear and front via q in the createq() function:

q->rear=NULL;
q->front=NULL;

This way you access memory that you did not allocate. You should put something like the following at the beginning of your main() function:

q = (struct queue *)malloc(sizeof(struct queue));

And do not forget to put free(q) at the end of your main() function to prevent a memory leak.

Sign up to request clarification or add additional context in comments.

Comments

1

You're passing pointer q of type struct queue * to the function. But you have not allocated memory for that pointer.

So you need to allocate memory to the pointer q and then pass to your functions. You need to allocate memory like this

q = (struct queue *)malloc(sizeof(struct queue));

then pass q to your functions.

Comments

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.