C语言栈的数组实现(源码笔记)

stackar.h

typedef int ElementType;
/* START: fig3_45.txt */
#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  /* _Stack_h */

/* END */

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; // 存储元素的数组
};

/* START: fig3_48.txt */
int
IsEmpty(Stack S)
{
    return S->TopOfStack == EmptyTOS;
}

/* END */

int
IsFull(Stack S)
{
    return S->TopOfStack == S->Capacity - 1; // 表示数组的最后一块区域用尽了
}

/* START: fig3_46.txt */
Stack
CreateStack(int MaxElements)
{
    Stack S;

/* 1*/      if (MaxElements < MinStackSize)
/* 2*/          Error("Stack size is too small");

/* 3*/      S = malloc(sizeof(struct StackRecord)); // 给栈记录这一结构体分配空间
/* 4*/      if (S == NULL)
/* 5*/          FatalError("Out of space!!!");

/* 6*/      S->Array = malloc(sizeof(ElementType) * MaxElements); // 给数组分配空间
/* 7*/      if (S->Array == NULL)
/* 8*/          FatalError("Out of space!!!");
/* 9*/      S->Capacity = MaxElements;
/*10*/      MakeEmpty(S);

/*11*/      return S;
}
/* END */

/* START: fig3_49.txt */
void
MakeEmpty(Stack S)
{
    S->TopOfStack = EmptyTOS;
}
/* END */

/* START: fig3_47.txt */
void
DisposeStack(Stack S)
{
    // 先释放结构体中的数组,然后再释放这个结构体本身
    if (S != NULL)
    {
        free(S->Array);
        free(S);
    }
}
/* END */

/* START: fig3_50.txt */
void
Push(ElementType X, Stack S)
{
    if (IsFull(S))
        Error("Full stack");
    else
        S->Array[++S->TopOfStack] = X;
}
/* END */


/* START: fig3_51.txt */
ElementType
Top(Stack S)
{
    if (!IsEmpty(S))
        return S->Array[S->TopOfStack];
    Error("Empty stack");
    return 0;  /* Return value used to avoid warning */
}
/* END */

/* START: fig3_52.txt */
void
Pop(Stack S)
{
    if (IsEmpty(S))
        Error("Empty stack");
    else
        S->TopOfStack--;
}
/* END */

/* START: fig3_53.txt */
// 弹出并返回栈顶元素,这在Java的栈中有一个这样的函数
ElementType
TopAndPop(Stack S)
{
    if (!IsEmpty(S))
        return S->Array[S->TopOfStack--];
    Error("Empty stack");
    return 0;  /* Return value used to avoid warning */
}
/* END */

teststka.c(main.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;
}

fatal.h

#include <stdio.h>
#include <stdlib.h>

#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s
", Str ), exit( 1 )

运行结果:

原文地址:https://www.cnblogs.com/fanlumaster/p/13698216.html