顺序栈及应用

参考的李春葆老师的数据结构教程
#ifndef __STACK_H_
#define __STACK_H_

#include <iostream>

using namespace std;

const int MaxSize = 100;

template<class T>
class SeqStack
{
public:
    SeqStack();
    ~SeqStack();
    bool StackEmpty();
    bool Pop(T &e);
    bool Push(T e);
    bool GetTop(T &e);
private:
    T *data;
    int top;
};
//
template<class T>
SeqStack<T>::SeqStack()
{
    data = new T[MaxSize];
    top = -1;
}
//
template<class T>
SeqStack<T>::~SeqStack(){}
//
template<class T>
bool SeqStack<T>::StackEmpty()
{
    if(top == -1)
        return true;
    else
        return false;
}
//
template<class T>
bool SeqStack<T>::Push(T e)
{
    if(top == MaxSize-1)
        return false;
    else
    {
        ++top;
        data[top] = e;
        return true;
    }
}
//
template<class T>
bool SeqStack<T>::Pop(T &e)
{
    if(StackEmpty())
        return false;
    else
    {
        e = data[top];
        top--;
        return true;
    }
}
//
template<class T>
bool SeqStack<T>::GetTop(T &e)
{
    if(StackEmpty())
        return false;
    else
    {
        e = data[top];
        return true;
    }
}
//
bool isSerial(int str[], int n) //判断序列str是否为一个合适的出栈序列
{
    int i, j ,e;
    int a[MaxSize];
    SeqStack<int> st;   //建立一个顺序栈
    for(i = 0; i<n; ++i)
        a[i] = i+1;
    i = 0;
    j = 0;
    while(i<n&&j<n)
    {
        if(st.StackEmpty()||(st.GetTop(e)&&e!=str[j]))
        {
            st.Push(a[i]);
            cout<<"元素"<<a[i]<<"进栈"<<endl;
            ++i;
        }
        else
        {
            st.Pop(e);
            cout<<"元素"<<e<<"出栈"<<endl;
            ++j;
        }
    }
    while(!st.StackEmpty()&&st.GetTop(e)&&e==str[j])
    {
        st.Pop(e);
        cout<<"元素 "<<e<<"出栈"<<endl;
        ++j;
    }
    if(j == n)
        return true;
    else
        return false;
}
void Disp(int str[],int n)      //输出str
{
    int i;
    for(i = 0;i<n; ++i)
        cout<<str[i];
}
//
bool isPalindrome(char str[],int n)     //判断一个字符串是否是回文串
{
    int i;
    char e;
    SeqStack<char> st;
    while(i<n)                      //将字符串入栈
    {
        st.Push(str[i]);
        ++i;
    }
    i = 0;
    while(i<n)
    {
        st.Pop(e);              //得到栈顶元素,并出栈
        if(e =! str[i])         //判断首尾是否一样
            return false;
        ++i;
    }
    return true;
}
//检查输入的表达式中的括号是否匹配
bool isMatch(char str[],int n)
{
    int i = 0;
    char e;
    SeqStack<char> st;
    while(i<n)
    {
        if(str[i] == '('||str[i] == '{'||str[i] == '[')//如果是左括号则入栈
            st.Push(str[i]);
        else
        {
            if(str[i] == ')')
            {
                if(!st.Pop(e))      //得到栈顶元素并出栈
                    return false;
                if(e != '(')
                    return false;   //如果左右不一样,则返回错误
            }
            if(str[i] == ']')
            {
                if(!st.Pop(e))
                    return false;
                if(e != '[')
                    return false;
            }
            if(str[i] == '}')
            {
                if(!st.Pop(e))
                    return false;
                if(e != '{')
                    return false;
            }
        }
        ++i;
    }
    if(!st.StackEmpty())            //最后栈不为空则也不是正确的匹配
        return false;
    else
        return true;
}
#endif // __STACK_H_



#include "Stack.h"

int main()
{
    //测试是否是合理的出栈顺序
    int n = 4;
    int str[] = {3,4,2,1};
    cout<<"由1~"<<n<<"产生";Disp(str,n);
    cout<<"的操作序列"<<endl;
    if(isSerial(str,n))
    {
        Disp(str,n);
        cout<<"是合适的出栈序列"<<endl;
    }
    else
    {
        Disp(str,n);
        cout<<"不是合适的出栈序列"<<endl;
    }
    //判断是否是回文串
    char str1[]= "abcba";
    if(isPalindrome(str1,5))
        cout<<"是回文串"<<endl;
    else
        cout<<"不是回文串"<<endl;
    //判断括号是否匹配
    char str3[]="{【)}";
    if(isMatch(str3,4))
        cout<<str3<<"中的括号匹配"<<endl;
    else
        cout<<str3<<"中的括号不匹配"<<endl;

    return 0;
}
//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值