常用算法与数据结构原代码

源代码在线查看: lstack.h

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

相关代码

				// file lstack.h
				// linked stack
				#ifndef LinkedStack_
				#define LinkedStack_
				
				#include 
				#include "lsnode.h"
				#include "xcept.h"
				
				template
				class LinkedStack 
				{
					friend ostream& operator					friend istream& operator>>(istream& ,LinkedStack&);
				public:
					LinkedStack() 
					{
						top = 0; 
						length=0;
					}
					~LinkedStack();
					bool IsEmpty() const 
					{
						return top == 0;
					}
					bool IsFull() const;
					int Length()const 
					{
						return length;
					}
					T Top() const;
					LinkedStack& Add(const T& x);
					LinkedStack& Delete(T& x);
				private:
					Lnode *top; // pointer to top Lnode
					int length;            
				};
				
				template
				LinkedStack::~LinkedStack()
				{// Stack destructor..
					Lnode *next;
					while (top) 
					{
						next = top->link;
						delete top;
						top = next;
					}
				}
				
				template
				bool LinkedStack::IsFull() const
				{// Is the stack full?
					try 
					{
						Lnode *p = new Lnode;
						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.
					Lnode *p = new Lnode;
					p->data = x;
					p->link = top;
					top = p;
					++length;
					return *this;
				}
				
				template
				LinkedStack& LinkedStack::Delete(T& x)
				{// Delete top element and put it in x.
					if (IsEmpty()) 
						throw OutOfBounds();
					x = top->data;
					Lnode *p = top;
					top = top->link;
					delete p;
					--length;
					return *this;
				}
				
				template 
				ostream& operator				{
					Lnode *current;
					for (current=L.top;current;current=current->link)
					{
						cout					}
					return out;
				}
				
				template 
				istream& operator>>(istream& in,LinkedStack& L)
				{
					T x;
					cin>>x;
					while(x)
					{
						L.Add(x);
						cin>>x;
					}
					return in;
				}
				
				#endif;
							

相关资源