Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this assignment for a c++ data structures class. I just need to create so

ID: 665519 • Letter: I

Question

I have this assignment for a c++ data structures class. I just need to create some methods and implement them. I also need a test driver to test the code. Here is the assignment:

Working with Lists

1. Create an overload of the insert method that appends the items of one list to another.

void List::insert( const List & list );

2. Create an overloaded method on the List class that sorts the items in the list. The first overload, sort(), sorts the items in ascending order; the second, sorts them in the order specified by a comparator function, e.g., in descending order. (Extra Credit: 5 points)

void List::sort();

void List::sort(bool (*comparator)( T &, T & ));

Working with Queues

1. Create a method on the Queue class that calculates the number of times a given value occurs in the queue. int Queue::count( const T & data ) const;

Example: count(…) function

2. Create a method on the Queue class that determines whether the queue is in descending order.

bool Queue::isDecreasing( ) const;

Example: isDecreasing() function

Working with Stacks

1. Create a method on the Stack class that determines whether a given value occurs consecutively in the stack. bool Stack::isConsecutive( const T & data ) const;

Example: isConsecutive(…) function

2. Create a method on the Stack class that reverses the values on the stack.

void Stack::reverse();

Example: reverse() function

Here is the code I have:

BadIterator.h

#ifndef BADITERATOR_H
#define BADITERATOR_H
#include <iostream>
#include <string>

namespace cs20 {

class BadIterator : public std::logic_error {
public:
   BadIterator( const std::string& msg = "" );
};

}

#endif

baditerator.cpp

#include "BadIterator.h"

namespace cs20 {

BadIterator::BadIterator( const std::string& msg ) : std::logic_error( msg.c_str() ) {}

}

emptyqueue.h

#ifndef EMPTYQUEUE_H
#define EMPTYQUEUE_H
#include <iostream>
#include <string>

namespace cs20 {

class EmptyQueue : public std::logic_error {
public:
   EmptyQueue( const std::string& msg = "" );
};

}
#endif

EmptyQueue.cpp

#include "EmptyQueue.h"

namespace cs20 {

EmptyQueue::EmptyQueue( const std::string& msg ) : std::logic_error( msg.c_str() ) {}

}

EmptyStack.h

#ifndef EMPTYSTACK_H
#define EMPTYSTACK_H
#include <iostream>
#include <string>

namespace cs20 {

class EmptyStack : public std::logic_error {
public:
   EmptyStack( const std::string& msg = "" );
};

}

#endif

EmptyStack.cpp

#include "EmptyStack.h"

namespace cs20 {

EmptyStack::EmptyStack( const std::string& msg ) : std::logic_error( msg.c_str() ) {}

}

List.h

#ifndef LIST_H

#define LIST_H
#include <iostream>
#include "ListNode.h"
#include "ListIterator.h"

namespace cs20 {

template <class Object>
class List {
public:
   List();
   List( const List& rhs );
   ~List();

   bool isEmpty() const;
   void makeEmpty();
   ListIterator<Object> zeroth() const;
   ListIterator<Object> first() const;
   void insert( const Object& data,
               const ListIterator<Object> &iter );
   void insert( const Object& data );
   ListIterator<Object> findPrevious( const Object& data ) const;
   void remove( const Object& data );
  
   const List& operator =( const List& rhs );
private:
   ListNode<Object> * head;
  
};

}
#endif

List.cpp

#ifndef LIST_CPP
#define LIST_CPP

#include "List.h"

namespace cs20 {
template <class Object>
List<Object>::List() {
   head = new ListNode<Object>;
}

template <class Object>
List<Object>::List( const List<Object>& rhs ) {
   head = new ListNode<Object>;
   *this = rhs;
}

template <class Object>
List<Object>::~List() {
   makeEmpty();
delete head;
}

template <class Object>
bool List<Object>::isEmpty() const {
   return( head->nextIsNull() );
}

template <class Object>
void List<Object>::makeEmpty() {
   while (!isEmpty()) {
       remove( first().retrieve() );
   }
}

template <class Object>
ListIterator<Object> List<Object>::zeroth() const {
   return( ListIterator<Object>( head ) );
}

template <class Object>
ListIterator<Object> List<Object>::first() const {
return( ListIterator<Object>( head->getNext() ) );
}

template <class Object>
void List<Object>::insert( const Object& data,
                        const ListIterator<Object> &iter ) {
   if (iter.isValid()) {
       ListNode<Object>* newnode = new ListNode<Object>( data, iter.current->getNext() );
       iter.current->setNext( newnode );
   }
}

template <class Object>
void List<Object>::insert( const Object& data ) {
   // insert after the header node
   ListNode<Object>* newnode = new ListNode<Object>( data, head->getNext() );
   head->setNext( newnode );
}

template <class Object>
ListIterator<Object> List<Object>::findPrevious( const Object& data ) const {
   ListNode<Object>* node = head;
   while( node->getNext() != NULL && node->getNext()->getElement() != data ) {
       node = node->getNext();
   }
   if (node->getNext() == NULL) {
       node = NULL;
   }
   return ListIterator<Object>( node );
}

template <class Object>
void List<Object>::remove( const Object& data ) {
   ListIterator<Object> iter = findPrevious( data );
   if (iter.isValid()) {
       ListNode<Object>* node = findPrevious( data ).current;
       if (node->getNext() != NULL) {
           ListNode<Object> *oldNode = node->getNext();
           node->setNext( node->getNext()->getNext() ); // Skip oldNode
           delete oldNode;
       }
   }
}

// Deep copy of linked list
template <class Object>
const List<Object>& List<Object>::operator =( const List<Object>& rhs ) {
   if (this != &rhs) {
       makeEmpty();
      
       ListIterator<Object> rightiter = rhs.first( );
       ListIterator<Object> myiterator = zeroth();
       while( rightiter.isValid() ) {
           insert( rightiter.retrieve(), myiterator );
           rightiter.advance();
           myiterator.advance();
       }
   }
   return( *this );
}

}

#endif

ListIterator.h

#ifndef LISTITERATOR_H
#define LISTITERATOR_H
#include <iostream>
#include "ListNode.h"
#include "BadIterator.h"

namespace cs20 {

template <class Object>
class List;                   // forward declaration

template <class Object>
class ListIterator {
public:
   ListIterator();
   bool isValid() const;
   void advance();
   const Object& retrieve() const;
  
private:
   ListNode<Object> * current;
  
   ListIterator( ListNode<Object> *node );

   // List exposes ListIterator instances via public methods
   // no external access should be given to the current pointer
   // friend access is required by List class to ensure this information hiding
   friend class List<Object>;
};

}
#endif

ListIterator.cpp

#ifndef LISTITERATOR_CPP
#define LISTITERATOR_CPP

#include "ListIterator.h"

namespace cs20 {
template <class Object>
ListIterator<Object>::ListIterator() : current( NULL ) {
   // all assignments occurred above
}

template <class Object>
ListIterator<Object>::ListIterator( ListNode<Object> *node ) : current( node ) {
   // all assignments occurred above
}

template <class Object>
bool ListIterator<Object>::isValid( ) const {
   return( (current != NULL) );
}


template <class Object>
void ListIterator<Object>::advance( ) {
   if (isValid()) {
       current = current->getNext();
   }
}

template <class Object>
const Object& ListIterator<Object>::retrieve( ) const {
   if (!(isValid())) throw BadIterator();
   return( current->getElement() );
}
}

#endif

ListNode.h

#define LISTNODE_H
#include <iostream>

namespace cs20 {

template <class Object>
class ListNode {
public:
   ListNode( const Object& theElement = Object(), ListNode * node = NULL );
   bool nextIsNull() const;

   // these accessors and mutators are called
   // from ListIterator and List classes
   // no public methods expose ListNode instances
   // outside ListIterator and List classes
   const Object& getElement() const;
   ListNode* getNext() const;
   void setNext( ListNode * node );

private:
   Object element;
   ListNode* next;

};

}
#endif

ListNode.cpp

#ifndef LISTNODE_CPP
#define LISTNODE_CPP

#include "ListNode.h"

namespace cs20 {

template <class Object>
ListNode<Object>::ListNode( const Object& theElement,
                               ListNode<Object> * node ) {
   element = theElement;
   next = node;
}

template <class Object>
bool ListNode<Object>::nextIsNull() const {
   return( next == NULL );
}

template <class Object>
const Object& ListNode<Object>::getElement() const {
   return( element );
}

template <class Object>
ListNode<Object>* ListNode<Object>::getNext() const {
   return( next );
}

template <class Object>
void ListNode<Object>::setNext( ListNode<Object> * node ) {
   next = node;
}

}

#endif

Queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include "QueueNode.h"
#include "EmptyQueue.h"

namespace cs20 {

template <class Object>
class Queue {
public:
   Queue();
   Queue( const Queue& rhs );
   ~Queue();

   bool isEmpty() const;
   void makeEmpty();
   void enqueue( const Object& data );
   const Object dequeue();
   const Object& getFront() const;
  
   const Queue& operator =( const Queue& rhs );

   std::ostream& printQueue( std::ostream& outs );
private:
   QueueNode<Object> * frontNode;
   QueueNode<Object> * backNode;
};

}
#endif

Queue.cpp

#ifndef QUEUE_CPP
#define QUEUE_CPP

#include "Queue.h"

namespace cs20 {

template <class Object>
Queue<Object>::Queue() {
   frontNode = NULL;
   backNode = NULL;
}

template <class Object>
Queue<Object>::Queue( const Queue<Object>& rhs ) {
   frontNode = NULL;
   backNode = NULL;
   *this = rhs;
}

template <class Object>
Queue<Object>::~Queue() {
   makeEmpty();
   // when empty, frontNode and backNode will already be null
}

template <class Object>
bool Queue<Object>::isEmpty() const {
   return( (frontNode == NULL) );
}

template <class Object>
void Queue<Object>::makeEmpty() {
   while (!isEmpty()) {
       dequeue();
   }
}

template <class Object>
void Queue<Object>::enqueue( const Object& data ) {
   QueueNode<Object>* newNode = new QueueNode<Object>( data );
   if (isEmpty()) {
       frontNode = newNode;
       backNode = newNode;
   }
   else {
       backNode->setNext( newNode );
       backNode = backNode->getNext();
   }
}

template <class Object>
const Object Queue<Object>::dequeue() {
   Object frontData = getFront();

   QueueNode<Object> * oldFront = frontNode;
   frontNode = frontNode->getNext();
   delete oldFront;
   return( frontData );
}

template <class Object>
const Object& Queue<Object>::getFront() const {
   if (isEmpty()) {
       throw EmptyQueue();
   }
   return( frontNode->getElement() );
}

// Deep copy of linked Queue
template <class Object>
const Queue<Object>& Queue<Object>::operator =( const Queue<Object>& rhs ) {
   if (this != &rhs) {
       makeEmpty();
       QueueNode<Object> * rhsFrontNode = rhs.frontNode;
       while( rhsFrontNode != NULL) {
           enqueue( rhsFrontNode->getElement() );
           rhsFrontNode = rhsFrontNode->getNext();
       }
   }
   return( *this );
}

template <class Object>
std::ostream& Queue<Object>::printQueue( std::ostream& outs ) {
   if (isEmpty()) {
       outs << "Empty Queue";
   }
   else {
       outs << "FRONT: ";
       QueueNode<Object> * node = frontNode;
       while( node != NULL) {
           outs << node->getElement();
           outs << " ";
           node = node->getNext();
       }
       outs << ":BACK";
   }
   return( outs );
}
  
}
#endif

QueueNode.h

#ifndef QUEUENODE_H
#define QUEUENODE_H
#include <iostream>

namespace cs20 {

template <class Object>
class QueueNode {
public:
   QueueNode( const Object& theElement = Object(), QueueNode * node = NULL );

   // these accessors and mutators are called
   // from Queue class
   // no public methods expose QueueNode instances
   // outside the Queue class
   const Object& getElement() const;
   QueueNode* getNext() const;
   void setNext( QueueNode * node );

private:
   Object element;
   QueueNode* next;

};

}
#endif

QueueNode.cpp

#ifndef QUEUENODE_CPP
#define QUEUENODE_CPP

#include "QueueNode.h"

namespace cs20 {

template <class Object>
QueueNode<Object>::QueueNode( const Object& theElement,
                           QueueNode<Object> * node ) {
   element = theElement;
   next = node;
}

template <class Object>
const Object& QueueNode<Object>::getElement() const {
   return( element );
}

template <class Object>
QueueNode<Object>* QueueNode<Object>::getNext() const {
   return( next );
}

template <class Object>
void QueueNode<Object>::setNext( QueueNode<Object> * node ) {
   next = node;
}

}

#endif

Stack.h

#ifndef STACK_H
#define STACK_H
#include <iostream>
#include "StackNode.h"
#include "EmptyStack.h"

namespace cs20 {

template <class Object>
class Stack {
public:
   Stack();
   Stack( const Stack& rhs );
   ~Stack();

   bool isEmpty() const;
   void makeEmpty();
   void push( const Object& data );
   void pop();
   const Object& top() const;
   Object topAndPop();
   std::ostream& printStack( std::ostream& outs ) const;
  
   const Stack& operator =( const Stack& rhs );

private:
   StackNode<Object> * topNode;
};

}

#endif

Stack.cpp

#ifndef STACK_CPP
#define STACK_CPP

#include "Stack.h"

namespace cs20 {

template <class Object>
Stack<Object>::Stack() {
   topNode = NULL;
}

template <class Object>
Stack<Object>::Stack( const Stack<Object>& rhs ) {
   topNode = NULL;
   *this = rhs;
}

template <class Object>
Stack<Object>::~Stack() {
   makeEmpty();
delete topNode;
}

template <class Object>
bool Stack<Object>::isEmpty() const {
   return( (topNode == NULL) );
}

template <class Object>
void Stack<Object>::makeEmpty() {
   while (!isEmpty()) {
       pop();
   }
}

template <class Object>
void Stack<Object>::push( const Object& data ) {
   StackNode<Object>* newNode = new StackNode<Object>( data, topNode );
   topNode = newNode;
}

template <class Object>
void Stack<Object>::pop() {
   if (isEmpty()) {
       throw EmptyStack();
   }
   StackNode<Object> *oldTop = topNode;
   topNode = topNode->getNext();
   delete oldTop;
}

template <class Object>
const Object& Stack<Object>::top( ) const {
   if (isEmpty()) {
       throw EmptyStack();
   }
   StackNode<Object> node = *topNode;
   return( node.getElement() );
}

template <class Object>
Object Stack<Object>::topAndPop( ) {
   Object o = top();
   pop();
   return( o );
}

// Deep copy of linked Stack
template <class Object>
const Stack<Object>& Stack<Object>::operator =( const Stack<Object>& rhs ) {
   if (this != &rhs) {
       makeEmpty();
       if (!(rhs.isEmpty())) {
           StackNode<Object> * rhsTopNode = rhs.topNode;
           StackNode<Object> * myTopNode = new StackNode<Object>( rhsTopNode->getElement() );
           topNode = myTopNode;

           rhsTopNode = rhsTopNode->getNext();
           while (rhsTopNode != NULL) {
               myTopNode->setNext( new StackNode<Object>( rhsTopNode->getElement() ) );
               myTopNode = myTopNode->getNext();
               rhsTopNode = rhsTopNode->getNext();
           }
       }
   }
   return( *this );
}

template <class Object>  
std::ostream& Stack<Object>::printStack( std::ostream& outs ) const {
   if (isEmpty()) {
       outs << "Empty Stack";
   }
   else {
       outs << "TOP: ";
       StackNode<Object> * node = topNode;
       while (node != NULL) {
           outs << node->getElement();
           outs << " "; /// for visual alignment
           node = node->getNext();
       }
   }
   return( outs );
}

}

#endif

StackNode.h

#ifndef STACKNODE_H
#define STACKNODE_H
#include <iostream>

namespace cs20 {

template <class Object>
class StackNode {
public:
   StackNode( const Object& theElement = Object(), StackNode * node = NULL );

   const Object& getElement() const;
   StackNode* getNext() const;
   void setNext( StackNode* node );
private:
   Object element;
   StackNode* next;

};

}
#endif

StackNode.cpp

#ifndef STACKNODE_CPP
#define STACKNODE_CPP

#include "StackNode.h"

namespace cs20 {

template <class Object>
StackNode<Object>::StackNode( const Object& theElement,
                           StackNode<Object> * node ) {
   element = theElement;
   next = node;
}

template <class Object>
const Object& StackNode<Object>::getElement() const {
   return( element );
}

template <class Object>
StackNode<Object>* StackNode<Object>::getNext() const {
   return( next );
}

template <class Object>
void StackNode<Object>::setNext( StackNode<Object>* node ) {
   next = node;
}

}

#endif

ListMenu.cpp

// Menu.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include "List.h"
#include "ListNode.h"
#include "ListIterator.h"
#include "List.cpp"
#include "ListNode.cpp"
#include "ListIterator.cpp"

using namespace std;
using namespace cs20;

enum CHOICE {MAKEEMPTY, REMOVE, ISEMPTY, FINDPREVIOUS, INSERT, QUIT, PRINT };

CHOICE menu();
void printList( const List<int>& l );

int main(int argc, char* argv[]) {
   int value;
   List<int> list;
   ListIterator<int> iter;

   CHOICE choice;
   do {
       choice = menu();
       switch( choice ) {
       case MAKEEMPTY:
           list.makeEmpty();
           break;
       case ISEMPTY:
           if (list.isEmpty()) {
               cout << "list is empty" << endl;
           }
           else {
               cout << "list is not empty" << endl;
           }
           break;
       case REMOVE:
           cout << "Please provide int to remove: ";
           cin >> value;
           list.remove( value );
           break;
       case INSERT:
           cout << "Please provide int to insert: ";
           cin >> value;
           list.insert( value );
           break;
       case FINDPREVIOUS:
           cout << "Please provide int to find: ";
           cin >> value;
           iter = list.findPrevious( value );
           if (iter.isValid()) {
               cout << "previous element = " << iter.retrieve() << endl;
           }
           else {
               cout << "data element was not found!" << endl;
           }
           break;
       case PRINT:
           printList( list );
           break;
   }  

   } while (choice != QUIT);

   return( 0 );
}

int sample() {
   cout << "Forming Lists" << endl;
   int two = 2;
   List<int> l1 = List<int>();
   List<int> l2 = List<int>();

   l1.insert( one );
   l1.insert( two );

   cout << "print l1" << endl;
   printList( l1 );

   cout << "l2 = l1" << endl;
   l2 = l1;

   cout << "print l2" << endl;
   printList( l2 );  

   cout << "l1.remove(one)" << endl;
   l1.remove( one );

   cout << "print l1" << endl;
   printList( l1 );

   cout << "print l2" << endl;
   printList( l2 );
   cout << "findPrevious 1 in l2" << endl;
   ListIterator<int> iter = l2.findPrevious( one );
   if (iter.isValid()) {
       cout << "--iter valid" << endl;
       cout << iter.retrieve() << endl;
   }
   else {
       cout << "--iter not valid" << endl;
   }

   cout << "findPrevious 2 in l2" << endl;
   iter = l2.findPrevious( two );
   if (iter.isValid()) {
       cout << "--iter valid" << endl;
       cout << iter.retrieve() << endl;
   }
   else {
       cout << "--iter not valid" << endl;
   }

   cout << "findPrevious 1 in l1" << endl;
   iter = l1.findPrevious( one );
   if (iter.isValid()) {
       cout << "--iter valid" << endl;
       cout << iter.retrieve() << endl;
   }
   else {
       cout << "--iter not valid" << endl;
   }

   cout << "findPrevious 2 in l1" << endl;
   iter = l1.findPrevious( two );
   if (iter.isValid()) {
       cout << "--iter valid" << endl;
       cout << iter.retrieve() << endl;
   }
   else {
       cout << "--iter not valid" << endl;
   }

   cout << "print l1" << endl;
   printList( l1 );  

       // you can remove whatever you want, whether it exists or not
   cout << "l1.remove(one)" << endl;
   l1.remove( one );

   cout << "print l1" << endl;
   printList( l1 );  

   return( 0 );
}

void printList( const List<int>& l ) {
   if (l.isEmpty())
       cout << "Empty list" << endl;
   else {
       ListIterator<int> iter = l.first();
       while (iter.isValid()) {
           cout << iter.retrieve() << " -> ";
           iter.advance();
       }
       cout << "NULL";
       cout << endl;
   }
}

CHOICE menu() {
   char choice;
   CHOICE result;
   cout << "(M)akeEmpty I(s)Empty (R)emove (I)nsert (F)indPrevious (P)rint (Q)uit: " << endl;
   cin >> choice;
   switch( choice ) {
   case 'M':
   case 'm':
       result = MAKEEMPTY;
       break;
   case 'S':
   case 's':
       result = ISEMPTY;
       break;
   case 'R':
   case 'r':
       result = REMOVE;
       break;
   case 'I':
   case 'i':
       result = INSERT;
       break;
   case 'F':
   case 'f':
       result = FINDPREVIOUS;
       break;
   case 'Q':
   case 'q':
       result = QUIT;
       break;
   case 'P':
   case 'p':
       result = PRINT;
       break;
   }

   return( result );
}

QueueMenu.cpp

// Menu.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include "Queue.h"
#include "EmptyQueue.h"
#include "QueueNode.h"
#include "Queue.cpp"
#include "QueueNode.cpp"

using namespace std;
using namespace cs20;

enum CHOICE {MAKEEMPTY, ENQUEUE, ISEMPTY, DEQUEUE, GETFRONT, QUIT, PRINT };

CHOICE menu();

int main(int argc, char* argv[]) {
   int value;
   Queue<int> q;

   CHOICE choice;
   do {
       choice = menu();
       switch( choice ) {
       case MAKEEMPTY:
           q.makeEmpty();
           break;
       case ISEMPTY:
           if (q.isEmpty()) {
               cout << "queue is empty" << endl;
           }
           else {
               cout << "queue is not empty" << endl;
           }
           break;
       case DEQUEUE:
           try {
               value = q.dequeue();
               cout << "Here's the value dequeued: ";
               cout << value << endl;
           } catch( EmptyQueue eq ) {
               cout << "You silly... don't try dequeueing an empty queue!" << endl;          
           }
           break;
       case GETFRONT:
           try {
               value = q.getFront();
               cout << "Here's the value returned from getFront: ";
               cout << value << endl;
           } catch( EmptyQueue eq ) {
               cout << "You silly... don't try gettingFront an empty queue!" << endl;
           }
           break;
       case ENQUEUE:
           cout << "Please provide int to enqueue: ";
           cin >> value;
           q.enqueue( value );
           break;
       case PRINT:
           q.printQueue( cout );
           cout << endl;
           break;
   }  

   } while (choice != QUIT);

   return( 0 );
}

CHOICE menu() {
   char choice;
   CHOICE result;

   cout << "(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (P)rint (Q)uit: " << endl;
   cin >> choice;
   switch( choice ) {
   case 'E':
   case 'e':
       result = ENQUEUE;
       break;
   case 'M':
   case 'm':
       result = MAKEEMPTY;
       break;
   case 'S':
   case 's':
       result = ISEMPTY;
       break;
   case 'D':
   case 'd':
       result = DEQUEUE;
       break;
   case 'G':
   case 'g':
       result = GETFRONT;
       break;
   case 'Q':
   case 'q':
       result = QUIT;
       break;
   case 'P':
   case 'p':
       result = PRINT;
       break;
   }

   return( result );
}

void sample() {
   Queue<int> q1;
   Queue<int> q2;
   cout << "making q1" << endl;
   q1.enqueue( 1 );
   q1.enqueue( 2 );
   cout << "print q1" << endl;
   q1.printQueue( cout );
   cout << endl;

   cout << "q2 = q1" << endl;

   q2 = q1;
   cout << "print q2" << endl;
   q2.printQueue( cout );
   cout << endl;

   q1.dequeue();
   cout << "dequeue q1" << endl;

   cout << "print q2" << endl;
   q2.printQueue( cout );
   cout << endl;

   cout << "print q1" << endl;
   q1.printQueue( cout );
   cout << endl;

}

StackMenu.cpp

// Menu.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include "Stack.h"
#include "EmptyStack.h"
#include "StackNode.h"
#include "Stack.cpp"
#include "StackNode.cpp"

using namespace std;
using namespace cs20;

enum CHOICE {MAKEEMPTY, PUSH, ISEMPTY, POP, TOP, TOPANDPOP, QUIT, PRINT };

CHOICE menu();

int main(int argc, char* argv[]) {
   int value;
   Stack<int> stack;

   CHOICE choice;
   do {
       choice = menu();
       switch( choice ) {
       case MAKEEMPTY:
           stack.makeEmpty();
           break;
       case ISEMPTY:
           if (stack.isEmpty()) {
               cout << "stack is empty" << endl;
           }
           else {
               cout << "stack is not empty" << endl;
           }
           break;
       case TOP:
           try {
               value = stack.top();
               cout << "Here's the value on top: ";
               cout << value << endl;
           } catch( EmptyStack es ) {
               cout << "You silly... don't try topping an empty stack!" << endl;          
           }
           break;
       case POP:
           try {
               stack.pop();
           } catch( EmptyStack es ) {
               cout << "You silly... don't try popping an empty stack!" << endl;
           }
           break;
       case PUSH:
           cout << "Please provide int to push: ";
           cin >> value;
           stack.push( value );
           break;
       case TOPANDPOP:
           try {
               value = stack.topAndPop();
               cout << "Here's the value on top that got popped: ";
               cout << value << endl;
           } catch( EmptyStack es ) {
               cout << "You silly... don't try topAndPopping an empty stack!" << endl;
           }
           break;
       case PRINT:
           stack.printStack( cout );
           cout << endl;
           break;
   }  

   } while (choice != QUIT);

   return( 0 );
}

CHOICE menu() {
   char choice;
   CHOICE result;
   cout << "(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op top(A)ndpop (P)rint (Q)uit: " << endl;
   cin >> choice;
   switch( choice ) {
   case 'A':
   case 'a':
       result = TOPANDPOP;
       break;
   case 'M':
   case 'm':
       result = MAKEEMPTY;
       break;
   case 'S':
   case 's':
       result = ISEMPTY;
       break;
   case 'U':
   case 'u':
       result = PUSH;
       break;
   case 'O':
   case 'o':
       result = POP;
       break;
   case 'T':
   case 't':
       result = TOP;
       break;
   case 'Q':
   case 'q':
       result = QUIT;
       break;
   case 'P':
   case 'p':
       result = PRINT;
       break;
   }

   return( result );
}

void sample() {
   Stack<int> s1;
   Stack<int> s2;
   cout << "making s1" << endl;
   s1.push( 1 );
   s1.push( 2 );
   cout << "print s1" << endl;
   s1.printStack( cout );
   cout << endl;

   cout << "s2 = s1" << endl;

   s2 = s1;
   cout << "print s2" << endl;
   s2.printStack( cout );
   cout << endl;

   s1.pop();
   cout << "pop s1" << endl;

   cout << "print s2" << endl;
   s2.printStack( cout );
   cout << endl;

   cout << "print s1" << endl;
   s1.printStack( cout );
   cout << endl;

}

Result Queue<int> count (5) 3 front->(1) (1) (5) (3) (5) (5) (2)<-back count (10) 0 front->(1) (1) (5) (3) (5) (5) (5) (2)<-back

Explanation / Answer

1.

// program to insert into a list
#include <iostream>
#include <list>
#include <vector>

int main ()
{
std::list<int> mylist;
std::list<int>::temperator temp;

// set some intempial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5

temp = mylist.begin();
++temp;       // temp points now to number 2           ^

mylist.insert (temp,10);                        // 1 10 2 3 4 5

// temp still points to number 2                      ^
mylist.insert (temp,2,20);                      // 1 10 20 20 2 3 4 5

--temp;       // temp points now to the second 20            ^

std::vector<int> myvector (2,30);
mylist.insert (temp,myvector.begin(),myvector.end());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
std::cout << "mylist contains:";
for (temp=mylist.begin(); temp!=mylist.end(); ++temp)
    std::cout << ' ' << *temp;
std::cout << ' ';

return 0;
}