In: Computer Science
4.3 Lab: Queues 1
Write the c++ implementation of the following four member functions of the Queue class:
The push function is given.
Write code in main to test these functions. Write a loop to enter an unknown number of words. The loop stops when you enter "#" As you are entering words, they are to be inserted into a queue. Once done, display the number of elements on the first line, the value at the front of the queue on the next line, and the value at the end of the queue on the last line.
Ex.: If the user enters cat dog mouse tiger # the output should be:
4 cat tiger
Ex.: If the user enters # the output should be:
0 Empty Queue!
Incomplete Program here:
#include <iostream>
#include <string>
using namespace std;
class Queue_str
{
private:
// Structure for the stack nodes
struct QueueNode {
string value; // Value in the node
QueueNode *next; // Pointer to next node
};
QueueNode *front; // Pointer to the first node
QueueNode *rear; // Pointer to the last node
int length; // Number of nodes in the queue
public:
Queue_str(){ front = rear = NULL; length = 0; } //Constructor
//~Queue_str(); // Destructor
// Queue operations
bool isEmpty() {/* Write your code here */}
bool push(string);
// string pop();
string peek() {/* Write your code here */ }
string peekRear() {/* Write your code here */ }
int getLength() {/* Write your code here */ }
};
/**~*~*
Member function push: inserts the argument into the queue
*~**/
bool Queue_str::push(string item)
{
QueueNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new QueueNode;
if (!newNode)
return false;
newNode->value = item;
newNode->next = NULL;
// Update links and counter
if (!front) // front is NULL: empty queue
front = newNode;
else
rear->next = newNode;
rear = newNode;
length++;
return true;
}
int main() {
Queue_str que;
string item;
return 0;
}
Here is your complete code:
******************************************************************************************************
#include <iostream>
#include <string>
using namespace std;
class Queue_str
{
private:
// Structure for the stack nodes
struct QueueNode {
string value; // Value in the node
QueueNode *next; // Pointer to next node
};
QueueNode *front; // Pointer to the first node
QueueNode *rear; // Pointer to the last node
int length; // Number of nodes in the queue
public:
Queue_str(){ front = rear = NULL; length = 0; } //Constructor
//~Queue_str(); // Destructor
// Queue operations
bool isEmpty() {if(length==0) return true;}
bool push(string);
// string pop();
string peek()
{
return front->value;
}
string peekRear()
{
return rear->value;
}
int getLength()
{
return length;
}
};
/**~*~*
Member function push: inserts the argument into the queue
*~**/
bool Queue_str::push(string item)
{
QueueNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new QueueNode;
if (!newNode)
return false;
newNode->value = item;
newNode->next = NULL;
// Update links and counter
if (!front) // front is NULL: empty queue
front = newNode;
else
rear->next = newNode;
rear = newNode;
length++;
return true;
}
int main() {
Queue_str que;
string item;
do{
cin>>item;
if(item != "#")
que.push(item);
}while(item!="#");
cout<<que.getLength()<<endl;
if(que.getLength()!=0)
{
cout<<que.peek()<<endl;
cout<<que.peekRear()<<endl;
}
else
{
cout<<"Empty Queue!"<<endl;
}
return 0;
}
**********************************************************************************************************
Output:
Screenshot 1:
Screenshot 2:
************************************************************************************************************
I hope this helps you. Good luck.