zoukankan      html  css  js  c++  java
  • 栈的数组实现

    //栈的数组实现

    #include <cstdio>
    #include <cstdlib>
    //#define _OJ_
    #define stack_size 100

    typedef struct Lnode
    {
        int base;
        int top;
        int *elem;
    } Lnode, *stack;

    stack
    creat_list(stack s)
    {
        s = (stack) malloc (sizeof(Lnode));
        s->elem = (int*) malloc (stack_size * sizeof(int));
        s->top = s->base = -1;
        return s;
    }

    int
    isEmpty(stack s)
    {
        if(s->top == s->pop)
            return 1;
        else
            return 0;
    }


    void
    push(stack s,int x)
    {
        if(!isEmpty(s))
        s->elem[++s->top] = x;
    }

    int
    pop(stack s)
    {
        if(!isEmpty(s))
        int e;
        e = s->elem[s->top--];
        return e;
    }


    int main(int argc, char const *argv[]) {
    #ifndef _OJ_  //ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif

        int i, n, x;
        stack s;
        s = creat_list(s);
        scanf("%d", &n);
        for(i = 0;i < n; i++) {
        scanf("%d", &x);
        push(s,x);
         }

         for(i = 0;i < n; i++) {
         printf("%d ", pop(s));
          }

        return 0;
    }

  • 相关阅读:
    Python 特点
    Python简介
    数据库查询语句
    人月神话读书笔记01
    团队介绍
    团队项目一 原型展示+电梯演讲
    全球疫情可视化展示
    NABCD模型
    第六周学习进度
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/airfand/p/4918662.html
Copyright © 2011-2022 走看看