EmbLogic's Blog

Circular queue using Link list

MAIN..

#include”header.h”

int main()
{
int choice,n=0;
struct node *start;
start=NULL;
printf(“\n ENTER THE SIZE OF CIRCULAR QUEUE\n”);
scanf(“%d”,&n);
while(1)
{
choice=getchoice();
operation(choice,&start,n);

}
return 0;
}

FUNCTION DEFINITIONS..

#include”header.h”
struct node *front,*rear=NULL;
struct node *temp =NULL;
int count=-1,flag=0,num=0;

int getchoice()
{
int choice;
printf(“\n1. CREATE “);
printf(“\n2. ENQUEUE”);
printf(“\n3. DEQUEUE “);
printf(“\n4. DISPLAY”);
printf(“\n0. EXIT\n”);
printf(“\n\n Enter your choice\n”);
scanf(” %d”,&choice);
return choice;
}
int operation(int choice ,struct node **start,int n)
{
switch(choice)
{
case 1 :  create(start);
break;
case 2 :  enqueue(start,n,choice);
break;
case 3 :  dequeue(start);
break;
case 4 :  display(start);
break;
case 0 : exit(0);
break;
default : printf(“\ninvalid operation”);
}
}

int create(struct node **start)
{
if(flag)
{
printf(“\n THE CIRCULAR QUEUE IS ALREADY CREATED!!”);
return -1;
}
if(!flag)
{
(*start)=NULL;
printf(“\n THE QUEUE CREATED SUCESSFULLY”);
flag=1;
}
}
int enqueue(struct node **start,int n,int choice)
{
if(flag)
{
if(num==0)
{
(*start)=(struct node *)malloc(sizeof(struct node));
printf(“\n ENTER THE VERY FIRST ELEMENT\n”);
scanf(“%d”,&((*start)->info));
(*start)->next=NULL;
rear=(*start);
front=(*start);
count++;
num=1;
return 0;
}
if(count==(n-1))
{
printf(“\n THE QUEUE IS FULL\n”);
return 0;
}
else
{
rear->next=malloc(sizeof(struct node));
rear=rear->next;
printf(“\n ENTER THE INFO : \n”);
scanf(“%d”,&(rear->info));
count++;
}
}
}

int dequeue(struct node **start)
{
if(flag)
{
if(front==rear)
{
if(count==(-1))
{
printf(“\n THE QUEUE IS EMPTY”);
return 0;
}
printf(“\n THE ELEMENT REMOVED IS %d”,front->info);
front=NULL;
rear=NULL;
num=0;
//    flag=0;
count–;

}

else if(front!=rear)
{
printf(“\n THE ELEMENT REMOVED IS %d”,front->info);
front = front->next;
count–;
}
}
if(!flag)
printf(“\nTHE QUEUE IS EMPTY….!!”);
}
int display(struct node **start)
{
if(flag)
{
if(front==NULL)
{
printf(“\n THE QUEUE IS EMPTY”);
return 0;
}
temp=front;
printf(“THE ELEMENTS ARE”);
while(temp!=rear)
{
printf(“–>  %d”,temp->info);
temp = temp->next;
}
if(temp==rear)
printf(“–> %d”,temp->info);
return 0;
}
if(!flag)
{    printf(“\n THE QUEUE IS EMPTY…!! NOTHING TO SHOW”);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>