Question

In: Computer Science

Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...

Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.

Solutions

Expert Solution

// ---- Queue.cpp ------
/*
* Testing all the operations that can be done by Queue ADT in cpp
*
* queue is in built implemented in cpp
* to use queue use #include<queue> to import it.
* operations are:
*    enqueue() --> push() in queue adt.
*    dequeue() --> pop() in queue adt.
*    front()   -> returns reference of the front of the queue.
*    back()   -> returns the reference of end of the queue
*    size() -> returns the size of the queue
*   empty() -> returns whethere queue is empty or not.
*/

#include<iostream>
#include<queue>
#include<limits>
using namespace std;
//function that checks the cin stream for
//any invalid input format is given like giving string for number as input.
bool isInvalidInput()
{
   if(cin.fail())
   {
       //clear the buffer.
       cin.clear();
       cin.ignore(numeric_limits<streamsize>::max(),'\n');
       return true;
   }
   return false;
}
//display menu to user.
void menu()
{
   cout<<"\n1. Enqueue a value"<<endl;
   cout<<"2. Dequeue a value"<<endl;
   cout<<"3. Front element in queue"<<endl;
   cout<<"4. Back element in queue"<<endl;
   cout<<"5. Print Queue"<<endl;
   cout<<"0. Exit"<<endl;
   cout<<"Enter your choice: ";
}
//print the queue and it's values.
void print(queue<int> q)
{
   cout<<"\nQueue : \n";
   queue<int> temp = q;
   if(temp.empty())
   {
       cout<<"\nQueue is Empty"<<endl;
       return;
   }
   while(!temp.empty())
   {
       cout<<temp.front()<<" ";
       temp.pop();
   }
   cout<<"\n";
}
//function that prompts the user with given message
//and returns the valid input that is an integer not text.
int getInt(string prompt)
{
   bool isValid = false;
   int val;
   while(!isValid)
   {
       cout<<endl<<prompt;
       cin >> val;
       if(isInvalidInput())
       {
           cout<<"\nInvalid Input. Try Again"<<endl;
       }
       else
       {
           isValid = true;
       }
   }
   return val;
}
int main()
{
   //create queue object. it takes a data type.
   queue<int> list;
   int choice = 1;
   int val;
   do
   {
       menu();
       cin >> choice ;
       if(isInvalidInput())
       {
           choice = -1;
       }
       switch(choice)
       {
           case 1:
           {
               val = getInt("Enter value to Enqueue: ");
               //call push to enqueue the value.
               list.push(val);
               break;
           }
           case 2:
           //get size of the queue.
               if(list.size() > 0)
               {
                   //store the front() of the queue to display
                   val = list.front();
                   //pop the value from queue
                   list.pop();
                   cout<<"\nValue Dequeued from queue: "<<val<<endl;
               }
               else
               {
                   cout<<"\nQueue is Empty"<<endl;
               }
               break;
           case 3:
               if(list.size() > 0)
               {
                   //get front() of the queue.
                   val = list.front();
                   cout<<"\nValue at Front of the queue: "<<val<<endl;
               }
               else
               {
                   cout<<"\nQueue is Empty"<<endl;
               }
               break;
           case 4:
               if(list.size() > 0)
               {
                   val = list.back();
                   cout<<"\nValue at Back of the queue: "<<val<<endl;
               }
               else
               {
                   cout<<"\nQueue is Empty"<<endl;
               }
               break;
           case 5:
           //call print() method to print the queue.
               print(list);
               break;
           case 0:
               cout<<"\nQuitting...";
               break;
           default:
               cout<<"\nInvalid Input. Try again\n";
               break;
       }
      
   }while(choice != 0);
}


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode*...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr) { } template QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE) { } template QueueLinked::QueueLinked(const QueueLinked& other) { } template QueueLinked& QueueLinked::operator=(const QueueLinked& other) { } template QueueLinked::~QueueLinked() { } template void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error) { } template DataType QueueLinked::dequeue() throw (logic_error) {    DataType temp;    return temp; } template void QueueLinked::clear() { } template bool QueueLinked::isEmpty() const {    return false; } template...
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT