0% found this document useful (0 votes)
3 views3 pages

Queue using array c++

This document contains a C++ program that implements a circular queue using arrays. It includes operations for inserting, deleting, and displaying elements in the queue. The program handles overflow and underflow conditions and provides a user interface for interaction.

Uploaded by

saravanaoctaquad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Queue using array c++

This document contains a C++ program that implements a circular queue using arrays. It includes operations for inserting, deleting, and displaying elements in the queue. The program handles overflow and underflow conditions and provides a user interface for interaction.

Uploaded by

saravanaoctaquad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// IMPLEMENT ADD, DELETE OPERATIONS OF QUEUE USING ARRAYS

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
#define size 5

class Queue
{
public:
int rear, front;
int ch;
int q[size];

public:
Queue()
{
rear = front = -1;
}

void insert_queue();
void delete_queue();
void display_queue();
};

void Queue::insert_queue()
{
if((front == 0) && (rear == size - 1))
{
cout<<"\n overflow";
//rear = 1;
return;
}
else if(front < 0)
{
front = 0;
rear = 0;
cout<<"\n Insert the element";
cin>>ch;
q[rear] = ch;
}
else if(rear == size - 1)
{
rear = 0;
cout<<"\nInsert the element:";
cin>>ch;
q[rear] = ch;
}
else
{
rear++;
cout<<"\nInsert the element:";
cin>>ch;
q[rear] = ch;
}
}

void Queue::delete_queue()
{
if(front < 0)
{
cout<<"\n underflow";
return;
}

ch = q[front];

cout<<"Elementis Deleted:"<<ch;

if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == size - 1)
{
front = 0;
}
else
{
front++;
}
}

void Queue::display_queue()
{
if(front < 0)
return;

if(rear >= front)


{
for(int i = front; i <= rear; i++)
{
cout<<q[i]<<" ";
}
}
else
{
for(int i = front; i <size; i++)
{
cout<<q[i]<<" ";
}
for(int j = 0; j <= rear; j++)
{
cout<<q[j]<<" ";
}
}
}
void main()
{
Queue Q;
int k = 0;
int s;
clrscr();

do
{
cout<<"\n \t 1. INSERT\t 2. DELETE\t 3. QUIT\n";
cout<<"\t Select the choice [1, 2, 3]:";
cin>>s;

switch(s)
{
case 1:
Q.insert_queue();
cout<<"\n queue after inserting:";
Q.display_queue();
break;

case 2:
Q.delete_queue();
cout<<"\n queue after deleteion:";
Q.display_queue();
break;

case 3:
k = 4;
}
} while(!k);

getch();
}

You might also like