1. 程式人生 > >棧的物理實現(順序棧+鏈式棧)

棧的物理實現(順序棧+鏈式棧)

  • 說明
  • 基於順序表實現的順序棧
  • 基於連結串列實現的鏈式棧

一、說明:

本文中基於順序表實現的順序棧中top指的是棧中第一個空閒位置,基於連結串列實現的鏈式棧中top指向鏈式棧中第一個結點(棧頂)的指標,無頭結點。

二、基於順序表實現的順序棧

Stack.h

#include<iostream>
using namespace std;
#ifndef _Stack
#define _Stack
namespace wangzhe
{
	template<typename E>
	class Stack
	{
		private:
			void operator = (const Stack&) {}
			Stack(const Stack&) {}
			
		public:
			Stack() {}
			virtual ~Stack() {}
			virtual void clear() = 0;//清空棧
			virtual void push(const E& it) = 0;//壓棧
			virtual E pop() = 0;//出棧
			virtual const E& topValue() const = 0;//返回棧頂元素
			virtual int length() const = 0;//返回元素個數 
	}; 
}
#endif

AStack.h

#include<iostream>
using namespace std;
#include"Stack.h"
#ifndef _AStack
#define _AStack
namespace wangzhe
{
	template<typename E>
	class AStack:public Stack<E>
	{
		private:
			int maxSize;//最大容量 
			int top;//棧頂指標,這裡指的是棧中第一個空閒位置 
			E *listArray;//表 
			
		public:
			AStack(int size);
			~AStack();
			void clear();
			void push(const E& it);
			E pop();
			const E& topValue() const;
			int length() const;
	}; 
}
#endif

AStack.cpp

#include<iostream>
using namespace std;
#include"AStack.h" 
namespace wangzhe
{
	template<typename E>
	AStack<E>::AStack(int size)
	{
		maxSize=size; 
		top=0;
		listArray=new E[size];
	}
	
	template<typename E>
	AStack<E>::~AStack()
	{
		delete [] listArray;
	}
	
	template<typename E>
	void AStack<E>::clear()
	{
		top=0;
	}
	
	template<typename E>
	void AStack<E>::push(const E& it)
	{
		if(top==maxSize) 
		{
			cout<<"Stack is full"<<endl;
			return;
		}
		listArray[top++]=it;
	}
	
	template<typename E>
	E AStack<E>::pop()
	{
		if(top==0) 
		{
			cout<<"Stack is empty"<<endl;
			return -1;//用-1作為出錯的返回值,並不嚴謹 
		}
		return listArray[--top];
	}
	
	template<typename E>
	const E& AStack<E>::topValue() const
	{
		if(top==0) 
		{
			cout<<"Stack is empty"<<endl;
			return -1;//用-1作為出錯的返回值,並不嚴謹
		}
		return listArray[top-1];
	}
	
	template<typename E>
	int AStack<E>::length() const
	{
		return top;
	}
}

main.cpp

#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include"AStack.h"
#include"AStack.cpp"
using namespace wangzhe;
int main(int argc, char** argv) 
{
	AStack<int> a(10010);
	a.push(2);
	a.push(3);
	a.push(4);
	cout<<a.topValue()<<endl;
	a.pop();
	cout<<a.topValue()<<endl;
	cout<<a.pop()<<endl;
	return 0;
}

三、基於連結串列實現的鏈式棧

Link.h

#include <iostream>
using namespace std;
#ifndef _Link
#define _Link
namespace wangzhe
{
	template<typename E> 
	class Link
	{
	    public:
		    E element;
			Link *next;
			Link(const E& elemval,Link* nextval =NULL)
			{
			    element=elemval;
				next=nextval;	
			}	
			Link(Link* nextval=NULL)
			{
				next=nextval;
			}
	};
}
#endif
 

Stack.h

同順序棧

LStack.h

#include<iostream>
using namespace std;
#include"Link.h"
#include"Stack.h"
#ifndef _LStack
#define _LStack
namespace wangzhe
{
	template<typename E>
	class LStack:public Stack<E>
	{
		private:
			Link<E>* top;
			int size;
			
		public:
			LStack(int size);
			~LStack();
			void clear();
			void push(const E& it);
			E pop();
			const E& topValue() const;
			int length() const;
	}; 
}
#endif

LStack.cpp

#include<iostream>
using namespace std;
#include"LStack.h"
namespace wangzhe
{
	template<typename E>
	LStack<E>::LStack(int size)
	{
		top=NULL;
		size=0;
	}
	
	template<typename E>
	LStack<E>::~LStack()
	{
		clear();
	}
	
	template<typename E>
	void LStack<E>::clear()
	{
		while(top!=NULL)
		{
			Link<E>* temp=top;
			top=top->next;
			delete temp;
		}
		size=0;
	}
	
	template<typename E>
	void LStack<E>::push(const E& it)
	{
		top=new Link<E>(it,top);
		size++;
	}
	
	template<typename E>
	E LStack<E>::pop()
	{
		if(top==NULL)
		{
			cout<<"Stack is empty"<<endl;
			return -1;//用-1作為出錯的返回值,並不嚴謹
		}
		E it=top->element;
		Link<E>* temp=top->next;
		delete top;
		top=temp;
		size--;
		return it; 
	}
	
	template<typename E>
	const E& LStack<E>::topValue() const
	{
		if(top==NULL)
		{
			cout<<"Stack is empty"<<endl;
			return -1;//用-1作為出錯的返回值,並不嚴謹
		}
		return top->element;
	}
	
	template<typename E>
	int LStack<E>::length() const
	{
		return size;
	}
}

main.cpp

同順序棧