Program to implement Linear Queues using Linked list using global front and rear pointers

Queues

Program to implement Linear Queues using Linked list using global front and rear pointers

/*Implementation of Linear Queues using Linked List
using global front and rear pointers*/

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

typedef struct queue
{int info;
struct queue *next;
}queue;

queue *front=NULL,*rear=NULL;

queue *create()
{queue *temp=(queue *)malloc(sizeof(queue));
if(temp==NULL)
{printf(“Memory Allocation Error!”);
exit(1);
}
return temp;
}

queue *makenode(int x)
{queue *temp=create();
temp->info=x;
temp->next=NULL;
return temp;
}

/*Enqueue operation references rear pointer
to insert a new node at the next address of rear*/

void enqueue(int x)
{
queue *temp=makenode(x);
if(front==NULL)
{front=temp;
rear=temp;
}
else/*rear pointer referred without traversing entire queue*/
{rear->next=temp;
rear=temp;
}
}
void dequeue()
{
queue *temp=front;
if(front==NULL)
{printf(“nQueue Underflow!”);
exit(1);
}
front=front->next;
free(temp);
}

void display()
{queue *temp=front;
while(temp!=NULL)
{printf(“%d->”,temp->info);
temp=temp->next;
}
printf(“bb “);
}

void main()
{
int num,ch;
while(1)
{
clrscr();
printf(“nMenu”);
printf(“nn1. Enqueue”);
printf(“n2. Dequeue”);
printf(“n3. Display”);
printf(“n4. Exit”);
printf(“nnEnter your choice?”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“nEnter an element?”);
scanf(“%d”,&num);
enqueue(num);
break;
case 2: printf(“Removing front element from the queue…!”);
dequeue();
break;
case 3: display();
break;
case 4: exit(0);
default:printf(“nInvalid choice…!”);
}
getch();
}
}