zoukankan      html  css  js  c++  java
  • 线性栈

    敲了下线性栈实现的代码,有必要保存起来

    #include <stdio.h>
    #define MAX_STACK_SIZE 100
    typedef int ElemType;
    typedef struct
    {
        ElemType stack_array[MAX_STACK_SIZE];
        int top;
    }SqStack;
    SqStack Init_Stack()
    {
        SqStack S;
        S.top = -1;
        return S;
    }
    bool Push(SqStack &S, ElemType e)
    {
        if(S.top == MAX_STACK_SIZE-1)
        {
            printf("栈满无法进栈
    ");
            return 0;
        }
        else
        {
            S.top++;
            S.stack_array[S.top] = e;
            //printf("S.stack_array[%d] = %d
    ", S.top, S.stack_array[S.top]);
            return 1;
        }
    }
    bool Pop(SqStack &S, ElemType &e)
    {
        if(S.top == -1)
        {
            printf("栈空, 无法出栈
    ");
            return 0;
        }
        else
        {
            e = S.stack_array[S.top];
            S.top--;
            return 1;
        }
    }
    void Print(SqStack S)
    {
        for(int i = 0; i <= S.top; i++)
        {
            if(i % 10 == 0 && i)
                printf("
    ");
            printf("%d	", S.stack_array[i]);
        }
    }
    int main()
    {
        SqStack S;
        int e;
        S = Init_Stack();
        for(int i = 0; i < MAX_STACK_SIZE; i++)
            Push(S, i);
        Push(S, 2);
        Print(S);
        Pop(S, e);
        printf("%d出栈
    ", e);
        Pop(S, e);
        printf("%d出栈
    ", e);
        Print(S);
        return 0;
    }
  • 相关阅读:
    面向对象-类
    模块04
    总结
    昨天的新的解决方法
    感冒了~ vs中py和vb实现一个小算法
    vs2015社区版不支持installshield
    网站被黑了
    2018/11/18(python)
    2018/11/14
    2018/11/12(python)
  • 原文地址:https://www.cnblogs.com/rain-1/p/4923028.html
Copyright © 2011-2022 走看看