栈(stack)

栈:

Last in First out 线性表,故又称作LIFO结构。

顺序存储:空栈设栈顶指针为-1;否则为数组下标。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#pragma warning (disable:4996)
#define MAXSIZE 1000

typedef int SElemType;

typedef struct {
    SElemType data[MAXSIZE];
    int top;//栈顶pointer
}SqStack;

bool push(SqStack* S, SElemType e) {
    if (S->top = MAXSIZE - 1) {
        return 0;
    }
    S->top++;
    S->data[S->top] = e;
    return 1;
}

bool pop(SqStack* S, SElemType* e) {
    if (S->top == -1) {
        return 0;
    }
    *e = S->data[S->top];
    S->top--;
    return 1;
}

当要处理的两组数据具有一定负相关性时,使用两栈共享空间可以比较好的解决问题。

链栈:

以头结点处的指针作为栈顶指针,使头节点失去意义。

链栈的基本操作同单链表,具体有什么区别待学习QAQ

栈的应用:

递归:非常自然地可以发现,递归的逻辑与栈的LIFO非常相似。故系统在实现递归时,会使用栈的结构。一般我们不需要自己管理这个栈。

原文地址:https://www.cnblogs.com/porest/p/14137073.html