Saturday, May 9, 2009

PROGRAM FOR IMPLEMENTING QUEUE USING LINKED LIST

#include
#include
#include
#define NULL 0
struct queue
{
int info;
struct queue *next;
};
typedef struct queue node;
node *first,*list,*front,*rear;
void add();
void del();
void print();
void main()
{
int ch;
front=rear=NULL;
do
{
printf("\n 1->add");
printf("\n 2->del");
printf("\n 3->exit");
printf("\n Enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
print();
break;
case 2: del();
print();
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
}while(ch!=3);
}
void add()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=NULL;
if(rear==NULL)
{
rear=first;
front=first;
}
else
rear->next=first;
rear=first;
}

void del()
{
if(front==NULL)
{
printf("\nList is empty\n");
getch();
}
else
{
printf("The popped element is %d \n ",front->info);
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->next;
}
}

void print()
{
list=front;
if(list==NULL)
{
printf("\nList is empty\n");
}
else
{
printf("\nfront->");
while(list!=rear)
{
printf("%d->",list->info);
list=list->next;
}
printf("%d->",list->info);
printf("rear\n");
}
}

No comments:

Post a Comment