概述
栈的特点
使用只能在一端进行插入和删除操作的特殊线性表。
按照后进先出的原则存储数据:
- 先进入的数据被压入栈底,最后的数据在栈顶
- 需要读取数据的时候,从栈顶开始读取数据
基本实现
栈的创建和初始化
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
void newStack(Stack *stack, int cap) {// 给栈分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);stack->cap = 0;stack->top = 0;
}
判断栈是否满了
核心代码:
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
void newStack(Stack *stack, int cap) {// 给栈分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);stack->cap = 0;stack->top = 0;
}// 判断栈是否满了
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}
判断栈是否为空
核心代码:
int isEmptyStack(Stack *stack) {return stack->top == 0;
}
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
void newStack(Stack *stack, int cap) {// 给栈分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);stack->cap = 0;stack->top = 0;
}// 判断栈是否满了
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}// 判断栈是否为空
int isEmptyStack(Stack *stack) {return stack->top == 0;
}
入栈
核心代码:
void pushStack(Stack *stack, int value) {if (stack == NULL) {return;}if (isFullStack(stack)) {printf("the stack is full\n");return;}stack->arr[stack->top] = value;stack->top++;
}
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
void newStack(Stack *stack, int cap) {// 给栈分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);stack->cap = 0;stack->top = 0;
}// 判断栈是否满了
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}// 判断栈是否为空
int isEmptyStack(Stack *stack) {return stack->top == 0;
}// 入栈
void pushStack(Stack *stack, int value) {if (stack == NULL) {return;}if (isFullStack(stack)) {printf("the stack is full\n");return;}stack->arr[stack->top] = value;stack->top++;
}
出栈
核心代码:
int popStack(Stack *stack) {if (stack == NULL) {printf("the stack is null\n");return -1;}if (isEmptyStack(stack)) {printf("the stack is empty\n");return -1;}int value = stack->arr[stack->top];stack->top--;return value;
}
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈
extern int popStack(Stack *stack); // 出栈#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
void newStack(Stack *stack, int cap) {// 给栈分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);stack->cap = 0;stack->top = 0;
}// 判断栈是否满了
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}// 判断栈是否为空
int isEmptyStack(Stack *stack) {return stack->top == 0;
}// 入栈
void pushStack(Stack *stack, int value) {if (stack == NULL) {return;}if (isFullStack(stack)) {printf("the stack is full\n");return;}stack->arr[stack->top] = value;stack->top++;
}// 出栈
int popStack(Stack *stack) {if (stack == NULL) {printf("the stack is null\n");return -1;}if (isEmptyStack(stack)) {printf("the stack is empty\n");return -1;}int value = stack->arr[stack->top];stack->top--;return value;
}
创建栈方法优化
核心代码:
// 创建栈
Stack *newStack(int cap) {// 创建栈Stack *stack = malloc(sizeof(Stack));// 给栈数组分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;// 返回return stack;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);free(stack);
}
stack.h
#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"// 栈
typedef struct stack {int *arr;int cap;int top;
} Stack;// 栈方法
extern Stack * newStack(int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈
extern int popStack(Stack *stack); // 出栈#endif //C01_HELLO_CLION_STACK_H
stack.c
#include "stack.h"// 创建栈
Stack *newStack(int cap) {// 创建栈Stack *stack = malloc(sizeof(Stack));// 给栈数组分配内存stack->arr = malloc(sizeof(int) * cap);// 初始化容量stack->cap = cap;// 初始化topstack->top = 0;// 返回return stack;
}// 释放栈
void freeStack(Stack *stack) {free(stack->arr);free(stack);
}// 判断栈是否满了
int isFullStack(Stack *stack) {return stack->top >= stack->cap;
}// 判断栈是否为空
int isEmptyStack(Stack *stack) {return stack->top == 0;
}// 入栈
void pushStack(Stack *stack, int value) {if (stack == NULL) {return;}if (isFullStack(stack)) {printf("the stack is full\n");return;}stack->arr[stack->top] = value;stack->top++;
}// 出栈
int popStack(Stack *stack) {if (stack == NULL) {printf("the stack is null\n");return -1;}if (isEmptyStack(stack)) {printf("the stack is empty\n");return -1;}int value = stack->arr[stack->top];stack->top--;return value;
}
使用栈
main.c
#include <stdio.h>
#include "stack.h"int main() {// 创建栈Stack *stack = newStack(3);// 入栈pushStack(stack, 11);pushStack(stack, 22);pushStack(stack, 33);pushStack(stack, 333); // 栈满了// 出栈printf("%d\n", popStack(stack));printf("%d\n", popStack(stack));printf("%d\n", popStack(stack));printf("%d\n", popStack(stack)); // 栈空了// 释放内存freeStack(stack);return 0;
}
输出:
the stack is full
305
33
22
the stack is empty
-1
遍历栈
main.c
#include <stdio.h>
#include "stack.h"int main() {// 创建栈Stack *stack = newStack(3);// 入栈pushStack(stack, 11);pushStack(stack, 22);pushStack(stack, 33);// 遍历栈while (!isEmptyStack(stack)) {printf("%d\n", popStack(stack));}// 释放内存freeStack(stack);return 0;
}
输出:
588
33
22