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不吝赐教。
  • 相关阅读:
    着迷英语900句总结
    SQL Server 常见数据类型
    SQL Server视频总结
    第三次SLA文档学习
    Rosetton Stone Summary
    【周总结】2018-10-19—2018-11-25
    学生信息管理系统总结
    VB中 EOF 和 BOF 的区别
    VB中 On error 的用法
    VB中 . 与 ! 的区别
  • 原文地址:https://www.cnblogs.com/liesun/p/7350349.html
Copyright © 2011-2022 走看看