Stack and Queue

Build a Stack and a Queue class based on the linked list functions you have built.

No "linked list code" should be visible inside these classes. If you need something done, call a function. If you don't have a function, write it.

Don't forget about your Big 3 Functions!

 

_copy_list() function

Add the following _copy_list function to your linked list library. This function will take the source and destination head pointers. Copies the source to destination list and returns the address of the last node in destination. 

You will use this function in your Queue's copy constructor and assignment operator. You do not want to travel the entire length of the queue to find the rear. In fact, at no point in your project will you travel the length of the queue to find the rear.

 

 

    //duplicate list and return the last node of the copy
    template <typename T>
    node<T> *_copy_list(node<T>* &dest, node<T> *src);

Queue:

 

 

template <typename T>
class Queue
{
public:
    class Iterator{
    public:
        friend class Queue;          //give access to list to access _ptr
        Iterator();                  //default ctor
        Iterator(node<T>* p);        //Point Iterator to where
        //...                        //  p is pointing to
 
    private:
        node<T>* _ptr;               //pointer being encapsulated
    };

    Queue();

    Queue(const Queue<T>& copyMe);
    ~Queue();
    Queue& operator=(const Queue<T>& RHS);

    bool empty();
    T front();
    T back();

    void push(T item);
    T pop();

    Iterator begin() const;            //Iterator to the head node
    Iterator end() const;              //Iterator to NULL
    void print_pointers();
    int size() const { return _size; }
    template<typename TT>
    friend ostream& operator << (ostream& outs, const Queue<TT>& printMe);
private:
    node<T>* _front;
    node<T>* _rear;
    int _size;
};

 

 

stack:

 

 

template <typename T>
class Stack{
public:
    class Iterator{
    public:
        friend class Stack;                     //give access to list to access _ptr
        Iterator(){_ptr = NULL;}                //default ctor
        Iterator(node<T>* p){_ptr =p;}          //Point Iterator to where
                                                //  p is pointing to

    private:
        node<T>* _ptr;                          //pointer being encapsulated
    };

    Stack();
    Stack(const Stack<T>& copyMe);
    ~Stack();
    Stack<T>& operator=(const Stack<T>& RHS);
    T top();
    bool empty();
    void push(T item);
    T pop();
    template<typename TT>
    friend ostream& operator<<(ostream& outs, 
const Stack<TT>& printMe);
Iterator begin() const; //Iterator to the head node Iterator end() const; //Iterator to NULL int size() const { return _size; } private: node<T>* _top; int _size; };