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)

  • 相关阅读:
    【剑指offer】把字符串转换成整数
    【剑指offer】不用加减乘除做加法
    【剑指offer】求1+2+3+...+n
    【剑指offer】孩子们的游戏(圆圈中最后剩下的数)
    【剑指offer】扑克牌顺子
    【剑指offer】翻转单词顺序列
    【剑指offer】左旋转字符串
    【剑指offer】和为S的两个数字
    【剑指offer】和为S的连续正数序列
    2019.1.10 Mac安装Nginx服务器
  • 原文地址:https://www.cnblogs.com/typewriter/p/6211469.html
Copyright © 2011-2022 走看看