顺序栈和链栈
顺序栈
#include <iostream>
using namespace std;
const int StackSize = 10;
template <class T>
class SeqStack
{
public:
SeqStack();
~SeqStack();
void Push(T x);//入栈操作 将元素x入栈
T Pop();//出栈操作 将栈顶元素弹出
T GetTop();//取出栈顶元素但不删除
bool Empty();//栈为空则返回true 不为空返回false
private:
T data[StackSize];//存放栈元素的数组
int top;//栈顶元素在数组中的下标
};
template <class T>
SeqStack<T>::SeqStack()
{
this->top = -1;
}
template <class T>
SeqStack<T>::~SeqStack()
{
}
template <class T>
void SeqStack<T>::Push(T x)
{
if (top == StackSize - 1)
{
throw "上溢";
}
top++;
this->data[top] = x;
}
template <class T>
T SeqStack<T>::Pop()
{
T x;
if (top == -1)
{
throw "下溢";
}
x = data[top--];//取出data[top]的值后top--
return x;
}
template <class T>
T SeqStack<T>::GetTop()
{
return this->data[top];
}
template <class T>
bool SeqStack<T>::Empty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}
int main()
{
int x;
SeqStack<int> s;
cout << "对15和10执行入栈操作,";
s.Push(1);
s.Push(2);
//s.Push(3);
//s.Push(4);
//s.Push(5);
//s.Push(6);
//s.Push(7);
//s.Push(8);
//s.Push(9);
//s.Push(10);
cout << "当前栈顶元素为:" << s.GetTop() << endl;
try
{
x = s.Pop();
cout << "执行一次出栈操作,删除元素" << x << endl;//输出出栈元素10
}
catch (const char* str)
{
cout << str << endl;
}
try
{
cout << "请输入待入栈元素:";
cin >> x;
s.Push(x);
}
catch (const char* str)
{
cout << str << endl;
}
if (s.Empty() == true)
{
cout << "栈为空" << endl;
}
else
{
cout << "栈非空" << endl;//有两个元素,输出栈非空
}
return 0;
}
catch捕获()里面一定是const char* str,否则任然会出现未经处理的异常
案例:进制转换
SeqStack<int> s;
int num, base;
cin >> num >> base;
while (num % base)
{
s.Push(num % base);
num /= base;
}
while (!s.Empty())
{
cout << s.Pop();
}
输入:159 8
输出:237
链栈
LinkStack.h中
#pragma once
#include <iostream>
using namespace std;
template <class T>
struct Node
{
T data;
struct Node<T>* next;
};
template <class T>
class LinkStack
{
public:
LinkStack();//构造函数 初始化一个空链栈
~LinkStack();//析构函数
void Push(T x);//入栈操作 将元素x入栈
T Pop();//出栈操作 将栈顶元素出栈
T GetTop();//取出栈顶元素但不删除
bool Empty();//判断链栈是否为空
private:
Node<T>* top;
};
template <class T>
LinkStack<T>::LinkStack()
{
this->top = nullptr;
}
template <class T>
LinkStack<T>::~LinkStack()
{
Node<T>* p = nullptr;
while (top)
{
p = top;
top = top->next;
delete p;
}
}
template <class T>
void LinkStack<T>::Push(T x)
{
Node<T>* s = new Node<T>;
s->data = x;
s->next = this->top;
this->top = s;
}
template <class T>
T LinkStack<T>::Pop()
{
Node<T>* p = this->top;//暂存
if (this->top == nullptr)
{
throw "下溢";
}
T x = this->top->data;
this->top = this->top->next;
delete p;
return x;
}
template <class T>
T LinkStack<T>::GetTop()
{
return this->top->data;
}
template <class T>
bool LinkStack<T>::Empty()
{
if (this->top == nullptr)
{
return true;
}
else
{
return false;
}
}
链栈的实现
#include "LinkStack.h"
int main()
{
int x;
LinkStack<int> s;
cout << "对15和10执行插入操作,";
s.Push(15);
s.Push(10);
cout << "当前栈顶元素为:" << s.GetTop() << endl;
try
{
x = s.Pop();
cout << "执行一次出栈操作,删除元素:" << x << endl;
}
catch (const char* str)
{
cout << str << endl;
}
try
{
cout << "请输入待插入元素:";
cin >> x;
s.Push(x);
}
catch (const char* str)
{
cout << str << endl;
}
if (s.Empty())
{
cout << "栈为空" << endl;
}
else
{
cout << "栈非空" << endl;
}
return 0;
}
案例:进制转换
LinkStack<int> s;
int num, base;
cin >> num >> base;
while (num % base)
{
s.Push(num % base);
num /= base;
}
while (!s.Empty())
{
cout << s.Pop();
}
输入:159 8
输出:237