1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-02-04 11:24:10 +08:00
Files
CS408/Code/sequence_stack.h
2021-04-21 00:32:06 +08:00

67 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "head.h"
// 顺序栈
typedef struct {
// 静态数组存储栈元素
element_type data[MAXSIZE];
// 栈顶指针
int top;
} SequenceStack;
// 初始化顺序栈
int InitSequenceStack(SequenceStack* stack) {
stack->top = -1;
return 0;
}
// 判断顺序栈是否为空
int IsSequenceStackEmpty(SequenceStack stack) {
if (stack.top == -1) {
return 1;
}
else {
return 0;
}
}
// 将元素推入顺序栈
int PushSequenceStack(SequenceStack* stack, element_type elem) {
// 栈满无法添加元素
if (stack->top == MAXSIZE - 1) {
printf("PushSequenceStack:栈满无法继续推入元素!");
return 1;
}
//// 指针加一
//stack->top += 1;
//// 新元素入栈
//stack->data[stack->top] = elem;
// 加一并入栈
stack->data[++stack->top] = elem;
return 0;
}
// 将元素弹出顺序栈
int PopSequenceStack(SequenceStack* stack, element_type* elem) {
// 栈空无法删除元素
if (stack->top == -1) {
printf("PopSequenceStack:栈空无法继续弹出元素!");
return 1;
}
// 弹出后再出栈
*elem = stack->data[stack->top--];
return 0;
}
// 读取顺序栈栈顶元素
int GetSequenceStackTop(SequenceStack* stack, element_type* elem) {
// 栈空无法获取元素
if (stack->top == -1) {
printf("PopSequenceStack:栈空无法读取元素!");
return 1;
}
// 弹出后再出栈
*elem = stack->data[stack->top];
return 0;
}