//栈的简单实现
#include<stdio.h>;
#include<stdlib.h>;
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
}Stack;
void initialize(Stack* stack) {
stack->top = -1;
}
void push(Stack* stack, int value) {
if (stack->top >= MAX_SIZE - 1) {
printf("Stack overflow\n");
return;
}
stack->data[++stack->top] = value;
printf("%d pushed to stack\n",value);
}
int pop(Stack* stack) {
if (stack->top == -1) {
printf("Stack is empty\n");
return -1;
}
return stack->data[--stack->top];
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
void printStack(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
for (int i = stack->top; i >= 0; i--) {
printf("%d", stack->data[i]);
}
printf("\n");
}
int peek(Stack* stack) {
if (stack->top == -1) {
printf("Stack is empty\n");
return -1;
}
return stack->data[stack->top];
}
int main() {
Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
printf("Top element : %d\n", peek(&stack));
int popped = pop(&stack);
printf("Popped element: %d\n", popped);
printStack(&stack);
return 0;
}
-
栈的定义和特性:
- 栈是一种具有后进先出(LIFO)特性的线性数据结构,只能在栈顶进行插入和删除操作。
- 栈可以使用数组或链表来实现。
-
栈的基本操作:
- 初始化栈(InitStack):创建一个空栈。
- 入栈(Push):将元素压入栈顶。
- 出栈(Pop):从栈顶弹出一个元素。
- 获取栈顶元素(Top):返回栈顶元素的值,但不对栈做修改。
- 判断栈空(IsEmpty):判断栈是否为空。
- 获取栈的大小(GetSize):返回栈中元素的个数。
-
栈的应用:
- 表达式求值:使用栈来处理中缀表达式转换为后缀表达式,并进行计算。
- 括号匹配:使用栈来检查括号的匹配情况。
- 函数调用:函数调用时,使用栈来保存调用现场和返回地址。
-
栈的应用题目的解题思路:
- 栈的应用题目通常涉及到栈的入栈和出栈操作,需要根据具体问题设计合适的算法。
- 例如,解决括号匹配问题可以使用栈来检查左右括号的匹配情况,解决迷宫问题可以使用栈来记录路径。
-
栈的实现方式:
- 顺序栈(使用数组实现):需要定义栈顶指针和数组来存储栈元素。
- 链式栈(使用链表实现):使用链表节点来表示栈元素,需要维护栈顶指针。
-
//栈应用案例 //1 表达式求值 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_SIZE 100 typedef struct { int data[MAX_SIZE]; int top; } Stack; void initStack(Stack* stack) { stack->top = -1; } void push(Stack* stack, int value) { if (stack->top == MAX_SIZE - 1) { printf("Stack overflow\n"); exit(1); } stack->data[++stack->top] = value; } int pop(Stack* stack) { if (stack->top == -1) { printf("Stack underflow\n"); exit(1); } return stack->data[stack->top--]; } int evaluatePostfix(char postfix[]) { Stack stack; initStack(&stack); int i, operand1, operand2, result; for (i = 0; postfix[i] != '\0'; i++) { if (isdigit(postfix[i])) { push(&stack, postfix[i] - '0'); } else { operand2 = pop(&stack); operand1 = pop(&stack); switch (postfix[i]) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } push(&stack, result); } } return pop(&stack); } int main() { char postfix[] = "34+2*"; int result = evaluatePostfix(postfix); printf("Result: %d\n", result); return 0; } //2 括号匹配 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 typedef struct { char data[MAX_SIZE]; int top; } Stack; void initStack(Stack* stack) { stack->top = -1; } void push(Stack* stack, char value) { if (stack->top == MAX_SIZE - 1) { printf("Stack overflow\n"); exit(1); } stack->data[++stack->top] = value; } char pop(Stack* stack) { if (stack->top == -1) { printf("Stack underflow\n"); exit(1); } return stack->data[stack->top--]; } int isMatchingPair(char character1, char character2) { if (character1 == '(' && character2 == ')') return 1; else if (character1 == '{' && character2 == '}') return 1; else if (character1 == '[' && character2 == ']') return 1; else return 0; } int isBalanced(char expression[]) { Stack stack; initStack(&stack); int i; char currentCharacter; for (i = 0; i < strlen(expression); i++) { currentCharacter = expression[i]; if (currentCharacter == '(' || currentCharacter == '{' || currentCharacter == '[') { push(&stack, currentCharacter); } else if (currentCharacter == ')' || currentCharacter == '}' || currentCharacter == ']') { if (stack.top == -1 || !isMatchingPair(pop(&stack), currentCharacter)) return 0; } } return stack.top == -1; // 栈为空时才表示括号匹配成功 } int main() { char expression[] = "{()}[]"; if (isBalanced(expression)) printf("Balanced\n"); else printf("Not balanced\n"); return 0; } //函数调用 #include <stdio.h> typedef struct { int value; } StackFrame; void foo(int n) { StackFrame frame; frame.value = n; printf("Entering foo(%d)\n", frame.value); if (n > 0) { foo(n - 1); } printf("Exiting foo(%d)\n", frame.value); } int main() { foo(3); return 0; }
以上是具体的案例讲解。