顺序栈:
操作:
建立,销毁,进栈,出栈,取栈顶元素,判断桟是否为空
图示:
代码:
#define MAXSIZE 20
template<typename T>
class SqStack {
T* data;
int top;
//基本运算
public:
SqStack() {
data = new T[MAXSIZE];
top = -1;
}
~SqStack() {
delete[]data;
}
//判断是否为空
bool empty() {
return top == -1;
}
//入桟
bool push(T e) {
if (top == MAXSIZE - 1)
return 0;
top++;
data[top] = e;
return 1;
}
//出栈
bool pop(T& e) {
if (empty())
return 0;
e = data[top];
top--;
return 1;
}
//取桟顶元素
bool gettop(T& e) {
if (empty())
return 0;
e = data[top];
return 1;
}
};
链桟:
操作:
建立,销毁,进栈,出栈,取栈顶元素,判断桟是否为空
图示:
代码:
template<typename T>
//构建链桟节点
struct LinkNode {
T data;
LinkNode* next;
LinkNode() :next(NULL) {};
LinkNode(T d) :data(d),next(NULL) {};
};
//构建链桟
template<typename T>
class LinkStack {
public:
LinkNode<T>* head;
//桟的基本算法
//构造桟
LinkStack() {
head = new LinkNode<T>();
}
//析构桟
~LinkStack() {
LinkNode<T>* pre = head, * p = pre->next;
while (p != NULL) {
delete pre;
pre = p;
p = p->next;
}
delete pre;
}
//判断桟是否为空
bool empty() {
return head->next = NULL;
}
//进栈算法
bool push(T e) {
LinkNode<T>* p = new LinkNode<T>(e);
p->next = head->next;
head->next = p;
return 1;
}
//出栈算法
bool pop(T& e) {
LinkNode<T>* p;
p = head->next;
if (head->next == NULL)return 0;
p = head->next;
e = p->data;
head->next = p->next;
delete p;
return 1;
}
//取栈顶元素
bool gettop(T& e) {
if (head->next == NULL)rturn 0;
e = head->next->data;
return 1;
}
};
/*链表一般用单链表,方便操作*/
算法设计:
顺序栈:
括号匹配算法:
代码:
#include "SqStack.cpp" //包含顺序栈类模板的定义
bool isMatch(string str) //判断表达式各种括号是否匹配的算法
{ SqStack<char> st; //建立一个顺序栈
int i=0; //遍历字符串
char e;
while (i<str.length())
{ if (str[i]=='(' || str[i]=='[' || str[i]=='{')
st.push(str[i]); //遇到将左括号,均进栈
else
{ if (str[i]==')') //遇到')'
{ if (st.empty()) //栈空时返回false
return false;
st.pop(e); //出栈元素e
if (e!='(') //栈顶不是匹配的'(',返回false
return false;
}
if (str[i]==']') //遇到']'
{ if (st.empty()) //栈空时返回false
return false;
st.pop(e); //出栈元素e
if (e!='[') //栈顶不是匹配的'[',返回false
return false;
}
if (str[i]=='}') //遇到'}'
{ if (st.empty()) //栈空时返回false
return false;
st.pop(e); //出栈元素e
if (e!='{') //栈顶不是匹配的'{',返回false
return false;
}
}
i++; //继续遍历str
}
return st.empty(); } //遍历结束后需要判断一下栈是否为空
#include"Ismatch.h"
int main() {
cout << "测试1: ";
string str = "([)]";
if (isMatch(str))
cout << str << "中括号是匹配的" << endl;
else
cout << str << "中括号不匹配的" << endl;
cout << "测试2:";
str = "([])";
if (isMatch(str))
cout << str << "中括号是匹配的" << endl;
else
cout << str << "中括号不匹配的" << endl;
return 0;
cout << "测试1: ";
str = "([)]";
if (isMatch(str))
cout << str << "中括号是匹配的" << endl;
else
cout << str << "中括号不匹配的" << endl;
cout << "测试2:";
str = "([])";
if (isMatch(str))
cout << str << "中括号是匹配的" << endl;
else
cout << str << "中括号不匹配的" << endl;
return 0;
}
回文判断:
方法:
代码
#include"isPalindrome.h"
int main() {
{
cout << "测试1: ";
string str = "abcba";
if (isPalindrome(str))
cout << str << "是回文" << endl;
else
cout << str << "不是回文" << endl;
cout << "测试2: ";
str = "1221";
if (isPalindrome(str))
cout << str << "是回文" << endl;
else
cout << str << "不是回文" << endl;
return 0;
}
}
最小桟:
要求:返回桟中的最小元素,要求各操作的事件复杂度均为O(1)。
操作:etmin()函数用于返回主栈中的最小元素,其操作是取mindata栈的栈顶元素。
进栈函数push(x)的操作是,当data栈空或者进栈元素x小于等于当前主栈中最小元素(即x≤Getmin())时,则将x进mindata栈。最后将x进data栈。
出栈函数pop()的操作是,当data栈不空时,从data栈出栈元素x,若mindata栈的栈顶元素等于x,则同时从mindata栈出栈x。最后返回x。
取栈顶函数gettop()的操作是,当data栈不空时,返回data栈的栈顶元素。
#include<iostream>
using namespace std;
const int MaxSize=100; //栈中最多元素个数
template <typename T>
class STACK //含Getmin()的栈类
{ T data[MaxSize]; //存放主栈中元素,初始为空
T mindata[MaxSize]; //存放min栈中元素,初始为空
int top;
int mintop;
public:
STACK():top(-1),mintop(-1) {} //构造函数
private: //min栈简化的基本运算算法,设为私有的
bool minempty() //判断min栈是否空
{
return mintop==-1;
}
void minpush(T e) //元素e进min栈
{ mintop++;
mindata[mintop]=e;
}
T minpop() //元素出min栈
{ T x=mindata[mintop];
mintop--;
return x;
}
T mingettop() //取min栈栈顶元素
{
return mindata[mintop];
}
public: //主栈基本运算算法,设为公有的
bool empty() //判断主栈是否空
{
return top==-1;
}
bool push(T x) //元素x进主栈
{ if (top==MaxSize-1) //主栈满返回false
return false;
if (empty() || x<=Getmin())
minpush(x); //栈空或者x<=min栈顶元素时进min栈
top++;
data[top]=x; //将x进主栈
return true;
}
bool pop(T& x) //元素x出主栈
{ if (empty()) //栈为空的情况,即栈下溢出
return false;
x=data[top]; //从主栈出栈x
top--;
if (x==mingettop()) //若栈顶元素为最小元素
minpop(); //min栈出栈一次
return true;
}
bool gettop(T& e) //取主栈栈顶元素
{ if (empty()) //栈为空的情况,即栈下溢出
return false;
e=data[top]; //取栈顶指针位置的元素
return true;
}
T Getmin() //获取栈中最小元素
{
return mingettop(); //返回min栈的栈顶元素即主栈中最小元素
}
};
int main()
{ STACK<int> st; //定义栈对象
int e;
cout <<"元素5,6,3,7依次进栈" << endl;
st.push(5);
st.push(6);
st.push(3);
st.push(7);
cout << " 求最小元素并出栈" << endl;
while (!st.empty())
{ cout << " 最小元素: "
<< st.Getmin() << endl;
st.pop(e);
cout << " 出栈元素: " << e << endl;
}
return 0;
}