• LinkStack(链栈)


      链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了。和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出。

      (2018-02-14 代码更新)

      linkstack.h:

    #ifndef  __LINKSTACK_H_
    #define __LINKSTACK_H_
    
    #define bool int
    #define true 1
    #define false 0
    
    typedef int KeyType;
    
    typedef struct lstack
    {
        KeyType key;
        struct lstack * top;
    }Stack;
    
    Stack*CreateStack();
    int IsEmpty();
    bool Push();
    bool Pop();
    Stack*getTopNode();
    KeyType getTop();
    void Clear();
    void Destroy();
    
    #endif
    

      linkstack.c:

    /* linkstack.c */
    #include <stdio.h>
    #include <stdlib.h>
    #include "linkstack.h"
    
    Stack*CreateStack(void)
    {
        Stack*s;
        
        s = (Stack*)malloc(sizeof(Stack));
        s->top = NULL;
    
        return s;
    }
    
    int IsEmpty(Stack*s)
    {
        return s->top == NULL;
    }
    
    bool Push(Stack*s, KeyType Data)
    {
        Stack*p;
        
        if((p = (Stack*)malloc(sizeof(Stack))) == NULL)
            return false;
        p->key = Data;
        p->top = s->top;
        s->top = p;
        return true;
    }
    
    bool Pop(Stack*s)
    {
        Stack*p;
        
        if(IsEmpty(s))
            return false;
        p = s->top;
        s->top = s->top->top;
        free(p);
        p = NULL;
        return true;
    }
    
    Stack*getTopNode(Stack*s)
    {
        return s->top;
    }
    
    KeyType getTop(Stack*s)
    {
        return getTopNode(s)->key;
    }
    
    void Clear(Stack*s)
    {
        while(!IsEmpty(s))
            Pop(s);
    }
    
    void Destroy(Stack*s)
    {
        if(s != NULL)
        {
            Clear(s);
            if(s != NULL)
                free(s);
            s = NULL;
        }
    }
    

      

  • 相关阅读:
    转载:KOF97东丈
    写一个ajax程序就是如此简单
    转载:97特瑞心得
    老生常谈:享元模式
    获得微软最有影响力开发者
    老生常谈设计模式系列文章索引
    asp.net中的异步页面
    转载:KOF97简易出招原理解析
    leaks 使用手册
    ObjectiveC中一种消息处理方法performSelector: withObject:
  • 原文地址:https://www.cnblogs.com/darkchii/p/7368317.html
Copyright © 2020-2023  润新知