Tuesday, 31 July 2018

Program-16 Array Queue Insertion and Deletion


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

int Remove(int []);
int Insert(int [], int);
void Display(int [], int, int);

const int size = 50;
int Queue[size],front=-1,rear=-1;

int main()
{
   int Item,res; char ch='Y';
   clrscr();

   while( ch=='y'||ch=='Y')
   {
     cout<<"\n  Enter Item for Insertion\n";
     cin>>Item;
     res=Insert(Queue,Item);
     if(res==-1)
       {
     cout<<"Overflow \n";exit(1);
    }
      cout<<"\n Now the Queue(front....to....rear)is: \n";
      Display(Queue,front,rear);
      cout<<"\n Want to insert more elements?(y|n)...";
      cin>>ch;
   }

     cout<<"Now deletion of elements begins,,,\n";
     ch='Y';
     while(ch=='y'||ch=='Y')
     { res=Remove(Queue);
       if( res==-1)
       {cout<<"Underflow!!!Aborting!!\n";   exit(1);
       getch();
       }
       else
       {  cout<<"\n Element deletion is: "<<res<<endl;
      cout<<"now the Queue(front...to...back) begins:";
      Display(Queue,front,rear);
       }
       cout<<"\nwant to delete more elements ?(y/n)....\n";cin>>ch;
     }
     return 0;
  }//main end

  int Insert(int Queue[], int ele)
  {
     if(rear == size-1) return -1;
     else
     if(rear==-1)
     {
      front=rear=0;
      Queue[rear]=ele;
     }
     else
     {
    rear++;
    Queue[rear]=ele;
     }
     return 0;
   }
   int Remove(int Queue[])
   {
     int ret;
     if(front==-1) return -1;
     else
     {
       ret=Queue[front];
       if(front == rear) front=rear=-1;
       else
       front++;
     }
     return ret;
   }
  void Display(int SQueue[],int front,int rear)
  {
     if(front==-1) return;
     for(int i=front;i<rear;i++)
    cout<<Queue[i]<<"<-";
     cout<<Queue[rear]<<endl;
  }

No comments:

Post a Comment