文章
7
粉丝
0
获赞
3
访问
158
用c写的,有自己写的解析。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char arr[27];
int flag[26]={0};//标记数组
// 定义栈的结构体
typedef struct {
char *array; // 指向动态分配的数组
int top; // 栈顶位置
int capacity; // 栈的容量
} Stack;
// 创建一个栈
Stack* createStack(int capacity) {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->top = -1; // 初始化栈顶位置为-1,表示栈为空
stack->array = (char*)malloc(stack->capacity * sizeof(char)); // 动态分配数组空间
return stack;
}
// 检查栈是否为空
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// 检查栈是否已满
int isFull(Stack *stack) {
return stack->top == stack->capacity - 1;
}
// 入栈操作
void push(Stack *stack, int item) {
if (isFull(stack))
return; // 如果栈已满,则不执行入栈操作
stack->array[++stack->top] = item; // 栈顶位置加1,并添加元素
}
// 出栈操作
int pop(Stack *stack) {
if (isEmpty(stack))
return 0; // 如果栈为空,返回INT_M...
登录后发布评论
暂无评论,来抢沙发