数据结构(C语言) -- 栈demo

基本栈操作

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

#define EMPTY 1
#define NONEMPTY 0

typedef struct Node
{
    int data;
    struct Node *next;
} Node;

Node *initStack()
{
    Node *Stack = (Node *)malloc(sizeof(Node));
    if (Stack == NULL)
    {
        printf("malloc Stack error!\n");
    }
    Stack->data = 0;
    Stack->next = NULL;
    return Stack;
}

int isEmpty(Node *Stack)
{
    if (Stack->data == 0 || Stack->next == NULL)
    {
        return EMPTY;
    }
    else
    {
        return NONEMPTY;
    }
}

int getTop(Node *Stack) // 获取栈顶元素
{
    if (isEmpty(Stack) == EMPTY)
    {
        return -1;
    }
    else
    {
        return Stack->next->data;
    }
}

int pop(Node *Stack) // 出栈
{
    if (isEmpty(Stack) == EMPTY)
    {
        return -1;
    }
    else
    {
        Node *node = Stack->next;
        int data = node->data;
        Stack->next = node->next;
        free(node);
        Stack->data--;
        return data;
    }
    
}

void push(Node *Stack, int data) // 压栈
{
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (newNode == NULL)
    {
        printf("malloc newNode error!\n");
    }
    newNode->data = data;

    newNode->next = Stack->next;
    Stack->next = newNode;
    printf("%d 入栈\n", data);
    Stack->data++;
}

void printStack(Node *Stack)
{
    printf("遍历栈 > ");
    Node *current = Stack->next;
    while (current != NULL)
    {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("over\n");
}

int main(int argc, char const *argv[])
{
    Node *Stack = initStack();
    for (int i = 0; i <= 10; i++)
    {
        push(Stack, i);
    }
    printf("----------------------------------------------------------\n");
    printStack(Stack);
    printf("----------------------------------------------------------\n");
    printf("现在站内存在 %d 个元素\n", Stack->data);
    printf("----------------------------------------------------------\n");
    for (int i = 0; i <= 10; i++)
    {
        int data = pop(Stack);
        printf("当前 %d 出栈\n", data);
    }
    printf("----------------------------------------------------------\n");
    printf("现在站内还剩 %d 个元素\n", Stack->data);
    printf("----------------------------------------------------------\n");
    printStack(Stack);
    return 0;
}

 运行结果如下:

0 入栈
1 入栈
2 入栈
3 入栈
4 入栈
5 入栈
6 入栈
7 入栈
8 入栈
9 入栈
10 入栈
----------------------------------------------------------
遍历栈 > 10 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> over
----------------------------------------------------------
现在站内存在 11 个元素
----------------------------------------------------------
当前 10 出栈
当前 9 出栈
当前 8 出栈
当前 7 出栈
当前 6 出栈
当前 5 出栈
当前 4 出栈
当前 3 出栈
当前 2 出栈
当前 1 出栈
当前 0 出栈
----------------------------------------------------------
现在站内还剩 0 个元素
----------------------------------------------------------
遍历栈 > over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值