Logo 
Search:

C++ Programming Articles

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

Program to create a queue using static memory allocation

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

Write a program create a queue using static memory allocation

Code for Program to create a queue using static memory allocation in C++ Programming

# include <iostream.h>
# include <conio.h>
# include <process.h>

int diplay_menu();
class queue
{
  int arr[10];
  int front; // delete from this positionint rear;  // insert from this positionpublic:
     queue()
     {
      front=-1;
      rear=-1;
     }
     int getfront();
     int getrear();
     void insert(int);
     void delete_element();
     void rearrange();
     void display();
};


int diplay_menu()
{
 clrscr();
 int ch;
 cout<<endl;
 cout<<"\t\t\t| 1 | : Insert Element"<<endl;
 cout<<"\t\t\t| 2 | : Delete Element"<<endl;
 cout<<"\t\t\t| 3 | : Display Queue"<<endl;
 cout<<"\t\t\t| 4 | : Rearrange Queue"<<endl;
 cout<<"\t\t\t| 5 | : Exit"<<endl;
 cout<<"Enter Your Choice :";
 cin>>ch;
 return(ch);
}

int queue :: getfront()
{
 return(front);
}
int queue :: getrear()
{
 return(rear);
}
void queue :: insert(int x)
{
  arr[rear+1]=x;
  if(front==-1)
  {
   front=0;
  }
  rear=rear+1;
}




void queue :: delete_element()
{
 arr[front]=0;
 front=front+1;
 if(front>rear)
 {
  front=-1;
  rear=-1;
 }
}

void queue :: rearrange()
{
 if(front!=-1 && rear !=-1)
 {
  if(front!=0)
  {
   for(int i=0;i<=(rear-front);i++)
   {
     arr[i]=arr[front+i];
   }
  }
  rear=rear-front;
  front=0;
 }
}

void queue :: display()
{
 cout<<endl;
 if(front!=-1 && rear !=-1)
  {
    for(int i=front;i<=rear;i++)
    {
        cout<<"Queue["<<i<<"] :"<<arr[i]<<"\t";
    }
  }
 else
 {
  cout<<"Queue is empty !!!";
 }
}


void main()
{
 clrscr();
 queue q1;
 while(1)
 {
  switch(diplay_menu())
  {
   case 1: if(q1.getrear()==9)
       {
         if(q1.getfront()==0)
         {
          cout<<"Queue is Full !!!";
          getch();
          break;
         }
         else
         {
           char c;
           cout<<"There is a Space in a Queue"<<endl;
           cout<<"Do you want to Rearrange Queue [Y/N] ?";





           cin>>c;
           if(c=='y' || c=='Y')
           {
        q1.rearrange();
           }
           else
           {
        break;
           }
         }
       }
        int no;
        cout<<"Enter Number to insert :";
        cin>>no;
        q1.insert(no);
        q1.display();
        getch();
        break;
   case 2 : if(q1.getfront()!=-1 && q1.getrear() !=-1)
        {
         q1.delete_element();
        }
        else
        {
         cout<<"Queue is empty !!!"<<endl;
        }
        q1.display();
        getch();

        break;
   case 3 : q1.display();
        getch();
        break;
   case 4 : q1.rearrange();
        break;
   case 5 : exit(1);
  }
 }
}
  
Share: 


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

Easy Tutor
Easy Tutor author of Program to create a queue using static memory allocation 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!