Question

In: Computer Science

You will use the definition of the linked-queue from lab6, and re-write it as a template...

You will use the definition of the linked-queue from lab6, and re-write it as a template for a linked-queue (I hope you finished the function definitions)

In the driver file, create and use queues of different types to show it works.

In the documentation, indicate if there are any types it won’t work for, and why not.

driver.cpp

#include <iostream>
using namespace std;

#include "LQueue.h"
void print(Queue q)
{
q.display(cout);
}
int main()
{
Queue q1;
cout << "Queue created. Empty? " << boolalpha << q1.empty() << endl;
cout << "How many elements to add to the queue? ";
int numItems;
cin >> numItems;
for (int i = 1; i <= numItems; i++)
  q1.enqueue(100 * i);
cout << "Contents of queue q1 (via print):\n";
print(q1); cout << endl;
Queue q2;
q2 = q1;
cout << "Contents of queue q2 after q2 = q1 (via print):\n";
print(q2); cout << endl;
cout << "Queue q2 empty? " << q2.empty() << endl;
cout << "Front value in q2: " << q2.front() << endl;
while (!q2.empty())
{
  cout << "Remove front -- Queue contents: ";
  q2.dequeue();
  q2.display(cout);
}
cout << "Queue q2 empty? " << q2.empty() << endl;
cout << "Front value in q2?" << endl << q2.front() << endl;
cout << "Trying to remove front of q2: " << endl;
q2.dequeue();
return 0;
}

LQueue.h

#include <iostream>
#ifndef LQUEUE
#define LQUEUE
typedef int QueueElement;
class Queue
{
public:
/***** Function Members *****/
/***** Constructors *****/
Queue();
/*-----------------------------------------------------------------------
     Construct a Queue object.
      Precondition: None.
       Postcondition: An empty Queue object has been constructed.
            (myFront and myBack are initialized to null pointers).
      -----------------------------------------------------------------------*/
Queue(const Queue & original);
/*-----------------------------------------------------------------------
     Copy Constructor
      Precondition: original is the queue to be copied and is received
           as a const reference parameter.
         Postcondition: A copy of original has been constructed.
      -----------------------------------------------------------------------*/
      /***** Destructor *****/
~Queue();
/*-----------------------------------------------------------------------
     Class destructor
      Precondition: None.
       Postcondition: The linked list in the queue has been deallocated.
    -----------------------------------------------------------------------*/
    /***** Assignment *****/
const Queue & operator= (const Queue & rightHandSide);
/*-----------------------------------------------------------------------
     Assignment Operator
      Precondition: rightHandSide is the queue to be assigned and is
           received as a const reference parameter.
         Postcondition: The current queue becomes a copy of rightHandSide
              and a reference to it is returned.
        -----------------------------------------------------------------------*/
bool empty() const;
/*-----------------------------------------------------------------------
    Check if queue is empty.

       Precondition: None.
       Postcondition: Returns true if queue is empty and false otherwise.
       -----------------------------------------------------------------------*/
void enqueue(const QueueElement & value);
/*-----------------------------------------------------------------------
    Add a value to a queue.

       Precondition: value is to be added to this queue.
       Postcondition: value is added at back of queue.             
       -----------------------------------------------------------------------*/
void display(ostream & out) const;
/*-----------------------------------------------------------------------
    Display values stored in the queue.
       Precondition: ostream out is open.
       Postcondition: Queue's contents, from front to back, have been
       output to out.
       -----------------------------------------------------------------------*/
QueueElement front() const;
/*-----------------------------------------------------------------------
    Retrieve/Peep value at front of queue (if any).

       Precondition: Queue is nonempty.
       Postcondition: Value at front of queue is returned, unless the queue
       is empty; in that case, an error message is displayed and a
       "garbage value" is returned.
       -----------------------------------------------------------------------*/
void dequeue();
/*-----------------------------------------------------------------------
    Remove value at front of queue (if any).
       Precondition: Queue is nonempty.
       Postcondition: Value at front of queue has been removed, unless
       queue is empty; in that case, an error message is displayed
       and execution allowed to proceed.
       -----------------------------------------------------------------------*/
private:
void delete_q(); // utility/helper function to delete queues for
      // destructor and assignment operator
         /*** Node class for the queue***/
class Node
{
public:
  QueueElement data;
  Node * next;
  //--- Node constructor
  Node(QueueElement value, Node * link = 0)
        /*-------------------------------------------------------------------
            Precondition: value and link are received
             Postcondition: A Node has been constructed with value in its
                 data part and its next part set to link (default 0).
                  ------------------------------------------------------------------*/
  {
   data = value; next = link;
  }

}; //for Node class
typedef Node * NodePointer;

/***** Data Members *****/
NodePointer myFront,      // pointer to front of queue
  myBack;                 // pointer to back of queue

}; // end of class declaration
#endif

LQueue-Incomplete.cpp

#include <new>
using namespace std;
#include "LQueue.h"
//--- Definition of Queue constructor
Queue::Queue()
: myFront(0), myBack(0)
{}
//--- Definition of Queue copy constructor
Queue::Queue(const Queue & original)
{
myFront = myBack = 0;
if (!original.empty())
{
  // Copy first node
  myFront = myBack = new Node(original.front());
  // Set pointer to run through original's linked list
  NodePointer origPtr = original.myFront->next;
  while (origPtr != 0)
  {
   myBack->next = new Node(origPtr->data);
   myBack = myBack->next;
   origPtr = origPtr->next;
  } //while
} //if
}
void Queue::delete_q(void) {
// Set pointer to run through the queue
NodePointer prev = myFront, // node to be released/deleted
  ptr; // points to the front node

while (prev != 0)
{
  ptr = prev->next;
  delete prev;
  prev = ptr;
}
}
//--- Definition of Queue destructor
// delete queue from the front
Queue::~Queue()
{
delete_q();
}
//--- Definition of assignment operator
const Queue & Queue::operator=(const Queue & rightHandSide)
{
if (this != &rightHandSide)         // check that not q = q
{
  this->delete_q();               // destroy current linked list

  if (rightHandSide.empty())       // empty queue
   myFront = myBack = 0;
  else
  {                                // copy rightHandSide's list
           // Copy first node
   myFront = myBack = new Node(rightHandSide.front());
   // Set pointer to run through rightHandSide's linked list
   NodePointer rhsPtr = rightHandSide.myFront->next;
   while (rhsPtr != 0)
   {
    myBack->next = new Node(rhsPtr->data);
    myBack = myBack->next;
    rhsPtr = rhsPtr->next;
   }
  }
}
return *this;
}
//--- Definition of empty()
bool Queue::empty() const
{
return (myFront == 0);
}
//--- Definition of enqueue()
void Queue::enqueue(const QueueElement & value)
{
NodePointer newptr = new Node(value);
if (empty())
  myFront = myBack = newptr;
else
{
  myBack->next = newptr;
  myBack = newptr;
}
}
//--- Definition of display()
void Queue::display(ostream & out) const
{
NodePointer ptr;

for (ptr = myFront; ptr != 0; ptr = ptr->next)
  out << ptr->data << " ";
out << endl;
}
//--- Definition of front()
// Peep the first element of the queue
QueueElement Queue::front() const
{
if (!empty())
  return (myFront->data);
else
{
  cerr << "*** Queue is empty "
   " -- returning garbage ***\n";
  QueueElement * temp = new(QueueElement);
  QueueElement garbage = *temp;     // "Garbage" value
  delete temp;
  return garbage;
}
}
//--- Definition of dequeue()
// simply decrement the queue
void Queue::dequeue()
{
if (!empty())
{
  NodePointer ptr = myFront;
  myFront = myFront->next;
  delete ptr;
  if (myFront == 0)     // queue is now empty
   myBack = 0;
}
else
  cerr << "*** Queue is empty -- can't remove a value ***\n";
}

Solutions

Expert Solution

Answer:

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
void push(int x)
{
    np = new node;
    np->data = x;
    np->next = NULL;
    if(front == NULL)
    {
        front = rear = np;
        rear->next = NULL;
    }
    else
    {
        rear->next = np;
        rear = np;
        rear->next = NULL;
    }
}
int remove()
{
    int x;
    if(front == NULL)
    {
        cout<<"empty queue\n";
    }
    else
    {
        p = front;
        x = p->data;
        front = front->next;
        delete(p);
        return(x);
    }
}
int main()
{
    int n,c = 0,x;
    cout<<"Enter the number of values to be pushed into queue\n";
    cin>>n;
    while (c < n)
    {
   cout<<"Enter the value to be entered into queue\n";
   cin>>x;
        push(x);
        c++;
     }
     cout<<"\n\nRemoved Values\n\n";
     while(true)
     {
        if (front != NULL)
            cout<<remove()<<endl;
        else
            break;
     }
     getch();
}


Related Solutions

Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
please answer my question... Write a template class that implements an extended queue (use Linked List)....
please answer my question... Write a template class that implements an extended queue (use Linked List). Ex: ExtendedQueue<int> int_queue; ExtendedQueue<double> double_queue; ExtendedQueue<char> char_queue; –Write a program to test this template class in c++. the details of the ExtendedQueue: insert delete front rear queue size. the output will be: Inserting 2 Inserting 4 Inserting 6 Front element is: 2 Removing 2 Inserting 8 Queue size is 3 Removing 4 Removing 6 Removing 8 Queue Is Empty
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 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)
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
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 C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
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...
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT