Google

Program to implement Linear Queues using Linked List

Written on:October 2, 2012
Comments are closed

Queues

Program to implement Linear Queues using Linked List

/*Implementation of Linear Queues using Linked List*/

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

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

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 requires traversing the entire
queue for inserting a new node.*/

queue *enqueue(queue *front,int x)
{
queue *temp,*ptr=makenode(x);
if(front==NULL)
{front=ptr;
return front;
}
/*entire queue is traversed to insert at end*/
for(temp=front;temp->next!=NULL;temp=temp->next);
temp->next=ptr;
return front;
}

queue *dequeue(queue *front)
{
queue *temp=front;
if(front==NULL)
{printf(“nQueue Underflow!”);
exit(1);
}
/*the following assignment directly operates upon front
pointer by making it point to the next node and deleting
the first node*/
front=front->next;
free(temp);
return front;
}

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

void main()
{
queue *front=NULL,*rear=NULL;
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);
front = enqueue(front,num);
break;
case 2: printf(“Removing front element from the queue…!”);
front=dequeue(front);
break;
case 3: display(front);
break;
case 4: exit(0);
default:printf(“nInvalid choice…!”);
}
getch();
}
}

 

Sorry, the comment form is closed at this time.