(2011.07.20) 체인스택//Link list-based stack implementation

1223 단어 nulldeleteClass
//    
// Link list-based stack implementation
template <class Elem>
class LStack: public Stack<Elem>
{
	private:
		Link<Elem>* top;			// Pointer to first element
		int size;					// Count number of elements
	public:
		LStack(int sz = DefaultListSize) { top = NULL; size = 0;}
		~LStack()	{ clear();}		// Destructor
		void clear()				// Delete link nodes
		{
			while(top != NULL)
			{
				Link<Elem>* temp = top;
				top = top -> next;
				size = 0;
				delete temp;
			}
		}
		bool push (const Elem& item)
		{
			top = new Link<Elem>(item, top);
			size++;
			return true;
		}
		bool pop(Elem& it)
		{
			if (size == 0) return false;
			it = top -> element;
			Link<Elem>* ltemp = top -> next;
			delete top;
			top = ltemp;
			size--;
			return true;
		}
		bool topValue(Elem& it)const
		{
			if(size == 0) return false;
			it = top -> element;
			return true;
		}
		int length() const { return size; }
};

좋은 웹페이지 즐겨찾기