typedef struct {
int top;
char data[MaxSize]
} Stack;
void InitStack(Stack *s) {
memset(s->data, '.', sizeof(char) * MaxSize);
s->top = -1;
}
_Bool isEmpty(Stack *s) {
return s->top == MaxSize;
}
_Bool isFull(Stack *s){
return s->top == MaxSize - 1;
}
void Push(Stack *s, char element) {
if (s->top != (MaxSize - 1))
s->data[++s->top] = element;
}
void Pop(Stack *s) {
if (s->top != -1)
s->top--;
}
char getTop(Stack *s) {
char topElement = s->data[s->top];
return topElement;
}
_Bool IsPalindrome(char *str, int len) {
Stack *s = (Stack *) malloc(sizeof(Stack));
InitStack(s);
int IsOdd = len % 2 ? 1 : 0;
int index = 0;
while (index < len)
if (index < len / 2) {
Push(s, str[index]);
index++;
} else if (index == len / 2 && IsOdd)
index++;
else {
if (str[index] == getTop(s)) {
Pop(s);
index++;
} else return 0;
}
return 1;
}
_Bool IsCorrectMatch(const char *str) {
Stack *s = (Stack *) malloc(sizeof(Stack));
InitStack(s);
while (*str != '\0') {
if (*str == '(' || *str == '[' || *str == '{')
Push(s, *str);
if (*str == ')' || *str == ']' || *str == '}') {
if ((*str == ')' && getTop(s) == '('))
Pop(s);
else if ((*str == ']' && getTop(s) == '['))
Pop(s);
else if ((*str == '}' && getTop(s) == '{'))
Pop(s);
else
return 0;
}
str++;
}
return 1;
}
typedef struct {
int data[MaxSize];
int topL;
int topR;
} ToStack;
void ToInitStack(ToStack *s) {
memset(s->data, 0, sizeof(int) * MaxSize);
s->topL = -1;
s->topR = MaxSize;
}
_Bool ToIsEmpty(ToStack *s, char type) {
if ((type == 'l' && s->topL == -1) || (type == 'r' && s->topR == MaxSize))
return 1;
else
return 0;
}
_Bool ToIsFull(ToStack *s) {
if (s->topR - s->topL > 1)
return 0;
else
return 1;
}
void ToPush(ToStack *s, char type, int val) {
if (!ToIsFull(s)) {
if (type == 'l')
s->data[++s->topL] = val;
else
s->data[--s->topR] = val;
} else
printf("STACK OVERFLOW !");
}
void ToPop(ToStack *s, char type) {
if (type == 'l') {
if (!ToIsEmpty(s, type)) {
s->data[s->topL] = 0;
s->topL--;
} else
printf("STACK IS EMPTY !");
} else {
if (!ToIsEmpty(s, type)) {
s->data[s->topR] = 0;
s->topR++;
} else
printf("STACK IS EMPTY !");
}
}
int ToGetTop(ToStack *s, char type) {
if (type == 'l')
return s->data[s->topL];
else
return s->data[s->topR];
}
typedef struct {
struct Stack *s1;
struct Stack *s2;
} Queue;
void InitQueue(Queue *q) {
memset(q->s1.data, 0, sizeof(int) * MaxSize);
q->s1.top = -1;
memset(q->s2.data, 0, sizeof(int) * MaxSize);
q->s2.top = -1;
}
_Bool QueueEmpty(Queue *q) {
if (q->s1->top == -1 && q->s2->top == -1)
return 1;
else
return 0;
}
void JoinQueue(Queue *q, char val) {
if (!isEmpty(q->s1))
Push(q->s1, val);
}
void LeaveQueue(Queue *q) {
while (q->s1.top != -1) {
Push(q->s2, q->s1->data[cur]);
q->s1.top--;
}
Pop(q->s2);
}
_Bool IsItLegal(char *str) {
int count = -1;
while (*str != '\0'){
if (*str == 'I'){
if (count < MaxSize - 1)
count++;
else return 0;
} else if (*str == 'O'){
if (count > -1)
count--;
else return 0;
}
str++;
}
return 1;
}
void DecimalToBinary(int val) {
Stack *s = (Stack*)malloc(sizeof(Stack));
InitStack(s);
do {
Push(s, val%2);
val /= 2;
}while (val);
while (s->top != -1){
printf("%d", getTop(s));
Pop(s);
}
}
void SaveData(Stack *s, int *val, int len) {
for (int i = 0; i < len; ++i) {
if (val[i] != -1){
if (!isFull(s)){
Push(s, val[i]);
printf("Not meet -1, Push %d\n", val[i]);
} else{
printf("STACK OVERFLOW !");
return;
}
} else{
if (!isEmpty(s)){
printf("Meet -1, Pop %d\n", getTop(s));
Pop(s);
} else{
printf("STACK EMPTY !");
return;
}
}
}
}
void SuffixExpression(Stack *s, char *str) {
while (*str != '\0'){
switch (*str) {
case '+':
while (s->top != -1){
if (getTop(s) == '*' || getTop(s) == '/' || getTop(s) == '-') {
printf("%c", getTop(s));
Pop(s);
}
if (getTop(s) == '(')
break;
}
Push(s, *str);
break;
case '-':
while (s->top != -1){
if (getTop(s) == '*' || getTop(s) == '/' || getTop(s) == '+') {
printf("%c", getTop(s));
Pop(s);
}
if (getTop(s) == '(')
break;
}
Push(s, *str);
break;
case '*':
Push(s, *str);
break;
case '/':
Push(s, *str);
break;
case '(':
Push(s, *str);
break;
case ')':
while (s->data[s->top] != '('){
printf("%c", getTop(s));
Pop(s);
}
Pop(s);
break;
default:
printf("%c", *str);
}
str++;
}
while (s->top != -1){
printf("%c", getTop(s));
Pop(s);
}
}
typedef struct {
int data[MaxSize];
int front;
int rear;
}Queue;
void Init(Queue *q){
memset(q->data, 0, sizeof(int) * MaxSize);
q->front = q->rear = 0;
}
判空
_Bool IsEmpty(Queue *q){
return q->rear == q->front;
}
_Bool IsFull(Queue *q){
return (q->rear + 1) % MaxSize == q->front;
}
void JoinQueue(Queue *q, int val){
if (!IsFull(q)){
q->data[q->rear] = val;
q->rear = (q->rear + 1) % MaxSize;
} else{
printf("QUEUE OVERFLOW !");
return;
}
}
void LeaveQueue(Queue *q){
if (!IsEmpty(q)){
q->data[q->front] = 0;
q->front = (q->front + 1) % MaxSize;
} else{
printf("QUEUE EMPTY !");
return;
}
}
int GetRear(Queue *q){
if (!IsEmpty(q))
return q->data[q->rear-1];
else return -1;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具