分别基于顺序存储/链式存储设计一个栈（C语言）（数据结构学习5）

什么是栈

栈的具体实现

• 顺序栈：采用顺序存储结构可以模拟栈存储数据的特点，从而实现栈存储结构；
• 链栈：采用链式存储结构实现栈结构；

顺序存储（数组）实现栈

定义栈结构

``````typedef int ElemType;//元素类型
typedef struct SqStack{

ElemType data[MAX_SIZE];
int top;//栈顶指针

} SqStack;
``````

1、初始化一个栈

``````Status initStack(SqStack *stack){

stack->top = -1;
return OK;
}
``````

2、将栈置空

``````Status clearStack(SqStack *stack){
stack->top = -1;
return OK;
}
``````

3、判断顺序栈是否为空栈

``````Status isStackEmpty(SqStack stack){
return stack.top < 0;
}
``````

4、返回栈的长度

``````int getStackLength(SqStack stack){
return stack.top + 1;
}
``````

5、获取栈顶元素

``````Status getTopData(SqStack stack, ElemType *data){

if (isStackEmpty(stack)) {
return ERROR;
}
*data = stack.data[stack.top];
return OK;
}
``````

6、判断栈是否已满

``````Status isStackFull(SqStack stack){

return stack.top >= MAX_SIZE - 1;
}
``````

7、入栈

``````Status pushData(SqStack *stack, ElemType data){

//满栈不能插入
if (isStackFull(*stack)) {
return ERROR;
}
stack->top ++;
stack->data[stack->top] = data;
return OK;
}
``````

8、出栈

``````Status popData(SqStack *stack, ElemType *data){

if (isStackEmpty(*stack)) {
return ERROR;
}
*data = stack->data[stack->top];
stack->top --;
return OK;
}
``````

9、遍历整个栈

``````void stackTraverse(SqStack stack){

printf("遍历栈(从顶到底)：");
for (int i = stack.top; i >= 0; i --) {
printf("%d ",stack.data[I]);
}
printf("\n");
}
``````

其它辅助代码

``````#include "stdlib.h"

#define OK    1
#define ERROR 0
#define TRUE  1
#define FALSE 0
#define MAX_SIZE 10

typedef int Status;
![](https://user-gold-cdn.xitu.io/2020/4/16/1718378a3e600993?w=894&h=592&f=png&s=125824)

int main(int argc, const char * argv[]) {
// insert code here...
printf("--顺序栈的表示与实现--\n");

SqStack stack;
ElemType data;

printf("初始化栈并压入数据:\n");
if (initStack(&stack) == OK) {
for (int i = 1 ; i <= 10; i++) {
pushData(&stack, i);
}
}
stackTraverse(stack);
printf("是否为满栈(1为满，0为不满):%d\n",isStackFull(stack));

popData(&stack, &data);
printf("弹出栈顶元素为: %d\n",data);
stackTraverse(stack);

printf("是否为空栈(1为空，0为不空):%d\n",isStackEmpty(stack));

getTopData(stack, &data);
printf("栈顶元素:%d \n栈长度:%d\n",data,getStackLength(stack));

printf("清空栈:\n");
clearStack(&stack);
printf("是否已经清空栈(1为空，0为不空) %d, 栈长度为:%d\n", isStackEmpty(stack),getStackLength(stack));

printf("\n");
return 0;
}
``````

链式存储（链表）实现栈

定义栈结构

``````typedef int ElemType;//元素类型
//链表节点
typedef struct StackNode{
ElemType data;
struct StackNode *next;
} StackNode;

//栈
typedef struct {

int count;

``````

1、构造一个空栈

``````void initStack(LinkStack *stack){

stack->top = NULL;
stack->count = 0;
}
``````

2、将链栈置为空栈

``````void clearStack(LinkStack *stack){

while (p) {
q = p;
p = p->next;
free(q);
}
stack->top = NULL;
stack->count = 0;
}
``````

3、判断是否为空栈

``````Status isStackEmpty(LinkStack stack){
return stack.count == 0;
}
``````

4、获取栈的长度

``````//返回栈的元素个数,即栈的长度
return stack.count;
}
``````

5、获取栈顶元素

``````//若链栈不为空,则用data返回栈顶元素,并返回OK ,否则返回ERROR*/

if (stack.count < 1) {
return ERROR;
}
*data = stack.top->data;
return OK;
}
``````

6、入栈

``````//插入元素到链栈(成为栈顶新元素)

node->data = data;
node->next = stack->top;
stack->top = node;
stack->count ++;
return OK;
}
``````

7、出栈

``````//若栈不为空,则删除栈顶元素,并返回其值,返回值返回OK,否则返回ERROR

if (stack->count < 1) {
return ERROR;
}
*data = topNode->data;
stack->top = topNode->next;
free(topNode);
stack->count --;
return OK;
}
``````

8、遍历链栈

``````Status stackTraverse(LinkStack stack){

printf("遍历栈（从顶到底）：\n");
while (node) {
printf("%d ",node->data);
node = node->next;
}
printf("\n");
return OK;
}
``````

其它辅助代码

``````#include "stdlib.h"

#define OK    1
#define ERROR 0
#define TRUE  1
#define FALSE 0

typedef int Status;

int main(int argc, const char * argv[]) {

printf("链栈定义与实现\n");

ElemType data;

initStack(&stack);
for(int i = 1; i <= 10; i++){
pushData(&stack, i);
}

stackTraverse(stack);

popData(&stack, &data);
printf("弹出的栈顶元素:data = %d\n",data);
stackTraverse(stack);

printf("栈空否：%d(1:空 0:否)\n",isStackEmpty(stack));

getTopData(stack, &data);
printf("栈顶元素 data=%d 栈的长度为%d\n",data,getStackLength(stack));

clearStack(&stack);
printf("清空栈后，栈空否：%d(1:空 0:否)\n",isStackEmpty(stack));

printf("\n");
return 0;
}
``````