• 栈是一种先进后出的数据结构,本文在链表的基础上实现栈结构。

    代码

    // stack.h
    /*********************************************************************************************
    * 版权所有 :  
    * 文件名称 :  queue.h
    * 文件标识 :  无
    * 内容摘要 :  实现队列功能
    * 其它说明 :  其它内容的说明
    * 当前版本 :  V1.00
    * 作    者 :  
    * 完成日期 :  2021-12-16
    *********************************************************************************************/ 
    
    #ifndef __STACK_H__
    #define __STACK_H__
    
    /***************************************************************/
    /* 类型定义                                                    */
    /***************************************************************/
    typedef List Stack;
    
    /***************************************************************/
    /* 宏定义                                                      */
    /***************************************************************/
    #define STACK_HEAD_INIT(name) { &(name), &(name) }
    #define STACK_HEAD(name) Stack name = STACK_HEAD_INIT(name)
    
    #define stack_for_each(pos, head) \
                for(pos = (head)->next; pos != (head); pos = pos->next)
    
    #define stack_for_each_safe(pos, n, head) \
            for (pos = (head)->next, n = pos->next; pos != (head); \
                    pos = n, n = pos->next)
    
    /***************************************************************/
    /* 函数声明                                                    */
    /***************************************************************/
    extern void init_stack_head(Stack *q);
    extern void stack_push(Stack *q, Stack *head);
    extern Stack* stack_pop(Stack *head);
    extern int stack_num(Stack *head);
    extern int stack_empty(Stack *head);
    
    #endif
    // stack.c
    /*********************************************************************************************
    * 版权所有 :  
    * 文件名称 :  list.c
    * 文件标识 :  无
    * 内容摘要 :  实现队列功能
    * 其它说明 :  其它内容的说明
    * 当前版本 :  V1.00
    * 作    者 :  
    * 完成日期 :  2021-12-16
    *********************************************************************************************/ 
    #include "list.h"
    #include "stack.h"
    
    /******************************************************************************
    * 函数名称: init_stack_head
    * 功能描述: 初始化栈
    * 其它说明: 无                                                           
    * 修改记录: 修改日期         修改人         修改内容
    *           2021-12-16       创建
    ******************************************************************************/
    void init_stack_head(Stack *q)
    {
        init_list_head(q);
    }
    
    /******************************************************************************
    * 函数名称: stack_push
    * 功能描述: 入栈
    * 其它说明: 无                                                           
    * 修改记录: 修改日期         修改人         修改内容
    *           2021-12-16       chusiyong      创建
    ******************************************************************************/
    void stack_push(Stack *q, Stack *head)
    {
        list_add_head(q, head);
    }
    
    /******************************************************************************
    * 函数名称: stack_pop
    * 功能描述: 出栈
    * 其它说明: 无                                                           
    * 修改记录: 修改日期         修改人         修改内容
    *           2021-12-16       创建
    ******************************************************************************/
    Stack* stack_pop(Stack *head)
    {
        Stack *q = head->next;
        list_del(q);
        return q;
    }
    
    /******************************************************************************
    * 函数名称: stack_num
    * 功能描述: 获取栈中元素个数
    * 其它说明: 无                                                           
    * 修改记录: 修改日期         修改人         修改内容
    *           2021-12-16             创建
    ******************************************************************************/
    int stack_num(Stack *head)
    {
        return list_num(head);
    }
    
    /******************************************************************************
    * 函数名称: stack_empty
    * 功能描述: 判断栈是否为空
    * 其它说明: 无                                                           
    * 修改记录: 修改日期         修改人         修改内容
    *           2021-12-16       创建
    ******************************************************************************/
    int stack_empty(Stack *head)
    {
        return list_empty(head);
    }

    使用

    #include <stdio.h>
    #include "list.h"
    #include "stack.h"
    
    typedef struct node
    {
        int data;
        Stack stack;
    }Node; 
    
    Node* initNode(int data)
    {
        Node *pNode = malloc(sizeof(Node));
        
        pNode->data = data;
        
        init_stack_head(&pNode->stack);
        
        return pNode;
    }
    
    int main()
    {
        STACK_HEAD(stack);
        
        stack_push(&initNode(1)->stack, &stack);
        stack_push(&initNode(2)->stack, &stack);
        stack_push(&initNode(3)->stack, &stack);
    
        Node *p = NULL;
        
        p = container_of(stack_pop(&stack), Node, stack);
        printf("%d\n", p->data);
        
        p = container_of(stack_pop(&stack), Node, stack);
        printf("%d\n", p->data);
        
        p = container_of(stack_pop(&stack), Node, stack);
        printf("%d\n", p->data);
    
        return 0;
    }
  • 相关阅读:
    react中关于render渲染次数的性能优化
    ES6中六种常用的传出调用方式
    在Vue-cli中得xxx.vue文件中快速生成组件模板
    如何在Vue-cli中关闭ESLint以及关闭部分ESLint验证
    使用ES6删除对象中某些属性
    React中使用遍历
    git commit报错解决,绕过代码检查
    React学习——子组件给父组件传值
    React学习——通过模态框中的表单,学习父子组件之间传值
    学习axios
  • 原文地址:https://www.cnblogs.com/chusiyong/p/15710638.html
Copyright © 2020-2023  润新知