栈和队列
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {int capacity; int top; int* array;
} Stack;
Stack* createStack(int size) {Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->capacity = size; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack;
}
void push(Stack* stack, int item) {if (stack->top >= stack->capacity - 1) { printf("栈已满,无法入栈!\n");return;}stack->array[++stack->top] = item;
}
int pop(Stack* stack) {if (stack->top == -1) { printf("栈为空,无法出栈!\n");return INT_MIN; }int item = stack->array[stack->top]; stack->top--; return item;
}
#include <stdio.h>
#include <stdlib.h>typedef struct Queue {int front; int rear; int capacity;int* array;
} Queue;Queue* createQueue(int size) {
}int isFull(Queue* queue) {return (queue->rear == queue->capacity - 1);
}int isEmpty(Queue* queue) {return (queue->front == -1);
}void enqueue(Queue* queue, int item) {if (isFull(queue)) {printf("队列已满,无法入队!\n");return;}queue->rear = (queue->rear + 1) % queue->capacity;queue->array[queue->rear] = item;
}int dequeue(Queue* queue) {if (isEmpty(queue)) {printf("队列为空,无法出队!\n");return INT_MIN;}int item = queue->array[queue->front]; queue->front = (queue->front + 1) % queue->capacity;return item;
}