Thursday, December 18, 2014

Queue Operation in c

/* save as QUEUE.h  and include it in  main program */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
} ;
void insert(struct node **front,struct node **rear,int item)
{
struct node *tmp;
struct node *rr=*rear;
struct node *ff=*front;
tmp=(struct node*)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Memory  not avaliable\n");
return ;
}
if(*front==NULL)
{
tmp->info=item;
tmp->link=NULL;
*front=tmp;
*rear=tmp;
}
else
{
while(rr->link!=NULL)
{
(rr)=(rr)->link;
}
tmp->info=item;
tmp->link=NULL;
(rr)->link=tmp;
}

}
int del(struct node **front)
{
struct node *tmp;
int item;
if(*front==NULL)
{
printf("Queue underflow\n");
exit(1);
}
tmp=*front;
item=tmp->info;
*front=(*front)->link;
free(tmp);
return item;
}
int peek(struct node *front)
{
struct node *tmp;
int item;
if(front==NULL)
{
printf("underflow\n");
exit(1);
}
return (front)->info;
}
int isEmpty(struct node *front)
{
if(front==NULL)
{
return 1;
}
else
{
return 0;
}
}
void display(struct node *front)
{
struct node *ptr;
ptr=front;
if(front==NULL)
{
printf("Queue is Empty\n");
return ;
}
printf("Queue elements :\n\n");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->link;
}
}