Whenever I try to run this code in turbo c++ 4.5 I recieve an error "General Protection Exception List.c 60 List(2) 0x24DF:0x0157 processor Fault" whereas when the same Program is run in Turbo c++ 3 Dos version It Compiles and Runs Propely without any error. Please Help.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct queue
{
    struct node *front;
    struct node * rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue *, int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
void main()
{
    int val, option;
    create_queue(q);
    clrscr();
    do
    {
        printf("\n****Main Menu****");
        printf("\n 1. INSERT");
        printf("\n 2. DELETE");
        printf("\n 3. PEEK");
        printf("\n 4. DISPLAY");
        printf("\n 5. EXIT\n");
        printf("******************");
        printf("\nEnter Your Option");
        scanf("%d", &option);
        switch(option)
        {
        case 1:
            printf("enter the number to be inserted into the queue");
            scanf("%d",&val);
            q=insert(q,val);
            break;
        case 2:
            q=delete_element(q);
            break;
        case 3:
            val=peek(q);
            printf("the value stored in the topmost position is:%d",val);
            break;
        case 4:
            q=display(q);
            break;
        }
    } while(option<5);
    getch();
}

void create_queue(struct queue *q)
{
    q->rear =NULL;
    q->front=NULL;
}
struct queue *insert(struct queue *q, int val)
{
    struct node *ptr;
    ptr=(struct node *) malloc(sizeof(struct node *));
    ptr->data =val;
    if( q->front==NULL)
    {
        q->front=ptr;
        q->rear=ptr;
        q->front->next=q->rear->next=NULL;
    }
    else
    {
        q->rear->next=ptr;
        q->rear=ptr;
        q->rear->next=NULL;
    }
    return q;
}
struct queue *display(struct queue *q)
{
    struct node *ptr;
    ptr=q->front;
    if(ptr==NULL)
    {
        printf("the queue is empty");
    }
    else
    {
        printf("\n");
        while(ptr!=q->rear)
        {
            printf("%d\t",ptr->data);
            ptr=ptr->next;
        }
        printf("%d\t",ptr->data);
    }
    return q;
}
struct queue *delete_element(struct queue *q)
{
    struct node *ptr;
    ptr=q->front;
    if(q->front==NULL)
        printf("\n UNDERFLOW");
    else
    {
        q->front=q->front->next;
        printf("\n the value being deleted is:%d", ptr->data);
        free(ptr);
    }
    return q;
}
int peek(struct queue *q)
{
    return q->front->data;
}

Recommended Answers

All 10 Replies

function create_queue() is attempting to dereference a NULL pointer. You don't need to call that function on line 23 because the pointer is already NULL.

But when i remove create_queue(q) function the program proceeds properly till it encounters the line 68 and displays the error message.

Same problem as I explained before. On line 68 what is the value of q ? Answer: NULL. You can't use a NULL pointer for anything because there's no memory assigned to it.

Also, in that same function, you need to pass q by address or by reference, not by value, so that the function can change the original pointer. You will have to make additional changes inside the insert() function.

struct queue *insert(struct queue** q, int val)

And change line 16 like this:

struct queue *insert(struct queue**, int);

line 41 change: (note: you don't need the q= assignment operator because insert() will do that.
insert(&q,val);

Now The error Is Change to "pointer to structure required on left side of ->or ->* in function"

By what method can I access front in the strcut queue."q->front" is not working at all.

This is my first time in creating this type of code.And am getting error that I have never seen before. Please Help

Can't help you until you post code.

Here is the code sir After making following changes Its is Generating an Error Message

struct queue *insert(struct queue** q, int val)

struct queue *insert(struct queue**, int);

insert(&q,val);

Error:
"Pointer to structure required on left side of -> or ->* in function Insert"
Warning:
Suspicious Pointer conversionin function insert.

code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct queue
{
    struct node *front;
    struct node * rear;
};
struct queue *q;
//void create_queue(struct queue *);
struct queue *insert(struct queue **, int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
void main()
{
    int val, option;
    //create_queue(q);
    clrscr();
    do
    {
        printf("\n****Main Menu****");
        printf("\n 1. INSERT");
        printf("\n 2. DELETE");
        printf("\n 3. PEEK");
        printf("\n 4. DISPLAY");
        printf("\n 5. EXIT\n");
        printf("******************");
        printf("\nEnter Your Option");
        scanf("%d", &option);
        switch(option)
        {
        case 1:
            printf("enter the number to be inserted into the queue");
            scanf("%d",&val);
            q=insert(&q,val);
            break;
        case 2:
            q=delete_element(q);
            break;
        case 3:
            val=peek(q);
            printf("the value stored in the topmost position is:%d",val);
            break;
        case 4:
            q=display(q);
            break;
        }
    } while(option<5);
    getch();
    }

  /*    void create_queue(struct queue *q)
    {
        q->rear =NULL;
        q->front=NULL;
    }
  */
    struct queue *insert(struct queue **q, int val)
    {
        struct node *ptr;
        ptr=(struct node *) malloc(sizeof(struct node *));
        ptr->data =val;
        if( q->front==NULL)
        {
            q->front=ptr;
            q->rear=ptr;
            q->front->next=q->rear->next=NULL;
        }
        else
        {
            q->rear->next=ptr;
            q->rear=ptr;
            q->rear->next=NULL;
        }
        return q;
    }
    struct queue *display(struct queue *q)
    {
        struct node *ptr;
        ptr=q->front;
        if(ptr==NULL)
        {
            printf("the queue is empty");
        }
        else
        {
            printf("\n");
            while(ptr!=q->rear)
            {
                printf("%d\t",ptr->data);
                ptr=ptr->next;
            }
            printf("%d\t",ptr->data);
        }
        return q;
    }
    struct queue *delete_element(struct queue *q)
    {
        struct node *ptr;
        ptr=q->front;
        if(q->front==NULL)
            printf("\n UNDERFLOW");
        else
        {
            q->front=q->front->next;
            printf("\n the value being deleted is:%d", ptr->data);
            free(ptr);
        }
        return q;
    }
    int peek(struct queue *q)
    {
        return q->front->data;
    }

line 67: it is not allocating enough memory -- need sizeof(struct node) instead of sizeof(struct node*) because it needs to allocate memory for the entire structure, not just a 4-byte pointer.

lines 69-79: q is now a ** pointer, and to reference an object of q is like this:
(*q)->front=ptr;

or like this:
*(*q).front=ptr;

And before using *q you need to check that it is not NULL, e.g.

if( *q == NULL)
{
    *q = ptr;
}
else
{
   // do stuff with *q here
}

Still the program Displays the same error
Error:
"Pointer to structure required on left side of -> or ->* in function Insert"
Warning:
Suspicious Pointer conversionin function insert.

After making the changes

struct queue *insert(struct queue **q, int val)
    {
        struct node *ptr;
        ptr=(struct node *) malloc(sizeof(struct node));
        ptr->data =val;
        if( *q->front==NULL)
        {
            *q = ptr;
        }
        else
        {
            *q->rear->next=ptr;
            *q->rear=ptr;
            *q->rear->next=NULL;
        }
        return q;
    }

you did it wrong. look again at the examples I gave you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.