EmbLogic's Blog

Implementation of Circular Queue using linked list.

HEADER

***************************************************************

#include<stdio.h>
#include<stdlib.h>
struct cq
{
int info;
struct cq *next;
};
int getchoice();
int operation(int, struct cq **, struct cq **, struct cq **);
int enqueue( struct cq **, struct cq **, struct cq **);
int display( struct cq **, struct cq **, struct cq **);
int dequeue( struct cq **, struct cq **, struct cq **);

 

 

MAIN

***************************************************************

#include”header.h”
int main()
{
struct cq *front = NULL , *rear = NULL, *start = NULL;
int choice;
while(1)
{
choice = getchoice();
operation(choice,&front,&rear,&start);
}
}

 

DEFINITIONS

***********************************************************************

#include”header.h”
int flag = 0,count = -1;
int getchoice()
{
int choice;
printf(”                   1.create\n”);
printf(”                   2.enqueue\n”);
printf(”                   3.dequeue\n”);
printf(”                   4.display\n”);
printf(”                   5.exit\n”);
printf(”                Enter your choice\n”);
scanf(“%d”,&choice);
return choice;
}
int operation(int choice, struct cq **front, struct cq **rear,struct cq **start)
{
int i,k;
switch(choice)
{
case 1:    if(flag == 0)
{
printf(“successfully created!!\n”);
flag = 1;
}
else
printf(“already created\n”);
break;
case 2:{
printf(“How many elments you want to enqueue?\n”);
scanf(“%d”,&k);
if(k > (10-(count+1)))
{
printf(“no sufficient space!! you can enter %d more elements\n”,10-(count+1));
break;
}
for(i = 0; i < k; i++)
{
enqueue(front,rear,start);
}
printf(“%d elements entered!!\n”,k);break;
}
case 3:{
printf(“How many elments you want to dequeue?\n”);
scanf(“%d”,&k);
if(k > (count+1))
{
printf(” you can dequeue %d elements only\n”,(count+1));
break;
}
for(i = 0; i < k; i++)
{
dequeue(front,rear,start);
}
printf(“%d elements dequeued!!\n”,k);break;
}
case 4:display(front,rear,start);break;
case 5:exit(0);
default:printf(“invalid choice\n”);
}
}
int dequeue(struct cq **front, struct cq **rear,struct cq **start)
{
struct cq *temp;
if(flag == 0)
{
printf(“c queue not created!!\n”);
return -1;;
}
if(count == -1)
{
printf(“c queue is empty(underflow)\n”);
return 0;
}
if(!(*front))
{
temp = *start;
*front = (*start)->next;
printf(“%d is dequeued\n”,temp->info);
free(temp);
*start = NULL;
count–;
}
else if(count == 0)
{
printf(“%d is dequeued\n”,(*front)->info);
*front = *rear = NULL;
count–;
}

else if(count > 0)
{
temp = *front;
*front = (*front)->next;
printf(“%d is dequeued\n”,temp->info);
count–;
}
//    printf(“count: %d\n”,count);
display(front,rear,start);

}
int enqueue(struct cq **front, struct cq **rear,struct cq **start)
{
struct cq *temp;
if(flag == 0)
{
printf(“queue not created!!\n”);
return -1;;
}
if(count >= 9)
{
printf(“c Queue is full!\n”);
return 0;
}
if(!(*rear))
{
*rear = malloc(sizeof(struct cq));
printf(“Enter the %d element\n”,count+1);
scanf(“%d”,&((*rear)->info));
(*rear)->next = NULL;
*start = *rear;
}
else if(rear)
{
temp = malloc(sizeof(struct cq));
(*rear)->next = temp;
*rear = (*rear)->next;
printf(“Enter the %d element\n”,count+1);
scanf(“%d”,&(temp->info));
(temp)->next = NULL;
}
count++;
//    printf(“count: %d\n”,count);
display(front,rear,start);
}
int display(struct cq **front, struct cq **rear,struct cq **start)
{
struct cq *temp;
if(flag == 0)
{
printf(“queue not created!!\n”);
return -1;;
}
if(!(*rear) && !(*front))
printf(“c queue is empty\n”);
if(!(*front) && *rear)
temp = *start;
else
temp = *front;
while(temp)
{
printf(“<-%d”,temp->info);
temp = temp->next;
}
printf(“\n”);
}

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>