• 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;
    }
  • 相关阅读:
    5、include为应用指定多个struts配置文件
    4、struts处理流程和action的管理方式
    8、类型转换器
    7、请求参数接收
    UESTC 2014 Summer Training #6 Div.2
    Codeforces Round #FF
    css ul li去除圆点
    css a标签去除下划线
    Axure的热区元件的作用
    结组开发项目(TD学生助手)
  • 原文地址:https://www.cnblogs.com/zgen1/p/14585428.html
Copyright © 2020-2023  润新知