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

Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Show how circular linked list can be useful to implement Circular Queue (CQ) ADT (Abstract Data...
Show how circular linked list can be useful to implement Circular Queue (CQ) ADT (Abstract Data Type). Compare and contrast with array based implementation. Which one would you recommend to implement CQ and why
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Give a complete implementation of the queue ADT using a singly linked list that includes a...
Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT