Pages

Monday, 8 November 2021

Program to illustrate Circular Queue using Arrays in c++

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define max 4

int i,n,cq[max],item,front=-1,rear=-1;
class circularqueue
{
public:
int add(void);
int del(void);
int display(void);
};

int circularqueue::add(void)
{
 if((front==0&&rear==max-1)||(front==rear+1))
 {
  cout<<"\ncircular queue overflow";
  return rear;
 }
 if(front==-1)
 {
  front=0;
  rear=0;
 }
 else
  if(rear==max-1)
  rear=0;
  else
  rear++;
 cout<<"\nenter item to be added :";
 cin>>item;
 cq[rear]=item;
 return rear;
}

int circularqueue::del(void)
{
 if(front==-1 && rear==-1)
 {
  cout<<"\nqueue is underflow";
  return rear;
 }
 cout<<"The item deleted is ";
 cout<<cq[front];
 cq[front]='*';
 if(front==rear)
 {
  front=-1;
  rear=-1;
 }
 else
  if(front==max-1)
  front=0;
  else
  front++;
return front;
}

int circularqueue::display(void)
{
 if(front==-1&&rear==-1)
 {
  cout<<"\ncircular queue is empty";
  return rear;
 }
 cout<<"\ncircular queue elements are:";
 for(i=0;i<=max-1;i++)
 {
  if(cq[i]=='*')
  {
  cout<<"\t";
  }
  else
  cout<<cq[i]<<"  ";
 }
 cout<<"\nPosition of FRONT:= "<<front<<" and REAR:="<<rear;
 return rear;
}

void main()
{
 circularqueue rkcq;
 int ch;
 clrscr();
 for(i=0;i<=max-1;i++)
 cq[i]='*';
 while(1)
 {
  cout<<"\n**MENU**";
  cout<<"\n1.Add";
  cout<<"\n2.Delete";
  cout<<"\n3.Display";
  cout<<"\n4.Exit";
  cout<<"\nEnter your choice:";
  cin>>ch;
  switch(ch)
  {
   case 1: rkcq.add(); break;
   case 2: rkcq.del(); break;
   case 3: rkcq.display(); break;
   case 4: exit(0); break;
   default: cout<<"entered wrong choice";break;
  }
 }
}


Output:

ANNIMATION LINK FOR CIRCUALR QUEUE
https://www.youtube.com/watch?v=-GxuQ-Y8sbA

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...