zoukankan      html  css  js  c++  java
  • 4.栈的数组实现

    fatal.h

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

    stackar.h

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

    stackar.c

    #include "stackar.h"
    #include "fatal.h"
    #include <stdlib.h>
    
    #define EmptyTOS ( -1 )
    #define MinStackSize ( 5 )
    
    struct StackRecord
    {
        int Capacity;
        int TopOfStack;
        ElementType *Array;
    };
    
    int IsEmpty(Stack S)
    {
        return S->TopOfStack == EmptyTOS;
    }
    
    int IsFull(Stack S)
    {
        return S->TopOfStack == S->Capacity - 1;
    }
    
    Stack CreateStack(int MaxElements)
    {
        Stack S;
    
        if (MaxElements < MinStackSize)
            Error("Stack size is too small");
    
        S = malloc(sizeof(struct StackRecord));
        if (S == NULL)
            FatalError("Out of space!!!");
    
        S->Array = malloc(sizeof(ElementType) * MaxElements);
        if (S->Array == NULL)
            FatalError("Out of space!!!");
        S->Capacity = MaxElements;
        MakeEmpty(S);
    
        return S;
    }
    
    void MakeEmpty(Stack S)
    {
        S->TopOfStack = EmptyTOS;
    }
    
    void DisposeStack(Stack S)
    {
        if (S != NULL)
        {
            free(S->Array);
            free(S);
        }
    }
    
    void Push(ElementType X, Stack S)
    {
        if (IsFull(S))
            Error("Full stack");
        else
            S->Array[++S->TopOfStack] = X;
    }
    
    ElementType Top(Stack S)
    {
        if (!IsEmpty(S))
            return S->Array[S->TopOfStack];
        Error("Empty stack");
        return 0;  /* Return value used to avoid warning */
    }
    
    void Pop(Stack S)
    {
        if (IsEmpty(S))
            Error("Empty stack");
        else
            S->TopOfStack--;
    }
    
    ElementType TopAndPop(Stack S)
    {
        if (!IsEmpty(S))
            return S->Array[S->TopOfStack--];
        Error("Empty stack");
        return 0;  /* Return value used to avoid warning */
    }
    

    teststka.c

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

    函数调用关系图(Call graph)

  • 相关阅读:
    【LeetCode】1248. 统计「优美子数组」
    【LeetCode】200. 岛屿数量
    【LeetCode】53. 最大子序和
    【剑指Offer】面试题42. 连续子数组的最大和
    【剑指Offer】面试题57. 和为s的两个数字
    【LeetCode】55. 跳跃游戏
    【LeetCode】56. 合并区间
    简历HTML网页版
    关于新建Eclipse新建一个WEB项目后创建一个jsp文件头部报错问题?
    jquery选择器 看这个链接吧!2017.6.2
  • 原文地址:https://www.cnblogs.com/typewriter/p/6211469.html
Copyright © 2011-2022 走看看