⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 数据结构实现-堆栈-用数组来实现.txt

📁 包括数据结构中的常见的排序
💻 TXT
字号:
/*****************************************************************************************************
** 
** Program Name : Implementation of Stack with array
** Author  Name : Lu Jian Hua
** Time         : 2007-5-23
**
******************************************************************************************************/

#include <iostream>
using namespace std ;

class Stack
{
public:
	Stack(int max_size) ;					// constructor
	int  GetTopValue() const ;				// get the top value of stack
	void Push(int value) ;					// push
	int  Pop() ;							// pop
	bool IsEmpty() ;						// is empty ?
	bool IsFull() ;							// is full ?
private:
	enum { MAX_SIZE = 10 } ;
	int top ;
	int element[MAX_SIZE] ;
} ;


Stack::Stack(int max_size) : top(-1) {}

int Stack::GetTopValue() const
{
	if (top < 0)
	{
		cout << "The stack is empty!   " ;
		return 0 ;
	}

	return element[top] ;
}

void Stack::Push(int value)					// Operation:push a element into stack
{
	if (top >= MAX_SIZE-1)
	{
		cout << "The stack is full!    " << endl << endl ;
		return ;
	}

	top++ ;									// 1> move the top pointer
	element[top] = value ;					// 2> infill the space top pointer directing
}

int Stack::Pop()							// Operation:Pop a element from stack
{
	if (top < 0)
	{
		cout << "The stack is empty!    " ;
		return 0 ;
	}

	int temp = element[top] ;				// 1> get the value
	top-- ;									// 2> move the top pointer
	return temp ;
}

bool Stack::IsEmpty()						// test if the stack is empty ?
{
	return ( top < 0 ) ;
}

bool Stack::IsFull()						// test if the stack is full ?
{
	return ( top >= MAX_SIZE-1 ) ;			
}

Stack* CreateStack(int max_size)			// crate a stack and return its pointer
{
	Stack *ptr = new Stack(max_size) ;
	return ptr ;
}

int main()
{
	Stack *stk = CreateStack(10) ;			// create a stack

	while (true)
	{
		cout << "-----------------------------------" << endl ;
		cout << "Please Select : " << endl
			 << "              1> Push" << endl
			 << "              2> Pop " << endl
			 << "              3> Get the top value" << endl
			 << "              4> Is empty?"<< endl
			 << "              5> Is full?         " << endl;
		cout << "-----------------------------------   " ;
		char c = 0 ;
		cin >> c ;
		cout << endl ;

		switch (c)
		{
		case '1' :
			cout << "Input the value : " ;
			int number ;
			cin >> number ;
			cout << endl ;
			stk->Push(number) ;
			break ;
		case '2' :
			cout << "The value poped : " << stk->Pop() << endl << endl ;
			break ;
		case '3' :
			cout << "The top value : " << stk->GetTopValue() << endl << endl ;
			break ;
		case '4' :
			cout << "Is empty ? " << (stk->IsEmpty() ? "Yes" : "No" )<< endl << endl ;
			break ;
		case '5' :
			cout << "Is full ? " << (stk->IsFull() ? "Yes" : "No" )<< endl << endl ;
		default:
			break ;
		}
	}

	return 0 ;
}


/*************************************************************************************************************
** Some points to be notice about stack:
** 1> When pushing a element, check whether the stack is full first
** 2> When poping  a element, check whether the stack is empty first
** 3> Writing recommended:
**		bool IsEmpty()
**		{
**			return (top < 0) ;
**		}
**	  Writing no recommended:
**		bool IsEmpty()
**		{
**			if (top < 0)
**				return true ;
**			else
**				return false ;
**		}
**************************************************************************************************************/

/********************************************************************************
*
* Notice : This Program Can Be Launched In VC6.0 Environment
*
********************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -