Purpose This assignment is an exercise in implementing the queue ADT using a sin
ID: 3761842 • Letter: P
Question
Purpose
This assignment is an exercise in implementing the queue ADT using a singly-linked list. This assignment also introduces the concept of templates.
Assignment
This program creates and implements a class to represent the Queue ADT using a singly-linked list.
A driver program is provided for this assignment to test your implementation. You don't have to write the tests.
Program
You will need to write one template structure and one template class for this assignment, called Node and Queue. You will need to implement several methods and functions associated with these data types.
Since these are both C++ templates and are closely related to each other, all of your code should be placed in a single header (.h) file. This includes the implementations of all methods and any other associated functions.
struct Node
Data members
This template structure should have two data members: a member of the template parameter type to store an item to be inserted into the queue, and a pointer to a Node. The pointer next will point to the next node in the linked list (or be nullptr if this is the last node in the list).
Since the Queue class will need access to these data members, make them public (the default for a struct).
Methods
Constructor
The structure should have one constructor that takes an argument of the template parameter type. Make this argument a reference to const data. The constructor should copy the argument into the queue node and set the node's pointer to nullptr.
class Queue
Data members
This class should have three data members. The first two are pointers to Nodes. The pointer qFront will point to the front node in the queue (or be nullptr if the queue is empty); the pointer qRear will point to the rear node in the queue (or be nullptr if the queue is empty). The third data member is used to keep track of the number of data items currently stored in the vector (the queue size). This data member should be declared as data type size_t (which corresponds to an unsigned integer).
Methods and associated functions
Constructor
The class should have a default constructor that takes no arguments. The constructor should set both pointer data members to nullptr and the queue size to 0.
Destructor
The class should have a destructor. The destructor can simply call the clear() method described below.
Copy constructor
The class should also have a proper copy constructor. If you choose to make a copy of the linked list by using the push() method, make sure you set both the front and rear pointers for the new queue to nullptr and the queue size to 0 before you attempt to insert any nodes into it.
operator=
The copy assignment operator should be properly overloaded.
operator<<
The output operator should be overloaded so that an entire Queue can be sent to the standard output. As usual, this function will need to be a friend rather than a method. Declaring a template function to be a friend of a template class requires some special syntax - see theImplementation Hints below.
clear()
This method takes no arguments and returns nothing. It should properly set the queue back to the empty state. That means deleting all of the nodes in the queue, setting the front and rear pointers back to nullptr, and setting the queue size back to 0.
size()
This method takes no arguments and returns a size_t. It should return the current size of the queue; i.e., the number of data items currently stored in the queue.
empty()
Returns true if there are no data items currently stored in the queue; otherwise returns false.
front()
This method takes no arguments and returns a reference to a constant item of the template parameter type. If the queue is empty, this method should throw an underflow_error exception. Otherwise, it should return the data stored in the front node of the queue.
back()
This method takes no arguments and returns a reference to a constant item of the template parameter type. If the queue is empty, this method should throw an underflow_error exception. Otherwise, it should return the data stored in the rear node of the queue.
push()
This method takes a reference to a constant item of the template parameter type as its argument (the item to insert into the queue). It returns nothing. The method should insert the item at the rear of the queue and increment the queue size.
pop()
This method takes no arguments and returns nothing. If the queue is empty, this method should throw an underflow_error exception. Otherwise, it should remove the item at the front of the queue and decrement the queue size.
If you like, you may write private methods for the Queue class in addition to the methods described above. For example, you may want to write a copyList() method that can be called by both the copy constructor and overloaded assignment operator.
Output
A driver program, assign7.cpp is provided for this assignment. The purpose of a driver program is to test other pieces that you code. You do not need to write the driver program yourself. A copy of the driver program can also be found on turing at/home/turing/t90kjm1/CS241/Code/Fall2015/Assign7/assign7.cpp.
Implementation Hints
Implement this similarly to the last assignment. Start off at the beginning of the driver program and try to get it working piece by piece. Maintain a working program as you go.
Declaring a template function to be a friend of a template class is one of the classic "gotcha's" in C++. We are trying to declare a function to be a friend of all classes that might be instantiated from the Queue template class, and most C++ compilers will quite properly refuse to do that without some special syntax.
The friend declaration must contain an extra set of <> to indicate that it is a template function (however, do not code this in the actual function definition - only the friend declaration). You'll also usually need to forward declare both the template class and the template function, as shown below.
How much of this you actually need to do can vary from compiler to compiler, but the code shown below works in Dev-C++ and with g++ on turing/hopper.
Explanation / Answer
This is how you need to do by declaring header to this code then sure you will get your output
template <class T>
class Stack<T>
{
// ...
friend ostream& operator<<( ostream& leftOp, const Stack<T>& rightOp )
{
//SNode<T>* ptr = head;
SNode<T>* ptr = rightOp.head;
while( ptr != 0 ) // while (ptr != NULL)
{
leftOp << ptr -> value << " ";
ptr = ptr -> next;
}
return leftOp;
}
// ...
private:
Snode<T>* head ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.