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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/OCEANtroye/article/details/83215270

栈ADT链表实现

  • 使用链表存储
  • 操作集合
    • 入栈push
    • 出栈pop
    • 清空
    • 初始化
    • 返回栈顶元素
    • 打印整个栈

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#ifndef _Stack_H
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

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

//插入 n个随机元素到站内
void GetRandomStack(Stack S, int num);

//判断链栈是否为空
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 // !_Stack_H

struct Node
{
    ElementType Element;
    PtrToNode Next;
};

int main(int argc, char const *argv[])
{
    int choose;
    int num;
    Stack S = CreateStack();
    printf("\t\t\t1.get a random stack.\n");
    printf("\t\t\t2.is the stack empty?\n");
    printf("\t\t\t3.get the top element of the stack.\n");
    printf("\t\t\t4.show a stack.\n");
    printf("\t\t\t5.make empty a stack.\n");

    printf("\t\t\t6.push a element.\n");
    printf("\t\t\t7.pop a element.\n");
    printf("\t\t\t0.exit.\n");
    while (1)
    {
        scanf("%d", &choose);

        switch (choose)
        {
        case 1:

            printf("\tplease input a int K to get a random K-length stack.\n");
                scanf("%d", &num);
            GetRandomStack(S, num);
            PrintStack(S);
            break;

        case 2:

            if (IsEmpty(S))
            {
                printf("\tstack is empty.\n");
            }
            else
            {
                printf("\tstack is not empty.\n");
            }
            putchar('\n');
            PrintStack(S);
            break;

        case 3:

            printf("the top element is %d.\n", Top(S));
            break;

        case 4:

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

        case 5:

            MakeEmpty(S);
            PrintStack(S);
            printf("\t\t\tdone!\n");
            break;

        case 6:
            printf("\t\t\tplease intput a number to push into stack.\n");
            scanf("%d",&num);
            Push(num,S);
            PrintStack(S);
            break;

        case 7:
            Pop(S);
            printf("\t\t\tdone.\n");
            PrintStack(S);
            break;

        case 0:
            return 0;
            break;
        }
    }
    system("pause");
    return 0;
}

int IsEmpty(Stack S)
{
    return S->Next == nullptr;
}

Stack CreateStack(void)
{
    Stack S;

    S = (Stack)malloc(sizeof(struct Node));
    if (S == nullptr)
    {
        printf("out of space!\n");
        exit(1);
    }

    S->Next = nullptr;
    MakeEmpty(S);
    return S;
}

void MakeEmpty(Stack S)
{
    if (S == nullptr)
    {
        printf("must use CreateStack first!\n");
    }
    else
    {
        while (!IsEmpty(S))
        {
            Pop(S);
        }
    }
}

void Pop(Stack S)
{
    PtrToNode FirstCell;
    if (IsEmpty(S))
    {
        printf("stack is already empty!.\n");
    }
    else
    {
        FirstCell = S->Next;
        S->Next = S->Next->Next;
        free(FirstCell);
    }
}

void DisposeStack(Stack S)
{
    if (S->Next == nullptr)
    {
        printf("stack is already empty.\n");
        exit(2);
    }
    else
    {
        while (!IsEmpty(S))
        {
            Pop(S);
        }
        free(S);
    }
}

void Push(ElementType X, Stack S)
{
    PtrToNode TmpCell;

    TmpCell = (PtrToNode)malloc(sizeof(struct Node));

    if (TmpCell == nullptr)
    {
        printf("out of space!\n");
        exit(3);
    }
    else
    {
        TmpCell->Element = X;
        TmpCell->Next = S->Next;
        S->Next = TmpCell;
    }
}

ElementType Top(Stack S)
{
    if (!IsEmpty(S))
    {
        return S->Next->Element;
    }
    else
    {
        printf("empty stack.\n");
        exit(10086);
    }
}

void PrintStack(Stack S)
{
    PtrToNode tmp = S->Next;
    while (tmp != nullptr)
    {
        printf("\t\t%d\n", tmp->Element);
        tmp = tmp->Next;
    }
}

void GetRandomStack(Stack S, int num)
{
    if (S == nullptr)
    {
        printf("create a stack first.\n");
        exit(5);
    }
    else
    {
        MakeEmpty(S);
        ElementType TempElement;
        srand((unsigned)time(NULL));
        for (int i = 0; i < num; i++)
        {
            TempElement = rand() % 51;
            Push(TempElement, S);
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页