zoukankan      html  css  js  c++  java
  • 5.栈的链表实现

    fatal.h

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Error(Str)        FatalError(Str)
    #define FatalError(Str)   fprintf(stderr, "%s
    ", Str), exit(1)
    

    stackli.h

    typedef int ElementType;
    
    #ifndef _Stack_h
    #define _Stack_h
    
    struct Node;
    typedef struct Node *PtrToNode;
    typedef PtrToNode Stack;
    
    int IsEmpty(Stack S);
    Stack CreateStack(void);
    void DisposeStack(Stack S);
    void MakeEmpty(Stack S);
    void Push(ElementType X, Stack S);
    ElementType Top(Stack S);
    void Pop(Stack S);
    
    #endif  
    

    stackli.c

    #include "stackli.h"
    #include "fatal.h"
    #include <stdlib.h>
    
    struct Node
    {
        ElementType Element;
        PtrToNode   Next;
    };
    
    int IsEmpty(Stack S)
    {
        return S->Next == NULL;
    }
    
    Stack CreateStack(void)
    {
        Stack S;
    
        S = malloc(sizeof(struct Node));
        if (S == NULL)
            FatalError("Out of space!!!");
        S->Next = NULL;
        MakeEmpty(S);
        return S;
    }
    
    void MakeEmpty(Stack S)
    {
        if (S == NULL)
            Error("Must use CreateStack first");
        else
            while (!IsEmpty(S))
                Pop(S);
    }
    
    void DisposeStack(Stack S)
    {
        MakeEmpty(S);
        free(S);
    }
    
    void Push(ElementType X, Stack S)
    {
        PtrToNode TmpCell;
    
        TmpCell = malloc(sizeof(struct Node));
        if (TmpCell == NULL)
            FatalError("Out of space!!!");
        else
        {
            TmpCell->Element = X;
            TmpCell->Next = S->Next;
            S->Next = TmpCell;
        }
    }
    
    ElementType Top(Stack S)
    {
        if (!IsEmpty(S))
            return S->Next->Element;
        Error("Empty stack");
        return 0;  /* Return value used to avoid warning */
    }
    
    void Pop(Stack S)
    {
        PtrToNode FirstCell;
    
        if (IsEmpty(S))
            Error("Empty stack");
        else
        {
            FirstCell = S->Next;
            S->Next = S->Next->Next;
            free(FirstCell);
        }
    }
    

    teststkl.c

    #include <stdio.h>
    #include "stackli.h"
    
    int main()
    {
        Stack S;
        int i;
    
        S = CreateStack();
        for (i = 0; i < 10; i++)
            Push(i, S);
    
        while (!IsEmpty(S))
        {
            printf("%d
    ", Top(S));
            Pop(S);
        }
    
        DisposeStack(S);
        return 0;
    }
    

    函数调用关系图(Call graph)

  • 相关阅读:
    【arc072f】AtCoder Regular Contest 072 F
    maven settings解决下载不了依赖包问题
    git 命令提交本地代码到新创建的仓库
    JAVA 利用切面、注解 动态判断请求信息中字段是否需要为空
    JAVA 根据身份证号码解析出生日期、性别、年龄
    利用JAVA正则快速获取URL的文件名
    datalist
    Mybatis map接收list参数
    bootstrap-table 列宽动态拖拽改变宽度
    JAVA 枚举类遍历与switch使用
  • 原文地址:https://www.cnblogs.com/typewriter/p/6211547.html
Copyright © 2011-2022 走看看