ACTIVITY Write a program using queues with the output and requirements shown below: a. 4 Include Library b. For Loop c. While Looping d. Switch Case not allowed e. 10 QUEUE Container f. You can use String data Type g. #define SIZE 10 h. Function Needed 1. Empty(); 2. Full(); 3. Enqueue(); 4. Dequeue 5. DisplayFront(); 6. DisplayAll(); 7. (You can make a New Function/Operation if Needed)
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
#define MAX_SIZE 10
class Queue
{
private:
int myq[MAX_SIZE], front, rear;
public:
Queue()
{
front = -1;
rear = -1;
}
Full()
{
if(front == 0 && rear == MAX_SIZE - 1)
{
return true;
}
return false;
}
Empty()
{
if(front == -1) return true;
else return false;
}
void EnQueue(int val)
{
if(Full())
{
cout << endl<< "Queue is full!!";
}
else
{
if(front == -1) front = 0;
rear++;
myq[rear] = val;
cout << val << " ";
}
}
int DeQueue()
{
int val;
if(Empty())
{
cout << "Queue is empty!!" << endl; return(-1);
}
else
{
val = myq[front];
if(front >= rear)
{ //only one element in queue
front = -1;
rear = -1;
}
else
{
front++;
}
cout << endl << "Deleted => " << value << " from myq";
return(val);
}
}
void display()
{
int i;
if(Empty())
{
cout << endl << "Queue is Empty!!" << endl;
}
else
{
cout << endl << "Front = " << front;
cout << endl << "Queue Elements : ";
for(i=front; i<=rear; i++)
cout << myqueue[i] << "\t";
cout << endl << "Rear = " << rear << endl;
}
}
};
int main()
{
Queue q;
q.deQueue();
cout<<"Create a Queue:"<<endl;
q.EnQueue(1); q.enQueue(2); q.enQueue(3); q.enQueue(4); q.enQueue(5);
q.EnQueue(11); q.enQueue(22); q.enQueue(33); q.enQueue(44); q.enQueue(55);
q.EenQueue(66);
q.display();
q.DeQueue();
q.display();
return 0;
}
Comments
Leave a comment