Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to create a circular queue

Posted By: Easy Tutor     Category: C++ Programming     Views: 28815

Write a program to create a circular queue

Code for Program to create a circular queue in C++ Programming

# include <iostream.h>
# include <process.h>
# include <conio.h>
int display_menu();
class circularqueue
{
  int arr[10];
  int front,rear;
  int size;
  public:
     circularqueue()
     {
      front=0;
      rear=0;
      size=10;
     }
     void display();
     void enqueue();
     void delete_element();
};

void circularqueue :: display()
{
 cout<<endl;
 if(front!=0 && rear!=0)
 {
        int i=front;
        cout<<"arr["<<i<<"] :"<<arr[i]<<endl;
        while(i!=rear)
        {
         i=(i % size)+1;
         cout<<"arr["<<i<<"] :"<<arr[i]<<endl;
        }

 }
 else
 {
    cout<<"Queue is empty"<<endl;
 }
 getch();
}

void circularqueue :: enqueue()
{
 cout<<endl;
 if(front==0 && rear==0)
 {
  cout<<"Enter Number to enqueue at Position arr["<<rear+1<<"] :";
  cin>>arr[1];
  rear=1;
  front=1;
 }
 else
 {
  int next=(rear % size)+1;






  if(next==front)
  {
   cout<<"Queue is Full ...";
   getch();
  }
  else
  {
   cout<<"Enter Number to enqueue at Position arr["<<next<<"] :";
   cin>>arr[next];
   rear=next;
  }
 }
}

void  circularqueue :: delete_element()
{
 cout<<endl;
 if(rear==0 && front==0)
 {
   cout<<"Queue is empty ...";
   getch();
   return;
 }


 if(rear==front)
 {
  rear=0;
  front=0;
 }
 else
 {
  front=(front % size)+1;
 }
}
void main()
{
 circularqueue cq1;
 while(1)
 {
  switch(display_menu())
  {
    case 1: cq1.enqueue();
        break;
    case 2: cq1.delete_element();
        break;
    case 3: cq1.display();
        break;
    case 4: exit(1);
  }
 }
}
int display_menu()
{
 int c;
 clrscr();
 cout<<endl;
 cout<<"| 1 | : Enqueue element"<<endl;
 cout<<"| 2 | : Delete element"<<endl;
 cout<<"| 3 | : Display"<<endl;
 cout<<"| 4 | : Exit"<<endl;
 cout<<"Enter your Choice :";
 cin>>c;
 return c;
}
  
Share: 


Didn't find what you were looking for? Find more on Program to create a circular queue Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to create a circular queue is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!