顺序栈的实现(类模板、异常处理)
声明:相关图片及内容来基于https://www.bilibili.com/video/av88693493
分文件编写(一)
类的声明和定义分开
Stack.h
#pragma once
const int MAX_SIZE = 100;
template <class T>
class Stack {
private:
int top;
T* point;
int size;
public:
Stack();
Stack(int s);
~Stack();
void push(T elem);
T pop();
T getTop();
bool isEmpth();
bool isFull();
void setNULL();
class Full {};
class Empty {};
};
//typedef Stack<char> CharStack;
Stack.cpp
#include "Stack.h"
template <class T>
Stack<T>::Stack() {
top = -1;
size = MAX_SIZE;
point = new T[MAX_SIZE];
}
template <class T>
Stack<T>::Stack(int s) {
top = -1;
size = s;
point = new T[size];
}
template<class T>
Stack<T>::~Stack() {
delete[] point;
}
template<class T>
void Stack<T>::push(T ch) {
if (isFull()) throw Stack<T>::Full(); //注意类模板的异常抛出,抛出匿名对象
else point[++top] = ch;
}
template<class T>
T Stack<T>::pop() {
if (isEmpth()) throw Stack<T>::Empty();
else return point[top--]; //注意top--,如果return和top--分开写,top--将不会执行
}
template<class T>
T Stack<T>::getTop() {
if (isEmpth()) throw Stack<T>::Empty();
else return point[top];
}
template<class T>
bool Stack<T>::isEmpth() {
if (top == -1) return true;
return false;
}
template<class T>
bool Stack<T>::isFull() {
if (top == size - 1) return true;
return false;
}
template<class T>
void Stack<T>::setNULL() {
top = -1;
}
template class Stack<char>; //类模板分文件必须加这一句
main.cpp
#include<iostream>
#include"Stack.h"
using namespace std;
int main() {
Stack<char> s1(2); //或CharStack s1(2);
char ch;
try {
s1.push('a');
s1.push('a');
s1.push('a');
}
catch (Stack<char>::Empty) { //或catch (CharStack::Empty)
cout << "empty";
}
catch (Stack<char>::Full) { //或catch (CharStack::Full)
cout << "full";
}
return 0;
}
分文件编写(二)
类的声明和定义一起放在.hpp文件下(通常做法)
Stack.hpp
const int MAX_SIZE = 100;
template <class T>
class Stack {
private:
int top;
T* point;
int size;
public:
Stack();
Stack(int s);
~Stack();
void push(T elem);
T pop();
T getTop();
bool isEmpth();
bool isFull();
void setNULL();
class Full {};
class Empty {};
};
//typedef Stack<char> CharStack;
template <class T>
Stack<T>::Stack() {
top = -1;
size = MAX_SIZE;
point = new T[MAX_SIZE];
}
template <class T>
Stack<T>::Stack(int s) {
top = -1;
size = s;
point = new T[size];
}
template<class T>
Stack<T>::~Stack() {
delete[] point;
}
template<class T>
void Stack<T>::push(T ch) {
if (isFull()) throw Stack<T>::Full();
else point[++top] = ch;
}
template<class T>
T Stack<T>::pop() {
if (isEmpth()) throw Stack<T>::Empty();
else return point[top--];
}
template<class T>
T Stack<T>::getTop() {
if (isEmpth()) throw Stack<T>::Empty();
else return point[top];
}
template<class T>
bool Stack<T>::isEmpth() {
if (top == -1) return true;
return false;
}
template<class T>
bool Stack<T>::isFull() {
if (top == size - 1) return true;
return false;
}
template<class T>
void Stack<T>::setNULL() {
top = -1;
}
//template class Stack<char>; //hpp文件可以不加
main.cpp
#include<iostream>
#include"Stack.hpp" //注意包含.hpp头文件
using namespace std;
int main() {
Stack<char> s1(2); //或CharStack s1(2);
char ch;
try {
s1.push('a');
s1.push('a');
s1.push('a');
}
catch (Stack<char>::Empty) { //或catch (CharStack::Empty)
cout << "empty";
}
catch (Stack<char>::Full) { //或catch (CharStack::Full)
cout << "full";
}
return 0;
}