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

📄 mystack.h

📁 gamecode 很不错的小游戏源代码
💻 H
字号:
//************************************************************
//堆栈类
//作者:曾铮
//时间:2002
//说明:实现堆栈模版
//************************************************************
#ifndef MYSTACK_H_
#define MYSTACK_H_

#include "iostream.h"
#include "stdlib.h"

#define TRUE 1
#define FALSE 0
#define OVERFLOW -2  //溢出错误
#define SIZE 10		//stack元素大小
#define INCREMENT 5		//空间增值大小。

typedef int status;

template<class elemtype> 
class stack
{
private:
		elemtype *base;
		elemtype *top;
		int len;
public:
		stack();
		bool clearstack();
		bool stackempty();
		//		int stacklength();
		status gettop(elemtype &e);
		elemtype gettop();
		status push(elemtype e);
		status pop(elemtype &e);
		status pop();
};

template<class elemtype>
stack<elemtype>::stack()
{
	this->base=(elemtype*)malloc(sizeof(elemtype)*SIZE);
	this->len=SIZE;
	this->top=base;
}

template<class elemtype>
bool stack<elemtype>::clearstack()
{
	this->top=this->base;
	return true;
}


template<class elemtype>
bool stack<elemtype>::stackempty()
{
	if(this->top==this->base)
		return true;
	return false;
}

template<class elemtype>
status stack<elemtype>::gettop(elemtype &e)
{
	if(this->top==this->base)
		return FALSE;
	e=*(this->top-1);
	return TRUE;
}

template<class elemtype>
elemtype stack<elemtype>::gettop()
{
	elemtype e;
	if(this->top==this->base)
		return FALSE;
	e=*(this->top-1);
	return e;
}

template<class elemtype>
status stack<elemtype>::push(elemtype e)
{
	if(this->top-this->base>=SIZE)
	{
		this->base=(elemtype*)realloc(base,(SIZE+INCREMENT)*sizeof(elemtype));
		if(!this->base)
			return OVERFLOW;
		this->len=SIZE+INCREMENT;
	}
	*this->top++=e;
	return TRUE;
}

template<class elemtype>
status stack<elemtype>::pop(elemtype &e)
{
	if(this->top==this->base)
		return FALSE;
	e=*--this->top;
	return TRUE;
}

template<class elemtype>
status stack<elemtype>::pop()
{
	if(this->top==this->base)
		return FALSE;
	*--this->top;
	return TRUE;
}

#endif

⌨️ 快捷键说明

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