An interesting C++ student queue problem that illustrates inheritance from a template class list ... ( also an insertion sort of a short queue/list ... and iterators via template class Iter )
// classQueue_test2.cpp //
// an interesting C++ student 'queue' problem that illustrates inheritance from
// a template class list ... ( and insertion sort of a short queue/list ... and
// ... iterators via template class Iter )
/*
A student has an assignment to create a Queue class that uses inheritance,
given the base class as a supplied starting point ... a template
LinkedList class and Node class...
Your Queue class, aa all the other classes, are to be template classes.
Your test program first creates a queue object to hold customer names that
are waiting in line for service.
Comment your code where appropriate, paying particular attention to any
functions called in the test program.
(1) Display a heading like ...
Author: Your Name
Program title: Your Program Title
(2) Prompt and accept user input for customer names to be placed in the queue:
"Jane", "Bob", "Nancy", "Tom", "Julie", "Joe"
(3) Display the names of the customers waiting in line;
(4) Identify and process the next three people in line; i.e. display and remove
the first three names in the queue ... (using the Linked List member
functions that were inherited from the base class)
(5) Add the following names to the end of the queue
Lara
Andrew
(6) Add/insert "Wendy" as the 3rd node in the queue/list
(7) Display the new queue/list from head to tail ... same as (3) above.
(8) Display the name of the next customer in line to be served.
*/
#include <iostream>
#include <string>
using namespace std;
// set to '2 'or '1' and recompile to use LinkedList_iter.h or LinkedList.h
#define use_given_h 2
#if( use_given_h == 2 )
#define text "linkedlist_iter.h"
#include text
#else
#define text "linkedlist.h"
#include text
#endif
#ifndef QUEUE_H
#define QUEUE_H
template< typename T >
class Queue : public List< T >
{
public:
Queue() : List< T >() { cout << "Queue ctor called ...\n"; } // call list default ctor
~Queue() { cout << "Queue dtor called ...\n"; } // dtor ...
T dequeue(); // pop list ...
void enqueue( T element ); // push_back list ...
void show() const;
void show( int n ) const;
};
template< typename T >
void Queue< T >::enqueue( T element ) { push_back( element ); }
template< typename T >
T Queue< T >::dequeue() { return this->pop_front(); }
template< typename T >
void Queue< T >::show() const
{
#if( use_given_h < 2 )
Node< T >* cur = this->head;
for( int i = 0; cur != NULL; cur = cur->next )
cout << ++i << ": " << cur->element << endl;
#else
Iter< T > it;
int i = 0;
for( it = this->begin(); it != this->end(); ++it )
cout << ++i << ": " << *it << endl;
#endif
}
template< typename T >
void Queue< T >::show( int n ) const
{
#if( use_given_h < 2 )
Node< T >* cur = this->head;
int i;
for( i = 0; cur != NULL && i < n; cur = cur->next )
cout << ++i << ": " << cur->element << endl;
if( cur == NULL && i < n ) cout << "Only " << i << " in queue ...\n";
#else
Iter< T > it;
int i = 0;
for( it = this->begin(); it != this->end() && i < n; ++it )
cout << ++i << ": " << *it << endl;
if( it == this->end() && i < n ) cout << "Only " << i << " in queue ...\n";
#endif
}
#endif // #ifndef QUEUE_H
void wait( const string& prompt )
{
cout << prompt << flush;
string dummy;
getline( cin, dummy );
}
void testItOut()
{
// construct an empty Queue object ...
Queue < string > q;
wait( "\nPress 'Enter' to continue ... " );
// for this simple test program, initial names supplied via an array of names
const string ary[] = { "Jane", "Bob", "Nancy", "Tom", "Julie", "Joe" };
for( size_t i = 0; i < sizeof ary/sizeof ary[0]; ++i )
{
//cout << "Enter name " << i+1: << ": " << flush;
//string tmp;
//getline( cin, tmp );
//q.enqueue( tmp );
q.enqueue( ary[i] ); // add names to end of queue ... (end of list)
}
cout << "Author : Name" << endl;
cout << "Title : Queue and Linked List Test Program" << endl;
cout << "===========================================" << endl;
cout << "Showing initial queue ...\n";
q.show();
cout << "Removing/showing first 3 from queue now ...\n";
int i = 0, count = 3;
while( count-- && q.get_size() )
cout << "Remove " << ++i << ": " << q.pop_front() << endl;
q.push_back( "Lara" ); // adding at end ... (i.e. ... push_back)
q.push_back( "Andrew" );
q.insert( 3, "Wendy" ); // insert at position 3 ...
cout << "After 2 push_back's & inserting 'Wendy' at 3rd position, the queue is ...\n";
q.show();
cout << "Showing the next customer to be served ...\n";
cout << q.get_front();
wait( "\n\nPress 'Enter' to continue ... " );
// added ...
cout << "\nShowing the next 10 customers to be served ...\n";
q.show( 10 );
wait( "\nPress 'Enter' to continue ... " );
cout << endl;
cout << "Showing the next customers to be served in sorted order ...\n";
q.isort();
q.show();
//wait( "\nPress 'Enter' to continue ... " );
q.insert_sorted( "Mariam" );
cout << "\nShowing added customer 'Mariam' to be served in sorted order ...\n";
q.show();
wait( "\nPress 'Enter' to continue ... " );
q.insert_sorted( "Adam" );
cout << "\nShowing added customer 'Adam' to be served in sorted order ...\n";
cout << "The first in queue now is: " << q.get_front() << endl
<< "The last in queue now is: " << q.get_back() << endl;
q.insert_sorted( "Zoe" );
cout << "\nShowing added customer 'Zoe' to be served in sorted order ...\n";
cout << "The first in queue now is: " << q.get_front() << endl
<< "The last in queue now is: " << q.get_back() << endl;
wait( "\nPress 'Enter' to continue ... " );
cout << "\nTraversing queue with Node pointer ... \n";
Node< string >* p = q.get_head();
for( ; p != NULL; p = p->get_next() )
cout << p->get_element() << " ";
cout << endl << endl;
#if ( use_given_h == 2 )
cout << "Traversing queue with Iter ...\n";
Iter< string > it = q.begin();
for( ; it != q.end(); ++it )
cout << *it << " ";
cout << "\n\nIn sorted order now ... ";
#endif
cout << "Serving the last " << q.get_size() << " customers ... \n";
i = 0;
while( q.get_size() )
cout << "Remove " << ++i << ": " << q.pop_front() << endl;
wait( "\nAll done! Press 'Enter' to continue ... " );
}
int main()
{
cout << "Using ... " << text << endl << endl;
testItOut();
wait( "\nPress 'Enter' to exit ... " );
}