栈的基本操作

基本特点

  • 后进先出(Last In First Out)
  • 只在栈顶进行插入和删除等操作

栈的基本数据结构(顺序栈)

struct stack
{
    int *base;//尾指针,指向栈底
    int *top;//头指针,一般指向栈顶上一个元素
    int stacksize;//栈的最大容量
};

制作栈

void InitStack(struct Stack &S,int maxsize)
{
    S.base=new int[maxsize];//给栈开辟空间
    S.top=S.base;//置空栈
    S.stacksize=maxsize;
}

栈的基本操作(数据结构版)

主函数

 SStack S;
  InitStack(S,100);

入栈

//入栈
void Push (struct Stack &S,int n)
{
    if(S.top-S.base==S.stacksize)
    {
        cout<<"NO"<<endl;
        return;
    }
    *S.top=n;//top是指针,赋值时候需要加上*
     S.top++;//此时是指针操作,不需要加*
}

出栈

//出栈
int Pop(struct Stack &S)
{
    if(S.top==S.base)
    {
        cout<<"NO"<<endl;
        return -1;
    }
    S.top--;//top指针指向实际顶部元素
    return *S.top;
}

查询栈顶元素

int Search(struct stack &S)
{
    if(S.top==S.base)
    {
        return -1;
    }
    int e;
    S.top--;
    e=*S.top++;
    return e;
}

销毁

void delete_stack(struct stack &S,int maxsize)
{
        free(S.base);
        S.stacksize=0;
        S.base=S.top=NULL;
}

显示栈内所有元素

void showStack (struct Stack &S)
{
    int a =S.top-S.base;
    int i;
    for (i=0;i<a;i++)
    {
       cout<<S.base<<" ";
        S.base++;
    }
    cout<<endl;
}

栈的基本操作(c++应用版)

用题目匹配括号演示

#include <bits/stdc++.h>
using namespace std;

int main()
{
    stack<char> st;//制作栈
    string a;
    getline(cin,a);//题目要求可以输入空格

    for(int i=0; i<a.length(); i++)
    {
        if(a[i]=='('||a[i]=='['||a[i]=='{')
        {
            st.push(a[i]);//入栈
        }

        if(a[i]=='}'||a[i]==']'||a[i]==')')
        {
            if(!st.empty())//如果栈不为空
            {
                char t=st.top();
                //访问顶部元素

                if(a[i]=='}'&&t!='{')
                {
                    cout<<"no"<<endl;
                    return 0;
                }
                if(a[i]==']'&&t!='[')
                {
                    cout<<"no"<<endl;
                    return 0;
                }
                if(a[i]==')'&&t!='(')
                {
                    cout<<"no"<<endl;
                    return 0;
                }
                   st.pop();//出栈

            }
            else
            {
                cout<<"no"<<endl;
                return 0;
            }
        }


    }
    if(!st.empty())
    {

         cout<<"no"<<endl;

    }
    else
    {
        cout<<"yes"<<endl;

    }

    return 0;
}

技巧题目:7-5 出栈序列的合法性

技巧:不可能出现:大小中

image

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int m,n,k;
    cin>>m>>n>>k;
    int a[k][n];
    for(int i=0; i<k; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin>>a[i][j];
        }
    }
    int s[k];
    for(int i=0; i<k; i++)
    {
        stack<int> st;
        int index=0;
        for(int j=1; j<=n; j++)
        {
            if(st.size()<m)
            {
                st.push(j);
            }
            else break;
            while(!st.empty())
            {
                if(st.top()!=a[i][index])
                {
                    break;
                }
                else
                {
                    st.pop();
                    index++;
                    continue;
                }
            }
        }

        if(st.empty())
        {
            s[i]=1;
        }

        else
        {
            s[i]=0;
        }
    }
    for(int i=0; i<k; i++)
    {
        if(s[i])cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

    return 0;

}


练习> SUDTOJ 题目链接

进制转换(经典题目)

posted @ 2021-08-26 08:55  kingwzun  阅读(301)  评论(0编辑  收藏  举报