Pages

Monday 8 November 2021

Program to illustrate Queue using Arrays in c++

  /* queue implementation with class and object using array */

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 20

int i,n,qa[max],item,front=-1,rear=-1; /* global declaration */

class rkqueue
{
public:
int add(void); /* create function declaration */
int del(void); /* delete function declaration */
int display(void); /* display function declaration */
};

int rkqueue::add(void) /* implementation function */
  {
    if(rear==n-1)
     {
     cout<<"\n queue overflow";
     return rear;
     }
    else
    {
     if(front==-1)
     {
       front=0;
     }
     rear++;
     cout<<"\n enter item to be added on to the queue:";
     cin>>item;
     qa[rear]=item;
     return rear;
    }
  }

int rkqueue::del(void) /* implementation function */
   {
    if(front==-1 || front>rear)
    {
    cout<<"\n queue underflow ";
    return front;
    }
    else
    {
    cout<<"\n The deleted item from queue is="<<qa[front];
    qa[front]='\0';
    if(front==rear)
     {
      front=-1;
      rear=-1;
     }
    front++;
    return front;
   }
 }

int rkqueue::display()
   {
   if((front<0 && rear<0)||(front>rear))
    {
    cout<<"\n queue is empty";
    }
    else
    {
    cout<<"\nqueue elements are:";
    for(i=0;i<n;i++)
     {
     if(qa[i] == '\0')
      cout<<" \t ";
     else
     cout<<qa[i]<<"  ";
     }
     cout<<"\n Position of front "<<front<<" and rear is "<<rear;
   }
 }

void main()
{
   rkqueue q;
   int choice;
    clrscr();
    cout<<"\nEnter the length of the queue array  :\n";
    cin>>n;
 while(1)
 {
    cout<<"\n  MENU \n";
    cout<<"  1.Add to queue  \n";
    cout<<"  2.Delete from queue \n";
    cout<<"  3.Display   \n";
    cout<<"  4.Quit      \n";
    cout<<"Enter ur choice: ";
    cin>>choice;
    switch(choice)
    {
       case 1: q.add();break;
       case 2: q.del();break;
       case 3: q.display();break;
       case 4: exit(1);break;
       default:cout<<"\n u have entered wrong choice";break;
    } /* end of switch case */
  }/* end of while loop */
}

Output:

/*Queue implementation without class and object using array */
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 20
int add(void); /* create function declaration */
int del(void); /* delete function declaration */
int display(void); /* display function declaration */
int i,n,qa[max],item,front=-1,rear=-1; /* global declaration */

void main()
{
   int choice;
    clrscr();
    cout<<"\nEnter the length of the queue array  :\n";
    cin>>n;
 while(1)
 {
    cout<<"\n  MENU \n";
    cout<<"  1.Add to queue  \n";
    cout<<"  2.Delete from queue \n";
    cout<<"  3.Display   \n";
    cout<<"  4.Quit      \n";
    cout<<"Enter ur choice: ";
    cin>>choice;
    switch(choice)
    {
       case 1: add();break;
       case 2: del();break;
       case 3: display();break;
       case 4: exit(1);break;
       default:cout<<"\n u have entered wrong choice";break;
    } /* end of switch case */
  }/* end of while loop */
}

int add(void) /* implementation function */
  {
    if(rear==n-1)
     {
     cout<<"\n queue overflow";
     return rear;
     }
    else
    {
     if(front==-1)
     {
       front=0;
     }
     rear++;
     cout<<"\n enter item to be added on to the queue:";
     cin>>item;
     qa[rear]=item;
     return rear;
    }
  }

int del(void) /* implementation function */
   {
    if(front==-1 || front>rear)
    {
    cout<<"\n queue underflow ";
    return front;
    }
    else
    {
    cout<<"\n The deleted item from queue is="<<qa[front];
    qa[front]='\0';
    if(front==rear)
     {
      front=-1;
      rear=-1;
     }
    front++;
    return front;
   }
 }

int display()
   {
   if((front<0 && rear<0)||(front>rear))
    {
    cout<<"\n queue is empty";
    }
    else
    {
    cout<<"\nqueue elements are:";
    for(i=0;i<n;i++)
     {
     if(qa[i] == '\0')
      cout<<" \t ";
     else
     cout<<qa[i]<<"  ";
     }
     cout<<"\n Position of front "<<front<<" and rear is "<<rear;
   }
 }


ANIMATION VIDEO LINK OF QUEUE :
https://www.youtube.com/watch?v=vPVWuoR43oM

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...