Test it as you add each new operation When youre done use th
Test it as you add each new operation. When you\'re done, use the program Qtester.cpp to test your class.
1. << Overload the output operator << so that a statement of the form cout << q; will display q\'s
contents from front to back.)
2. size() //returns the number of elements in the queue
3. back() // returns the element at the back of a queue
/*
tester.cpp
Program to test class Queue.
Input (keyboard): Commands entered by the user.
Output (screen): List of commands and messages describing
the effects of each command.
----------------------------------------------------------------*/
#include \"Queue.h\"
#include <iostream>
#include <cctype>
using namespace std;
/*--------------------------------------------------------------
Function to list the available commands.
Output (screen): List of commands.
----------------------------------------------------------------*/
void ShowCommands()
{
cout << \"Use the following commands to test the Queue class:\ \"
<< \"a --- add an element to the queue\ \"
<< \"d --- display contents of queue\ \"
<< \"e --- test whether a queue is empty\ \"
<< \"f --- retrieve the item at the front of the queue\ \"
<< \"h --- help -- print this list of commands\ \"
<< \"r --- remove item from front of the queue\ \"
<< \"s --- display the size of queue\ \"
<< \"b --- retrieve the item at the back of the queue\ \"
<< \"q --- quit testing\ \";
}
int main()
{
QueueElement item; // item to add to the queue
char command; // user command (selected from menu)
Queue q; // The QUEUE
ShowCommands();
do
{
cout << \"Command? \";
cin >> command;
if (isupper(command)) command = tolower(command);
switch(command)
{
case \'a\':
cout << \"Enter item to add to queue: \";
cin >> item;
q.enqueue(item);
cout << \"--> \" << item << \" added\ \";
break;
case \'d\':
cout << \"--> Queue contents:\ \" << q << endl;
break;
case \'e\':
cout << \"--> Queue \" << (q.empty() ? \"is\" : \"is not\")
<< \" empty\ \";
break;
case \'f\':
cout << \"--> \" << q.front() << \" is at the front\ \";
case \'h\':
ShowCommands();
break;
case \'r\':
q.dequeue();
cout << \"--> Front element removed\ \";
break;
case \'s\':
cout << \"--> the size of the queue is: \" << q.size() << endl;
break;
case \'b\':
cout << \"--> \" << q.back() << \" is at the back\ \";
break;
case \'q\':
cout << \"--> End of test\ \";
break;
default:
cout << \"*** Illegal command: \" << command << endl;
}
}
while (command != \'q\');
}
Solution
This program contains some compilation errors is their .So this program is not run in proper way .Because QueueElement is not declared with in scope.And iteam was not declared with in scope .So it causes some compilation errors is occured .
So i explain in detail way how to perform a Queue operations .Queue is also linear non primitive data strecture.which is used to sore collection of elements in sequential continous memory locations.A queue is an ordered collection of elements into which we can store data iteam from only one end is called as rear end .
we can delete from an element from another end is called front end .Queue is working under the principle as FIFO(First in first out) .It means first inserted element is deleted first.
and operation of the queue is 1)create 2) Insert 3)Deletion 4)Display 5) IsFull 6)Is Empty 7)Rare value 8)front value.
one important thing is after deleting the last element queue rare and front values becomes \"-1\" .
Syntax:
Data Type Queue name[size];
rare=-1;
fornt=-1;
// Here i explained how to implement Queue operations in c++
# include <iostream>
#include <cctype>
using namespace std;
{
public :
int Q[100],front,rare,max;
Queue()
{
rare=-1;
front=-1;
max=10;
}
void insert();
void delete();
void diaplay();
void IsFull();
void IsEmpty();
};
void Queue :: insert()
{
int x;
cout <<\"enter x\";
cin >>x;
if(rear==max-1)
cout <<\"Queue is over flow\";
else if (rear =-1)
{
rear=rear+1;
front=front+1;
Q[rear]=x;
}
else
{
rear=rear+1;
Q(rear)=x;
}
viod Queue :: delete()
{
if(front==-1)
cout << \"Q is under flow\";
else if (rear==front)
y=Q[front];
rear=-1;
front=-1;
}
else
{
x=Q[front];
front=front+1;
viod Queue :: display()
{
int i;
i=front;
while(i<=rear)
{
cout << Q[i] << endl;
i++;
}
}
void Queue :: IsFull()
{
if (rear==max-1)
cout << \"Queue is full\";
else
cout << \"Queue is not full\";
}
void Queue :: IsEmpty()
{
if (rear==-1)
cout <<\"Queue is Empty\";
else
cout <<\"Queue is not Empty\";
}
main()
Queue qu;
int ch;
while(1)
{
cout <<\"1.insert\"; <<\"2.delete\"; << 3.\"display\"; <<\"4.is full\"; << \"5.isempty\" <<\"6.exit\"
cout <<\"enter ur choice\";
cin>> ch;
switch(ch);
case 1:qu.insert();
break;
case 2:qu.delete();
break;
case 3:qu.display();
break;
case 4:qu.IsFull();
break;
case 5:qu.IsEmpty();
break;
case 6:exit(0);
break;
default ; cout << \"invalid choice:\"
}
}
}





