Thursday, 11 October 2012

Queue Using Linked List

/*Queue Using Linked List*/

#include<stdio.h>

#include<conio.h>

struct node

{

int info;

struct node* next;

}*front,*rear;

void enqueue(int elt);

int dequeue();

void display();

void main()

{

int ch,elt;

rear=NULL;

front=NULL;

clrscr();

while(1)

{

printf("\nEnter:\n1->Insert\n2->Delete\n3->Display\n4->Exit\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("Enter The Element Value\n");

scanf("%d",&elt);

enqueue(elt);

break;

case 2:

elt=dequeue();

printf("The deleted element = %d\n",elt);

break;

case 3:

display();

break;

default:

printf("~~~Exit~~~");

getch();

exit(0);

break;

}

}

}

void enqueue(int elt)

{

struct node *p;

p=(struct node*)malloc(sizeof(struct node));

p->info=elt;

p->next=NULL;

if(rear==NULL||front==NULL)

front=p;

else

rear->next=p;

rear=p;

}

int dequeue()

{

struct node *p;

int elt;

if(front==NULL||rear==NULL)

{

printf("\nUnder Flow");

getch();

exit(0);

}

else

{

p=front;

elt=p->info;

front=front->next;

free(p);

}

return(elt);

}

void display()

{

struct node *t;

t=front;

while(front==NULL||rear==NULL)

{

printf("\nQueue is empty");

getch();

exit(0);

}

while(t!=NULL)

{

printf("->%d",t->info);

t=t->next;

}

}

No comments:

Post a Comment