Purpose This program is an exercise in implementing the queue ADT using a singly
ID: 3631554 • Letter: P
Question
Purpose
This program is an exercise in implementing the queue ADT using a singly-linked list. This program also introduces the concept of templates.
This program creates and implements a class to represent the Queue ADT using a singly-linked list.
A driver program is provided for this program 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 QNode 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 QNode
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 QNode. The pointer next will point to the next node in the linked list (or be NULL 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 NULL.
class Queue
Data members
This class should have two data members, both pointers to QNodes. The pointer qFront will point to the front node in the queue (or be NULL if the queue is empty); the pointer qRear will point to the rear node in the queue (or be NULL if the queue is empty).
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 NULL.
• 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 NULL before you attempt to insert any nodes into it.
• operator=
The 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 the Implementation 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 and setting the front and rear pointers back to NULL.
• size()
This method takes no arguments and returns an integer. It should return the current size of the queue; i.e., the number of data items currently stored in the queue. Since this is not stored as a data member of the queue class, you will have to traverse the linked list and count the nodes.
• 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 the template parameter type. If the queue is empty, this method should throw an out_of_range exception. Otherwise, it should return the data stored in the front node of the queue.
• back()
This method takes no arguments and returns the template parameter type. If the queue is empty, this method should throw an out_of_range 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.
• pop()
This method takes no arguments and returns nothing. If the queue is empty, this method should throw an out_of_range exception. Otherwise, it should remove the item at the front of the queue.
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 is provided for this program. The purpose of a driver program is to test other pieces that you code. You do not need to write the driver program yourself.
/*********************************************************************
FUNCTION: This program tests the functionality of the Queue
template class.
*********************************************************************/
#include "Queue.h"
#include
#include
using std::cout;
using std::endl;
using std::out_of_range;
int main()
{
cout << "Testing default constructor ";
Queue q1;
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q1 is " << ((q1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing push() ";
q1.push(17);
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q1 is " << ((q1.empty()) ? "empty " : "not empty ");
cout << endl;
q1.push(2);
q1.push(6);
q1.push(4);
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q1 is " << ((q1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing copy constructor ";
Queue q2(q1);
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q2 (size " << q2.size() << "): " << q2 << endl << endl;
cout << "Testing clear() ";
q1.clear();
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q2 (size " << q2.size() << "): " << q2 << endl << endl;
Queue q3;
q3.push(36);
q3.push(41);
q3.push(75);
q3.push(28);
cout << "q3 (size " << q3.size() << "): " << q3 << endl << endl;
cout << "Testing assignment operator ";
Queue q4;
q4 = q3;
cout << "q3 (size " << q3.size() << "): " << q3 << endl;
cout << "q4 (size " << q4.size() << "): " << q4 << endl << endl;
q3.clear();
cout << "q3 (size " << q3.size() << "): " << q3 << endl;
cout << "q4 (size " << q4.size() << "): " << q4 << endl << endl;
cout << "Testing assignment to self ";
q4 = q4;
q3 = q4;
q4.clear();
cout << "q3 (size " << q3.size() << "): " << q3 << endl;
cout << "q4 (size " << q4.size() << "): " << q4 << endl << endl;
cout << "Testing chained assignment ";
Queue q5;
q5 = q4 = q3;
cout << "q3 (size " << q3.size() << "): " << q3 << endl;
cout << "q4 (size " << q4.size() << "): " << q4 << endl;
cout << "q5 (size " << q5.size() << "): " << q5 << endl << endl;
cout << "Testing front(), push(), pop() ";
Queue q6, q7;
for(char c = 'a'; c < 'k'; c++)
q6.push(c);
cout << "q6 (size " << q6.size() << "): " << q6 << endl << endl;
for(int i = 0; i < 10; i++)
{
int val;
val = q6.front();
q7.push(val);
q6.pop();
}
cout << "q6 (size " << q6.size() << "): " << q6 << endl;
cout << "q7 (size " << q7.size() << "): " << q7 << endl << endl;
cout << "Testing back() ";
int val1, val2;
q6 = q7;
val1 = q6.back();
val2 = q7.back();
val1 = q6.back(); // Make sure that back() doesn't remove a value.
cout << ((val1 == val2) ? "back() works " : "back() failure ");
cout << "Testing front() ";
val1 = q6.front();
val2 = q7.front();
val1 = q6.front(); // Make sure that front() doesn't remove a value.
cout << ((val1 == val2) ? "front() works " : "front() failure ");
cout << "Testing const correctness ";
q7.clear();
const Queue& r6 = q6;
cout << "q6 (size " << r6.size() << "): " << r6 << endl;
cout << "q6 is " << ((r6.empty()) ? "empty " : "not empty ");
cout << "Front item of q6: " << r6.front() << endl;
cout << "Back item of q6: " << r6.back() << endl << endl;
q7 = r6;
Queue q8(r6);
cout << "q7 (size " << q7.size() << "): " << q7 << endl;
cout << "q8 (size " << q8.size() << "): " << q8 << endl << endl;
cout << "Testing exception handling ";
try
{
cout << q1.front() << endl;
}
catch (out_of_range orex)
{
cout << "Exception: "<< orex.what() << endl << endl;
}
try
{
cout << q1.back() << endl;
}
catch (out_of_range orex)
{
cout << "Exception: "<< orex.what() << endl << endl;
}
try
{
q1.pop();
}
catch (out_of_range orex)
{
cout << "Exception: "<< orex.what() << endl;
}
return 0;
}
Implementation Hints
• 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.
#ifndef QUEUE_H
#define QUEUE_H
#include
#include
template
struct QNode
{
...
};
// Method definitions for the QNode class
// Forward declaration of the Queue template class
template
class Queue;
// Forward declaration of the operator<< template function
template
std::ostream& operator<<(std::ostream&, const Queue&);
template
class Queue
{
// friend declaration for the template function - note the
// special syntax
friend std::ostream& operator<< <>(std::ostream&, const Queue&);
...
};
// Method definitions for the Queue class
#endif /* QUEUE_H */
Other Points
• Note that the driver program assumes that your header file is called Queue.h. If you name your header file differently, you'll need to modify the driver program accordingly.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct queue
{
int data;
struct queue *next;
}*p,*rear=NULL,*front=NULL,*temp;
template<class t>
class queue
{
public:
void insert(void);
void del(void);
void display(void);
};
template<class t>
void queue<t>::insert(void)
{
int x;
cout<<"enter element to be inserted:";
cin>>x;
p=new queue;
p->data=x;
p->next=NULL;
if(front==NULL)
{
front=p;
rear=p;
}
else
{
rear->next=p;
rear=p;
}
}
template<class t>
void queue<t>::del(void)
{
if(front==NULL)
{
cout<<"queue underflows "<<endl;
}
else
{
cout<<"deleted ele is:";
cout<front->data;
front=front->next;
}
}
template<class t>
void queue<t>::display(void)
{
if(front==NULL)
cout<<"queue empty";<<endl;
else
{
temp=front;
cout<<"ele in the queue are:";
while(temp!=NULL)
{
cout<<temp->data;
cout<<"->";
temp=temp->next;
}
}
}
int main()
{
clrscr();
int ch;
queue<int>q;
do
{
cout<<" menu 1.insert 2.delet 3.display";
cout<<"enter choice:";
cin>>ch;
switch(ch)
{
case 1:q.insert();break;
case 2:q.del();break;
case 3:q.display();break;
case 4:exit(0);
}
while(ch!=4)
}
getch();
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.