Your ultimate guide in programming...

Queue implementation in C++


                    //Queue implementation in C++
#include<iostream>           
using namespace std;
class Queue {
int item[5];
int front;
int rear;
public:
Queue() { //Constructor....
front = 0;
rear = -1;
}
void insert(int x) { // INSERTION FUNCTION TO INSERT VALUES IN QUEUE....
item[++rear] = x;
}
bool isempty() {
if (front>rear)
return true;
return false;
}
void apinsert(int x) { // ORDERING OF DATA IN QUEUE IN ASSENDING ORDER....
if (isempty()) {
insert(x);
}
else if (x >= item[rear])
{
insert(x);
}
else
{
Queue q2;
while (!isempty()) {
int t = remove();
if (x<t) {
q2.insert(x);
q2.insert(t);
break;
}
else
{
q2.insert(t);
}
}
while (!isempty())
q2.insert(remove());

front = 0;
rear = -1;
while (!q2.isempty()) {
insert(q2.remove());
}

}
}

int remove()
{
return item[front++];
}
void dislpay()
{

for (int k = 4; k>rear; k--)
cout << "\nEmpty";
for (int i = rear; i >= front; i--)
cout << endl << item[i];

for (int j = (front - 1); j >= 0; j--)
cout << "\nEmpty";
}
bool isfull()
{
if (rear >= 4)
return true;
return false;
}
};
int main() // MAIN DRIVER....
{
Queue q1;

char repeat = 'y';
do {

int input;
cout << "1) Press '1' to insert: " << endl;
cout << "2) Press '2' to remove: " << endl;
cout << "3) Press '3' to Display: " << endl;
cin >> input;
system("cls");          //STANDARD LIBARAY HEADER FILE   (TO CLEAR SCREEN)
switch (input) {
case 1: {
int num;
cout << "Enter Number: ";
cin >> num;
system("cls");
q1.apinsert(num);
break;
}
case 2: {
if (q1.isempty())cout << "\nThe queue is already empty";
else
cout << "\nThe removed value is:" << q1.remove();
break;
}
case 3: {
q1.dislpay();
break;
}
default:
cout << "Error 404: ";
}
cout << "\nRepeat: y/n..";
cin >> repeat;
system("cls");
} while (repeat == 'y');
return 0;
}

Share:

No comments:

Post a Comment

Newest Post

Coding Problem To Solve Exercise 3

                   Coding Problem To Solve Exercise 3 Objectives: Practice and understanding of basic c++ programs Control Structure(repetit...

Popular Posts

Recent Posts