本文件为C++数据结构源代码

源代码在线查看: lstack.h

软件大小: 99 K
上传用户: lvuxinwu
关键词: 数据结构 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				// file lstack.h
				// linked stack
				#ifndef LinkedStack_
				#define LinkedStack_
				
				#include "node.h"
				#include "xcept.h"
				
				template
				class LinkedStack {
				   public:
				      LinkedStack() {top = 0;}
				      ~LinkedStack();
				      bool IsEmpty() const {return top == 0;}
				      bool IsFull() const;
				      T Top() const;
				      LinkedStack& Add(const T& x);
				      LinkedStack& Delete(T& x);
				   private:
				      Node *top; // pointer to top node
				};
				
				template
				LinkedStack::~LinkedStack()
				{// Stack destructor..
				   Node *next;
				   while (top) {
				      next = top->link;
				      delete top;
				      top = next;
				      }
				}
				
				template
				bool LinkedStack::IsFull() const
				{// Is the stack full?
				   try {Node *p = new Node;
				        delete p;
				        return false;}
				   catch (NoMem) {return true;}
				}
				
				template
				T LinkedStack::Top() const
				{// Return top element.
				   if (IsEmpty()) throw OutOfBounds();
				   return top->data;
				}
				
				template
				LinkedStack& LinkedStack::Add(const T& x)
				{// Add x to stack.
				   Node *p = new Node;
				   p->data = x;
				   p->link = top;
				   top = p;
				   return *this;
				}
				
				template
				LinkedStack& LinkedStack::Delete(T& x)
				{// Delete top element and put it in x.
				   if (IsEmpty()) throw OutOfBounds();
				   x = top->data;
				   Node *p = top;
				   top = top->link;
				   delete p;
				   return *this;
				}
				
				#endif;
							

相关资源