1、两个栈模拟一个队列(优)
使用两个顺序栈来模拟一个队列
栈2必须空栈,栈1才能入栈2
从栈1到栈2必须一个不留地入
Queue
{
Stack* s1;
Stack* s2;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 定义一个栈的结构和基本操作
typedef struct {
int* elements;
int top;
int capacity;
} Stack;
// 初始化栈
void initStack(Stack* s, int capacity) {
s->elements = (int*)malloc(capacity * sizeof(int));
if (!s->elements) {
printf("Memory allocation failed\n");
exit(1);
}
s->top = -1; // 栈空时,top为-1
s->capacity = capacity;
}
// 检查栈是否为空
bool isEmpty(Stack* s) {
return s->top == -1;
}
// 入栈操作
void push(Stack* s, int value) {
if (s->top == s->capacity - 1) {
printf("Stack overflow\n");
return;
}
s->elements[++s->top] = value;
}
// 出栈操作
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow\n");
return -1; // 假设-1表示错误
}
return s->elements[s->top--];
}
// 判断序列b是否是序列a的出栈顺序
bool is_pop_stack(int a[], int b[], int len) {
Stack s;
initStack(&s, len); // 假设栈的容量足够大
int i = 0, j = 0;
while (i < len) {
push(&s, a[i++]); // 依次将a的元素入栈 1 2 54
// 尝试按b的顺序出栈
while (!isEmpty(&s) && s.elements[s.top] == b[j]) {
pop(&s); //3 1
j++;
}
// 如果栈为空但b中还有未匹配的元素,则不是合法的出栈顺序
if (isEmpty(&s) && j < len) {
free(s.elements);
return false;
}
}
// 检查栈是否为空,若为空则说明b完全匹配了a的出栈顺序
bool result = isEmpty(&s);
free(s.elements); // 释放栈占用的内存
return result;
}
int main() {
int a[] = { 1, 2, 3, 4, 5 };
int b1[] = { 1, 2, 3, 4, 5 };
int b2[] = { 3, 2, 1, 4, 5 };
int b3[] = { 3, 1, 2, 5, 4 };
int len = sizeof(a) / sizeof(a[0]);
printf("Is b1 a valid pop order? %s\n", is_pop_stack(a, b1, len) ? "Yes" : "No");
printf("Is b2 a valid pop order? %s\n", is_pop_stack(a, b2, len) ? "Yes" : "No");
printf("Is b3 a valid pop order? %s\n", is_pop_stack(a, b3, len) ? "Yes" : "No");
return 0;
}
2、两个栈模拟一个队列(良)
从栈1到栈2必须一个不留
栈2不空,栈1不能到栈2
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define TYPE int
//顺序栈结构
typedef struct ArrayStack
{
TYPE* ptr;//存储元素的内存首地址
size_t cap;//栈的容量
size_t top;//栈顶 从0开始空增栈(区别与满栈(栈顶下标从-1开始))
}ArrayStack;
//队列结构体定义
typedef struct Queue
{
ArrayStack* s1;
ArrayStack* s2;
}Queue;
//创建栈
ArrayStack* create_array_stack(size_t cap)
{
//为栈结构分配内存
//分配存储元素的内存
ArrayStack* stack =(ArrayStack*) malloc(sizeof(ArrayStack));
stack->ptr =(TYPE*) malloc(sizeof(TYPE) * cap);
stack->cap = cap;
stack->top = 0;
return stack;
}
//创建队列
Queue* create_queue(TYPE val)
{
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->s1 = create_array_stack(val);
queue->s2 = create_array_stack(val);
return queue;
}
//栈满
bool full_array_stack(ArrayStack* stack)
{
return stack->top >= stack->cap;
}
//入栈
bool push_array_stack(ArrayStack* stack, TYPE val)
{
// if(stack==pop)
if (full_array_stack(stack)) return false;
stack->ptr[stack->top] = val;
stack->top++;
return true;
}
//栈空
bool empty_array_stack(ArrayStack* stack)
{
return !stack->top;
}
//出栈 栈空
bool pop_array_stack(ArrayStack* stack)
{
// if(stack->top==0)
if (empty_array_stack(stack)) return false;
stack->top--;
return true;
}
//取栈顶元素
bool top_array_stack(ArrayStack* stack, TYPE* val)
{
if (empty_array_stack(stack)) return false;
*val = stack->ptr[stack->top - 1];
return true;
}
bool enque(Queue* queue, int element)
{
if (full_array_stack(queue->s1)) return false;
push_array_stack(queue->s1,element);
return true;
}
bool dequeue(Queue* queue, int* element)
{
if (empty_array_stack(queue->s2))
{
while (!empty_array_stack(queue->s1))
{
int temp;
top_array_stack(queue->s1, &temp);
pop_array_stack(queue->s1);
push_array_stack(queue->s2, temp);
}
if (empty_array_stack(queue->s2))
{
return false;
}
}
top_array_stack(queue->s2, element);
pop_array_stack(queue->s2);
}
int main(int argc, const char* argv[])
{
Queue* queue = create_queue(5);
for (int i = 0;i < 5;i++)
{
enque(queue, i);
}
//删除出栈测试
int elemnt;
for (int i = 0; i < 5; i++)
{
dequeue(queue, &elemnt);
printf("%d\n", elemnt);
}
return 0;
}