[C++] Stack class using singly-linked list

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++] Stack class using singly-linked list

Post by ayu »

This is a stack class I wrote once that uses a singly-linked list. The linked list used is provided on this forum.

Code: Select all

#ifndef STACK_H
#define STACK_H

#include "LinkedList.h"

template<class TemplateItem>
class Stack
{
public:
   Stack();
   virtual ~Stack();
   Stack(const Stack& obj);
   Stack& operator=(const Stack& obj);

   bool push(TemplateItem);
   bool pop();
   bool empty();
   TemplateItem top();

private:
   LinkedList<TemplateItem> mStackList;

};



/****Function definitons****/


template<class TemplateItem>
Stack<TemplateItem>::Stack()
{
}

template<class TemplateItem>
Stack<TemplateItem>::~Stack()
{
}

template<class TemplateItem>
Stack<TemplateItem>& Stack<TemplateItem>::operator=(const Stack& obj)
{
   mStackList = obj.mStackList;

   return *this;
}

template<class TemplateItem>
Stack<TemplateItem>::Stack(const Stack& obj)
{
   mStackList = obj.mStackList;
}

template<class TemplateItem>
bool Stack<TemplateItem>::push(TemplateItem item)
{
   return mStackList.insertFirst(item)

}

template<class TemplateItem>
bool Stack<TemplateItem>::pop()
{
   return mStackList.removeFirst();
}

template<class TemplateItem>
bool Stack<TemplateItem>::empty()
{
   return mStackList.size() > 0;
}
   
template<class TemplateItem>
TemplateItem Stack<TemplateItem>::top()
{
   return mStackList.elementAt(0);
}
#endif
"The best place to hide a tree, is in a forest"

Post Reply