Skip to main content
Circular Queue to implement Enqueue,Dequeue & Traversal using Linked list in C program.
CODING:
#include<stdio.h>
#include<conio.h>
struct queue
{
 struct node *front;
 struct node *rear;
};
struct node
{
 int data;
 struct node *next;
};
struct queue *q;
void   create_que(struct queue *);
struct queue *enqueue(struct queue *,int);
struct queue *dequeue(struct queue *);
struct queue *disp(struct queue *);
int peek(struct queue *);
void main()
{
int num,ch;
clrscr();
create_que(q);
do
{
 printf("\n\n<---MAIN MENU--->");
 printf("\n1.INSERT");
 printf("\n2.DELETE");
 printf("\n3.PEEK");
 printf("\n4.DISPLAY");
 printf("\n5.EXIT");
 printf("\nEnter The choice:");
 scanf("%d",&ch);
 switch(ch)
  {
  case 1:
  printf("ENTER THE NUMBER:");
  scanf("%d",&num);
  q=enqueue(q,num);
  break;
  case 2:
  q=dequeue(q);
  break;
  case 3:
  num=peek(q);
  printf("->TOP OF THE QUEUE %d STORED...",num);
  break;
  case 4:
  q=disp(q);
  break;
  case 5:
  printf("->PRESS ENTER FOR EXIT...");
  break;
  default:
  printf("->INVALID ENTER THE NUMBER,TRY AGAIN");
  }
}while(ch!=5);
getch();
}
void create_que(struct queue *q)
{
 q->front=NULL;
 q->rear=NULL;
}
struct queue *enqueue(struct queue *q,int num)
{
struct node *new_node;
new_node=(struct node *)malloc(sizeof(struct node *));
new_node->data=num;
if(q->front==NULL)
 {
  q->front=q->rear=new_node;
  new_node->next=NULL;
 }
 else
 {
  q->rear->next=new_node;
  q->rear=new_node;
  q->rear->next=NULL;
 }
 return q;
}
struct queue *disp(struct queue *q)
{
struct node *new_node;
new_node=q->front;
if(new_node==NULL)
  printf("->QUEUE IS EMPTY...");
 else
 {
  while(new_node!=NULL)
   {
   printf("%d",new_node->data);
   new_node=new_node->next;
   }
 }
 return q;
}
struct queue *dequeue(struct queue *q)
{
struct node *new_node;
new_node=q->front;
if(q->front==NULL)
  printf("->QUEUE IS UNDERFLOW...");
 else
 {
  q->front=q->front->next;
  printf("->THE DATA %d IS DELETED...",new_node->data);
  free(new_node);
 }
 return q;
}
int peek(struct queue *q)
{
return q->front->data;
}

Comments