数据结构 顺序栈(泛型编程)

顺序栈及其基本操作

特点:
1、 后进先出(LIFO)
2、 栈底指针始终指向0,栈顶指针移动,且始终指向最后一个元素的后一个位置
3、 当栈顶指针与栈底指针相等,都指向0时表示栈空,当栈顶指针等于最大元素个数时表示栈满

在编写过程中出现 LNK2019 链接器错误
经过寻找原因了解到:C++模板不支持分离编译,只要将声明与实现都写在.h文件中就可以通过
参考资料:

https://www.cnblogs.com/qlwy/archive/2012/03/21/2410045.html

定义:

#define MAX_SIZE 100

template<typename ElemType>
struct SqStack
{
	ElemType et[MAX_SIZE];
	int base, top;
};

enum Status { ERROR = 0, OK = 1 };

基本操作:

template<typename ElemType>
Status initSqStsck(SqStack<ElemType>& S)//初始化
//栈顶指针与栈底指针同时指向0
{
	S.top = 0;
	S.base = 0;
	return OK;
}

template<typename ElemType>
bool isEmpty(SqStack<ElemType> S)//判断栈空
//栈顶指针与栈底指针相等,都指向0
{
	return S.top == S.base ? true : false;
}

template<typename ElemType>
bool isFull(SqStack<ElemType> S)//判断栈满
//栈顶指针等于最大元素个数
{
	return S.top == MAX_SIZE ? true : false;
}

template<typename ElemType>
Status push(SqStack<ElemType>& S, const ElemType& e)//入栈
//将数据存入栈顶指针位置的元素,栈顶指针加1
{
	if (isFull(S))
	{
		cout << "栈满" << endl;
		return ERROR;
	}
	S.et[S.top++] = e;
	return OK;
}

template<typename ElemType>
Status pop(SqStack<ElemType>& S, ElemType& e)//出栈 e用来接收数据
//栈顶指针减1,将栈顶指针指向的元素赋值给e,此时e必须是引用,否则无法接收
{
	if (isEmpty(S))
	{
		cout << "栈空" << endl;
		return ERROR;
	}
	e = S.et[--S.top];
	return OK;
}

template<typename ElemType>
Status getTop(SqStack<ElemType>& S, ElemType& e)//获取栈顶元素 e用来接收数据
//将栈顶指针减1位置的元素赋值给e,所以此时e也必须是引用
{
	if (isEmpty(S))
	{
		cout << "栈空" << endl;
		return ERROR;
	}
	e = S.et[S.top - 1];
	return OK;
}

template<typename ElemType>
ostream& operator<<(ostream& os, SqStack<ElemType>& S)//按线性表格式输出
//按线性表格式输出元素,即 (a1, a2, a3, ……, an) 的格式
{
	if (isEmpty(S))
		return os << "栈空";
	os << "(";
	for (int i = S.base; i < S.top; i++)
		os << S.et[i] << ", ";
	os << "\b\b)";
	return os;
}

template<typename ElemType>
Status printTop2Base(SqStack<ElemType>& S, string interval = "")//按从栈顶到栈底输出 interval为各个元素之间的间隔,默认为没有
//指定间隔的从栈顶到栈底输出
{
	if (isEmpty(S))
	{
		cout << "栈空" << endl;
		return ERROR;
	}
	for (int i = S.top - 1; i > S.base; i--)
		cout << S.et[i] << interval;
	cout << S.et[S.base] << endl;//最后一个元素后不需要interval,单独输出
	return OK;
}

template<typename ElemType>
Status printBase2Top(SqStack<ElemType>& S, string interval = "")//按从栈底到栈顶输出 interval为各个元素之间的间隔,默认为没有
//指定间隔的从栈底到栈顶输出
{
	if (isEmpty(S))
	{
		cout << "栈空" << endl;
		return ERROR;
	}
	for (int i = S.base; i < S.top - 1; i++)
		cout << S.et[i] << interval;
	cout << S.et[S.top - 1] << endl;//最后一个元素后不需要interval,单独输出
	return OK;
}

template<typename ElemType>
Status clear(SqStack<ElemType>& S)//清空栈
//将栈顶指针归0
{
	S.top = 0;
	S.base = 0;
	return OK;
}

来写一个函数测试一下基本功能

void testSqStack()//演示测试栈的功能
{
	SqStack<int> S;
	initSqStsck(S);
	cout << "0~9入栈……" << endl;
	for (int i = 0; i < 10; i++)
	{
		push(S, i);
	}
	cout << "结果为:" << S << endl;
	cout << "出栈3个元素……" << endl;
	for (int i = 0; i < 3; i++)
	{
		int e;
		pop(S, e);
		cout << e << "出栈 ";
	}
	cout << "\n结果为:" << S << endl;
	int e;
	getTop(S, e);
	cout << "此时栈顶元素为:" << e << endl;
	cout << "按从栈底到栈顶输出元素,以,为分隔:";
	printBase2Top(S, ",");
	cout << "按从栈顶到栈底输出元素,无分隔:";
	printTop2Base(S);
	cout << "清空栈……" << endl;
	clear(S);
	cout << "结果为:" << S << endl;
	system("pause");
}

看看结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值