懒猫老师数据结构4作业:用类模板实现顺序栈

#pragma once
const int MAX_SIZE = 100;
template <class DataType>
class stack
{
private:
	DataType* data;//属性:线性表!
	int size;//堆栈实际大小
	int top;//栈顶
public:
	stack();//默认构造函数
	stack(int s);//有参构造函数
	~stack();//析构函数
	void push(DataType ch);//入栈
	DataType pop();//出栈并返回栈顶元素
	DataType getTop();//获得栈顶元素但不出栈
	bool isEmpty();//判断栈是否为空
	bool isFull();//判断栈是否满
	void setNull();//设置栈为空!!!!

	//捕捉异常需要设定内部类!!!!!
	class Full {};
	class Empty {};//注意是花括号!!!
};
typedef stack<char> Charstack;
typedef stack<int > Intstack;
typedef stack<double> Doublestack;
//#endif

注意末尾处所添加的代码!!!!

 

#include "stack.h"
#include <iostream>
using namespace std;
template <class DataType>
stack<DataType>::stack() {
	size = MAX_SIZE;
	top = -1;
	data = new DataType(MAX_SIZE);
}
template <class DataType>
stack<DataType>::stack(int s) {
	size = s;
	top = -1;
	data = new DataType(size);
}
template <class DataType>
stack<DataType>::~stack()
{
	//delete[] data;//内存回收 释放new创建data占用的内存
	//释放内存超出所分配的边界!报错!!!?删去该行代码九回复正常!why????
	cout << "finished delete\n";
}
template <class DataType>
void stack<DataType>::push(DataType ch) {
	if (isFull())
		throw stack<DataType>::Full();
	else
		data[++top] = ch;
	//return true;
}
template <class DataType>
DataType stack<DataType>::pop() {
	if (isEmpty())
		throw stack<DataType>::Empty();
	else
		return data[top--];
}
template <class DataType>
DataType stack<DataType>::getTop() {
	if (!isEmpty())
		return data[top];
}
template <class DataType>
bool stack<DataType>::isEmpty() {
	if (top == -1) {
		return true;
	}
	else {
		return false;
	}
}
template <class DataType>
bool stack<DataType>::isFull() {
	if (top == size - 1) {
		return true;
	}
	else {
		return false;
	}
}
template <class DataType>
void stack<DataType>::setNull() {
	top = -1;
	cout << "top=-1!\n";
}


template class stack<char>;
template class stack<int>;
template class stack<double>;

注意末尾处所添加的代码!!!!

#include "stack.h"
#include <iostream>
using namespace std;

int main() {
	Charstack st(2);
	//stack<char> st(2);//利用两者均可构造函数初始化
	char ch;
	try {
		st.push('a');
		st.push('b');
		st.push('c');
	}
	catch (Charstack::Full) {//注意!!!此处FULL后面不需要括号!!!!!
		cout << "stack full!\n";
	}
	try {
		ch = st.pop();
		cout << ch << endl;
		ch = st.pop();
		cout << ch << endl;
		ch = st.pop();
		cout << ch << endl;
	}
	catch (Charstack::Empty) {
		cout << "stack empty!\n";
	}



	double c;
	Doublestack sl(2);
	try {
		sl.push(1998.3);
		sl.push(1999.9);
		sl.push(2000.1);
	}
	catch (Doublestack::Full) {//注意!!!此处FULL后面不需要括号!!!!!
		cout << "stack full!\n";
	}
	try {
		c = sl.pop();
		cout << c << endl;
		c = sl.pop();
		cout << c << endl;
		c = sl.pop();
		cout << c << endl;
	}
	catch (Doublestack::Empty) {
		cout << "stack empty!\n";
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值