数据结构与算法分析c语言描述(Mark Allen)--栈ADT数组实现

版权声明:请注明转载地址 https://blog.csdn.net/OCEANtroye https://blog.csdn.net/OCEANtroye/article/details/83216053

栈ADT数组实现

  • 使用数组存储
  • 操作集合
    • 入栈push
    • 出栈pop
    • 清空
    • 初始化
    • 返回栈顶元素
    • 得到一个随机栈
    • 打印整个栈

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct StackRecord;
typedef int ElementType;
const int MaxElement = 16;
typedef struct StackRecord *Stack;

#ifndef _Stack_h

//打印整个栈
void PrintStack(Stack S);

//判断是否为空栈
int isEmpty(Stack S);

//判断是否满栈
int isFull(Stack S);

//建栈
Stack CreateStack(int MaxElement);

//销毁栈
void DisposeStack(Stack S);

//清空一个栈
void MakeEmpty(Stack S);

//压栈
void push(Stack S, ElementType X);

//返回栈顶元素
ElementType Top(Stack S);

//弹出栈顶元素
void Pop(Stack S);

//返回栈顶元素并且pop操作
ElementType TopAndPop(Stack S);

#endif // !_Stack_h
const int EmptyTOS = -1;
//允许的最小栈的大小
const int MinStackSize = 5;
struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};

int main(int argc, char const *argv[])
{
    int choose;
    Stack S = nullptr;
    S = CreateStack(MaxElement);
    int num;
    printf("\t\t\t1.get a random stack.\n");
    printf("\t\t\t2.push a element.\n");
    printf("\t\t\t3.pop a element.\n");
    printf("\t\t\t4.get the top element of the stack.\n");
    printf("\t\t\t5.get the top element and pop.\n");
    printf("\t\t\t6.print the stack.\n");
    printf("\t\t\t7.make empty a stack.\n");
    while (1)
    {
        scanf("%d", &choose);
        if (choose == 0)
        {
            break;
        }
        switch (choose)
        {
        case 1:
            MakeEmpty(S);

            printf("\t\t\tplease intput a int to get a random stack.\n");
            scanf("%d", &num);
            srand((unsigned)time(NULL));
            for (int i = 0; i < num; i++)
            {
                int X = rand() % 51;
                push(S, X);
            }
            PrintStack(S);
            break;

        case 2:

            printf("\t\tint put a element.\n");
            scanf("%d", &num);
            push(S, num);
            printf("Done.\n-------------------\n");
            PrintStack(S);
            break;

        case 3:

            printf("Done.\n------------------\n");
            Pop(S);
            PrintStack(S);
            break;

        case 4:

            printf("\t\tthe top element of the stack is %d\n", Top(S));
            break;

        case 5:

            printf("the top element of the stack is %d\n", TopAndPop(S));
            printf("-----------------------------\n");
            PrintStack(S);
            break;

        case 6:

            printf("-----------------------------\n");
            PrintStack(S);
            break;

        case 7:

            MakeEmpty(S);
            printf("Done.\n");
            break;
        }
    }
    DisposeStack(S);
    system("pause");
    return 0;
}

//打印整个栈
void PrintStack(Stack S)
{
    if (S->TopOfStack == EmptyTOS)
    {
        printf("empty!\n");
        return;
    }
    for (int i = S->TopOfStack; i != EmptyTOS; i--)
    {
        printf("\t\t%d\n", S->Array[i]);
    }
}
//判断是否为空栈
int isEmpty(Stack S)
{
    return S->TopOfStack == EmptyTOS;
}

//判断是否满栈
int isFull(Stack S)
{
    return S->TopOfStack == MaxElement;
}

//建栈
Stack CreateStack(int MaxElement)
{
    Stack S;

    if (MaxElement < MinStackSize)
    {
        printf("stack size is too small.\n");
        exit(1);
    }
    S = (Stack)malloc(sizeof(struct StackRecord));
    if (S == nullptr)
    {
        printf("out of space.\n");
        exit(10086);
    }

    S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElement);
    if (S == nullptr)
    {
        printf("out of space.\n");
        exit(2);
    }
    S->Capacity = MaxElement;
    MakeEmpty(S);
    return S;
}

//销毁栈
void DisposeStack(Stack S)
{
    if (S != nullptr)
    {
        free(S->Array);
        free(S);
    }
}

//清空一个栈
//注意 有软删除和硬删除两种
//这里是软删除
//对于顺序存储只需要软删除即可达到硬删除的效果
void MakeEmpty(Stack S)
{
    S->TopOfStack = EmptyTOS;
}

//压栈
void push(Stack S, ElementType X)
{
    if (isFull(S))
    {
        printf("stack is full.\n");
        exit(1);
    }
    else
    {
        S->Array[++S->TopOfStack] = X;
    }
}

//返回栈顶元素
ElementType Top(Stack S)
{
    return S->Array[S->TopOfStack];
}

//弹出栈顶元素
void Pop(Stack S)
{
    if (isEmpty(S))
    {
        printf("empty stack.\n");
        return;
    }
    else
    {
        S->TopOfStack--;
    }
}

//返回栈顶元素并且pop操作
ElementType TopAndPop(Stack S)
{
    if (isEmpty(S))
    {
        printf("empty stack.\n");
        exit(1);
    }
    else
    {
        return S->Array[S->TopOfStack--];
    }
}

猜你喜欢

转载自blog.csdn.net/OCEANtroye/article/details/83216053