Wednesday 10 October 2012

IMPLEMENTATION OF QUEUE USING LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*rear,*front;
void insert();
void del();
void show();
void main()
{
int ch;
while(1)
{
clrscr();
printf("1. Insert");
printf("\n\n2. Delete");
printf("\n\n3. Show");
printf("\n\n4. Exit");
printf("\n\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: del(); break;
case 3: show(); break;
case 4: exit(0); break;
default: printf("\n\n\n\tWrong choice..");
}
}
}
void insert()
{
struct node *temp;
clrscr();
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element: ");
scanf("%d",&temp->info);
if(rear==NULL)
{
temp->next=NULL;
rear=temp;
front=temp;
return;
}
else
{
rear->next=temp;
temp->next=NULL;
rear=temp;
return;
}
}
void show()
{
struct node *q;
clrscr();
q=front;
if(q==NULL)
{
printf("List empty");
getch();
return;
}
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
getch();
}
void del()
{
struct node *q;
clrscr();
if(rear==NULL)
{
printf("QUEUE EMPTY");
getch();
return;
}
else if(front==rear)
{
front=rear=NULL;

}
else
{
q=front;
front=front->next;
free(q);
}
printf("item deleted..");
getch();
}

No comments: