zoukankan      html  css  js  c++  java
  • C语言实现栈

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    typedef struct stack_tag{
        int top;
        int num;
        int *data;
    } stack;
    
    stack * stack_init(int num){
        stack *s = (stack*)malloc(sizeof(stack));
        s->num = num;
        s->data = (int*)malloc(sizeof(int) * num);
        s->top = -1; 
        return s;
    }
    
    void stack_destroy(stack *s){
        free(s->data);
        free(s);
    }
    
    int stack_push(stack *s, int data){
        if(s->top == s->num - 1)
            return -1; 
        s->data[++s->top] = data;
        return 0;
    }
    
    int stack_pop(stack *s, int *data){
        if(s->top == -1){
            return -1; 
        }   
        *data =  s->data[s->top --];
        return 0;
    }
    bool stack_empty(stack *s){
        return s->top == -1;
    }
    
    int main() {
        stack *s = stack_init(3);
        stack_push(s, 1);
        stack_push(s, 2);
        while(!stack_empty(s)){
            int data = 0;
            stack_pop(s, &data);
            printf("%d
    ", data);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Java遍历Map、List、Array
    自签名SSL生成
    oracle_round
    Timestamp_时间戳
    oracle_substr
    eval
    orcale_聚合函数
    oracle_decode
    js_JSON
    sql拼接
  • 原文地址:https://www.cnblogs.com/moxiaopeng/p/4912430.html
Copyright © 2011-2022 走看看