用两个顺序栈来模拟队列

 

#include <stdio.h>

#include <stdbool.h>

#include <stdlib.h>

#define TYPE int

// 设计顺序栈结构

typedef struct ArrayStack

{

TYPE* ptr; // 存储元素内存首地址

size_t cal; // 栈的容量

size_t top; // 栈顶下标 从0开始 空增栈

}ArrayStack;

// 创建

ArrayStack* create_array_stack(size_t cal)

{

// 为栈结构分配内存

ArrayStack* stack = malloc(sizeof(ArrayStack));

// 分配存储元素的内存

stack->ptr = malloc(sizeof(TYPE)*cal);

// 记录栈容量

stack->cal = cal;

// 记录栈顶位置,接下来要入栈的位置

stack->top = 0;

return stack;

}

// 销毁

void destory_array_stack(ArrayStack* stack)

{

free(stack->ptr);

free(stack);

}

// 栈空

bool empty_array_stack(ArrayStack* stack)

{

return 0 == stack->top;

}

// 栈满

bool full_array_stack(ArrayStack* stack)

{

return stack->top >= stack->cal;

}

// 入栈

bool push_array_stack(ArrayStack* stack,TYPE val)

{

if(full_array_stack(stack)) return false;

stack->ptr[stack->top++] = val;

return true;

}

// 出栈

bool pop_array_stack(ArrayStack* stack)

{

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 is_pop_stack(int a[],int b[],int len)

{

// 创建一个栈

ArrayStack* stack = create_array_stack(len);

for(int i=0,j=0; i<len; i++)

{

// 按a的顺序一个个入栈

push_array_stack(stack,a[i]);

// 按b的顺序尝试出栈

int val = 0;

while(top_array_stack(stack,&val) &&

val == b[j])

{

pop_array_stack(stack);

j++;

}

}

// 判断栈是否为空

bool flag = empty_array_stack(stack);

destory_array_stack(stack);

return flag;

}

/* 使用两个栈模拟队列功能 */

typedef struct Queue

{

ArrayStack* s1;

ArrayStack* s2;

}Queue;

// 创建队列

Queue* create_queue(size_t cal)

{

Queue* queue = malloc(sizeof(Queue));

queue->s1 = create_array_stack(cal);

queue->s2 = create_array_stack(cal);

return queue;

}

// 队满

bool full_queue(Queue* queue)

{

return full_array_stack(queue->s1) &&

full_array_stack(queue->s2);

}

// 队空

bool empty_queue(Queue* queue)

{

return empty_array_stack(queue->s1) &&

empty_array_stack(queue->s2);

}

// 入队

bool push_queue(Queue* queue,TYPE val)

{

if(full_array_stack(queue->s1))

{

if(!empty_array_stack(queue->s2)) return false;

while(!empty_array_stack(queue->s1))

{

TYPE top = 0;

top_array_stack(queue->s1,&top);

pop_array_stack(queue->s1);

push_array_stack(queue->s2,top);

}

}

printf("tail:%d\n",val); //测试

return push_array_stack(queue->s1,val);

}

// 出队

bool pop_queue(Queue* queue)

{

if(empty_array_stack(queue->s2))

{

if(empty_array_stack(queue->s1)) return false;

while(!empty_array_stack(queue->s1))

{

TYPE top = 0;

top_array_stack(queue->s1,&top);

pop_array_stack(queue->s1);

push_array_stack(queue->s2,top);

}

}

TYPE top = 0;

top_array_stack(queue->s2,&top);

printf("top:%d\n",top); //测试

return pop_array_stack(queue->s2);

}

int main(int argc,const char* argv[])

{

Queue* queue = create_queue(10);

for(int i=0; i<20; i++)

{

push_queue(queue,i);

}

while(!empty_queue(queue))

{

pop_queue(queue);

}

/*

int a[5] = {1,2,3,4,5};

int b[5] = {3,2,1,5,4};

printf("%s\n",is_pop_stack(a,b,5)?"Yes":"No");

ArrayStack* stack = create_array_stack(10);

TYPE top_num = -100;

for(int i=0; i<10; i++)

{

push_array_stack(stack,i+1);

top_array_stack(stack,&top_num) &&

printf("top:%d\n",top_num);

}

printf("------------------\n");

while(!empty_array_stack(stack))

{

top_array_stack(stack,&top_num);

printf("top:%d\n",top_num);

pop_array_stack(stack);

}

*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoyu1381

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值