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

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        int data;
        struct node* next;
    }Node;
    
    typedef struct line_stack 
    {
        Node* top;
        int len;
    }Stack;
    
    Stack* creat_stack()
    {
        Stack* line = (Stack*)malloc(sizeof(Stack));
        line->top = NULL;
        line->len = 0;
        return line;
    }
    
    Node* creat_node(int data)
    {
        Node* node = (Node*)malloc(sizeof(Node));
        node->data = data;
        node->next = NULL;
        return node;
    }
    
    bool empty_stack(Stack* sta)
    {
        return !sta->len;
    }
    
    void push_stack(Stack* sta, int data)
    {
        Node* node = creat_node(data);
        if (empty_stack(sta)) {
            sta->top = node;
        } else {
            node->next = sta->top;
            sta->top = node;
        }
        sta->len++;
    }
    
    Node* top_stack(Stack* sta)
    {
        if (empty_stack(sta))
            return NULL;
        return sta->top;
    }
    
    bool pop_stack(Stack* sta)
    {
        if (empty_stack(sta))
        {
            return false;
        }
        Node* node = sta->top;
        sta->top = node->next;
        printf("pop_stack:%d
    ",node->data);
        free(node);
        sta->len--;
        return true;
    }
    
    void destory_stack(Stack* sta)
    {
        while (pop_stack(sta))
        {
            ;
        }
        free(sta);
    }
    
    int main()
    {
        int i;
        Stack* sta = creat_stack();
        for (i = 1; i <= 5; i++) {
            push_stack(sta, i);
            printf("%d
    ", top_stack(sta)->data);
        }
        destory_stack(sta);
        return 0;
    }
  • 相关阅读:
    #include <boost/shared_array.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/scoped_array.hpp>
    df命令
    telnet命令
    sort 命令
    苏宁大数据面试题
    hive严格模式
    k-means伪代码
    vim编辑器
  • 原文地址:https://www.cnblogs.com/zgen1/p/14585428.html
Copyright © 2011-2022 走看看