Question

In: Computer Science

4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the...

4.3 Lab: Queues 1

Write the c++ implementation of the following four member functions of the Queue class:

  • getLength(): returns the number of elements in the queue
  • isEmpty(): returns true if the queue is empty, false otherwise
  • peek(): returns the value at the front of the queue without removing it. Assumes queue is not empty (no need to check)
  • peekRear(): returns the value at the end of the queue without removing it. Assumes queue is not empty (no need to check)

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;
}

Solutions

Expert Solution

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.


Related Solutions

C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open...
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open account (with an initial deposit). This is called to put initial values in dollars and cents. 2. Make a deposit. A function that will add value to dollars and cents 3. Make a withdrawal. A function that will subtract values from dollars and cents. 4. Show current balance. A function that will print dollars and cents. Data (Member Data) 1. dollars 2. cents Operations...
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an...
C++ 19.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2...
*In C++ Please! Give the full implementation of a constant member function that returns the second...
*In C++ Please! Give the full implementation of a constant member function that returns the second element from the top of the stack without actually changing the stack. Write separate solutions for the two different stack versions. (C++ program for array implementation of stack with insert function and display of the second element from top)
C++ , Write an iterative routine that will have 2 queues. Loop for 100 times and...
C++ , Write an iterative routine that will have 2 queues. Loop for 100 times and in the loop roll two die and place the first result into the queue1 and second into queue2. Then dequeue the rolls one at a time from each, printing each pair, and count how many times two rolls either add up to 7 or are doubles (i.e. same value). After the queues are empty, print out the number of 7s and doubles. Assume srand...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT