Question: Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header

Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following:

  • Define the Queue class template in the header file QueueLinked.h.
// QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include  using namespace std; template  class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void enqueue(const T& item) // Removes the element at the start of the queue. void dequeue(); // returns the front element const T& front_element() const; // Prints the elements of the queue. void print() const private: struct NodeType { T data; NodeType* next; NodeType(const T & d = T()): data(d) { next = nullptr; } }; NodeType* front; NodeType* back; }; #endif

Please read the comments carefully and implement the Queue class template.

You can implement the Queue class template in the seperate file QueueLinked.cpp.

// QueueLinked.cpp #include "QueueLinked.h" template  Queue::Queue() { front = nullptr; back = nullptr; } // add other member functions // ....

You also can put the implementation of the Stack class template in Stack.h.

// QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include  using namespace std; template  class Queue { public: // Constructor Queue() { front = nullptr; back = nullptr; } // add other member functions // .... private: struct NodeType { T data; NodeType* next; NodeType(const T & d = T()): data(d) { next = nullptr; } }; NodeType* front; NodeType* back; }; #endif

The main function is contained in the file lab05.cpp

// lab05.cpp #include  #include "QueueLinked.h" #include "QueueLinked.cpp" // add if the interface and implementation are seperate int main() { .... }

  • The main function,
    1. Declare a queue which stores int values.
    2. Prompt the user to enter integers, enqueue the entered values, stop entering the integers when the user enter -1.
    3. Print the elements of queue.
    4. Prompt the user to enter a number k, and dequeue k values from the front of the queue.
    5. Print the elements of queue.
    6. Declare a queue which stores strings.
    7. Prompt the user to enter strings, enqueue the entered strings, stop entering the strings when the user enter exit.
    8. Print the elements of queue.
    9. Prompt the user to enter a number k, and dequeue k strings from the front of the queue.
    10. Print the elements of queue.

The expected result:

Enqueue positive numbers (enter -1 to stop): 1 2 3 4 5 6 7 8 9-1 print queue: 1, 2, 3, 4, 5, 6, 7, 8, 9, How many numbers to be removed from queue: 5 print queue: 6, 7, 8, 9, Enqueue string (enter exit to stop): abc def ghi jkl mno pqr exit print queue: abc, def, ghi, jkl, mno, pqr, How many strings to be removed from queue: 5 print queue: pqr,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!