Friday, September 23, 2016

OPERATIONS ON QUEUES

Tags

Program to Perform Insertion and Deletion Operations on Queues Implemented as an Array.

#include<iostream>
#include <string.h>
using namespace std;
void add(int q[],int front,int rear);
void deletion(int q[],int front,int rear);
int main()
{
int q[30],choice;
int i,n,front=1,rear;
start:
cout<<"\n\t**OPERATIONS ON QUEUES**\n";
cout<<"Enter Size of Queue: ";
cin>>n;
cout<<"Enter Elements of the Queue: ";
for(i=front;i<=n;i++)
{
cin>>q[i];
rear=i;
}
cout<<"\n1: Insertion\n2: Deletion\n";
    cout<<"Enter your Choice(Press F6 to Exit): ";

    while(cin>>choice)
    {

        switch(choice)
        {
        case 1:
            add(q,front,rear);
            cout<<"\nEnd of Program";
            break;
        case 2:
            deletion(q,front,rear);
            cout<<"\nEnd of Program";
            break;
        default:
            cout<<"\nInvalid Input";
        }
        goto start;
    }
    return 0;
}

void add(int q[],int front,int rear)
{
int i,item;
cout<<"\nEnter Element to Be Inserted: ";
cin>>item;
rear++;
q[rear]=item;
cout<<"\nNew Queue is:\n";
for(i=front;i<=rear;i++)
cout<<q[i]<<" ";
}

void deletion(int q[],int front,int rear)
{
int i;
front++;
cout<<"\nQueue after Deletion is:\n";
for(i=front;i<=rear;i++)
cout<<q[i]<<" ";
}