一、栈模型
- 栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈的顶(top)。栈有时又称为LIFO(后进先出)表。
- 栈的应用
Expression evaluation
Backtracking
Memory Management
二、栈的实现
1. 定义结构体
typedef struct {
double *values;
int top;
int maxTop;
} Stack;
2. 栈的创建
- 这里我们初始化top为-1, 则存第一个元素的时候 top 为0,相当于数组的A[0]
bool CreateStack(Stack *stack, int size){
if (size < 1){
return false;
}
stack->values = (double*)malloc(sizeof(double)*size);
stack->top = -1;
stack->maxTop = size - 1;
return true;
}
3. 判断栈空和栈满
bool IsEmpty(Stack *stack){
return stack->top == -1;
}
bool IsFull(Stack *stack){
return stack->top == stack->maxTop;
}
4. 读取栈顶
bool Top(Stack *stack, double *x){
if (IsEmpty(stack)){ // 判断栈空
return false;
}
*x = stack->values[stack->top];
return true;
}
5. 入栈
bool Push(Stack *stack, double x){
if (IsFull(stack)){ // 判断栈满
return false;
}
// add x to the stack, top must be increased
stack->values[++stack->top] = x;
return true;
}
6. 出栈
bool Pop(Stack *stack, double *x){
if (IsEmpty(stack)){ // 判断栈空
return false;
}
// pass the value of the top element to x
// move the top value to the next
*x = stack->values[stack->top--];
return true;
}
7. 历遍打印
void DisplayStack(Stack *stack){
printf("top --> ");
for (int i=stack->top; i>=0; i--){
printf("|%12f\t|\n\t", stack->values[i]);
}
printf("+---------------+");
}
8. 栈的销毁
void DestroyStack(Stack *stack){
free(stack->values);
stack->maxTop = -1;
stack->top = -1;
stack->values = NULL;
}
9. 完整代码
- stack.h
#include <stdbool.h>
typedef struct {
double *values;
int top;
int maxTop;
} Stack;
bool CreateStack(Stack *stack, int size);
bool IsEmpty(Stack *stack);
bool IsFull(Stack *stack);
bool Top(Stack *stack, double *x);
bool Push(Stack *stack, double x);
bool Pop(Stack *stack, double *x);
void DisplayStack(Stack *stack);
void DestroyStack(Stack *stack);
- stack.c
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
bool CreateStack(Stack *stack, int size){
if (size < 1){
return false;
}
stack->values = (double*)malloc(sizeof(double)*size);
stack->top = -1;
stack->maxTop = size - 1;
return true;
}
bool IsEmpty(Stack *stack){
return stack->top == -1;
}
bool IsFull(Stack *stack){
return stack->top == stack->maxTop;
}
bool Top(Stack *stack, double *x){
if (IsEmpty(stack)){ // 判断栈空
return false;
}
*x = stack->values[stack->top];
return true;
}
bool Push(Stack *stack, double x){
if (IsFull(stack)){ // 判断栈满
return false;
}
// add x to the stack, top must be increased
stack->values[++stack->top] = x;
return true;
}
bool Pop(Stack *stack, double *x){
if (IsEmpty(stack)){ //判断栈空
return false;
}
// pass the value of the top element to x
// move the top value to the next
*x = stack->values[stack->top--];
return true;
}
void DisplayStack(Stack *stack){
printf("top --> ");
for (int i=stack->top; i>=0; i--){
printf("|%12f\t|\n\t", stack->values[i]);
}
printf("+---------------+");
}
void DestroyStack(Stack *stack){
free(stack->values);
stack->maxTop = -1;
stack->top = -1;
stack->values = NULL;
}
- main.c
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
int main(void) {
Stack stack;
double val;
CreateStack(&stack, 5);
Push(&stack, 5.0);
DisplayStack(&stack);
printf("\n");
Push(&stack, 6.5);
DisplayStack(&stack);
printf("\n");
Push(&stack, -3.0);
Push(&stack, -8.0);
DisplayStack(&stack);
if(Top(&stack, &val)){
printf("Top: %f\n", val);
}
printf("\n");
Pop(&stack, &val);
DisplayStack(&stack);
if(Top(&stack, &val)){
printf("Top: %f\n", val);
}
while(!IsEmpty(&stack)){
Pop(&stack, &val);
}
DisplayStack(&stack);
DestroyStack(&stack);
return 0;
}
-
结果显示
-
以上均为个人学习数据结构的时候的笔记,如有错误请多多指教