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;
}