zoukankan      html  css  js  c++  java
  • 栈的顺序存储及其基本操作

    #include <stdio.h>
    #include <stdlib.h>
    #define M 10
    
    typedef struct   //定义一个顺序栈
    {
        char data[M];
        int top;
    }SqStack;
    
    void InitStack(SqStack &st)//创建一个栈
    {
        st.top=-1;
    }
    
    int PushStack(SqStack &st, char x)//进栈操作
    {
        if(st.top==M-1)
            return 0;
        else
        {
            st.top++;
            st.data[st.top]=x;
            return 1;
        }
    }
    
    int PopStack(SqStack &st,char &x)  //出栈操作
    {
        if(st.top==-1)
            return 0;
        else
        {
            x=st.data[st.top];
            st.top--;
            return 1;
        }
    }
    
    int GetTop(SqStack st,char &x)   //取栈顶元素
    {
        if(st.top==-1)
            return 0;
        else
        {
            x=st.data[st.top];
            return 1;
        }
    }
    
    int StackEmpty(SqStack st)  //判断栈空
    {
        if(st.top==-1)
            return 1;
        else
            return 0;
    }
    
    int main()
    {
        SqStack st;
        char e;
        InitStack(st);
        printf("栈%s
    ",(StackEmpty(st)==1?"空":"不空"));
        printf("a进栈
    ");PushStack(st,'a');
        printf("b进栈
    ");PushStack(st,'b');
        printf("c进栈
    ");PushStack(st,'c');
        printf("d进栈
    ");PushStack(st,'d');
        printf("栈%s",(StackEmpty(st)==1?"空":"不空"));
        GetTop(st,e);
        printf("栈顶元素:%c
    ",e);
        printf("出栈次序:");
        while(!StackEmpty(st))
        {
            PopStack(st,e);
            printf("%c ",e);
        }
        printf("
    ");
        return 0;
    }
    
    输入元素后输出
    请dalao不吝赐教。
  • 相关阅读:
    历史版本xcode的下载
    mac上安装hg
    xcode不能抓帧
    window buffer alignment
    highp 和 mediump
    AFBC mali
    AO composition
    gpu memory wait
    L2 cache//bifrost --- cortex-A55
    效果样式
  • 原文地址:https://www.cnblogs.com/liesun/p/7350349.html
Copyright © 2011-2022 走看看