两个队列实现一个栈思路c语言,C语言实现,两个栈实现队列的功能

#include 

#include 

#define QUEUE_NULL 10000

typedef struct node {

int value;

struct node *next;

}NODE, *NODEP;

NODEP a = NULL;

NODEP b = NULL;

/* 初始化栈*/

NODEP init(NODEP top)

{

top = (NODEP)malloc(sizeof(NODE));

top->next = NULL;

return top;

}

/*入栈*/

void stack_push(NODEP top, int value)

{

NODEP new;

new = (NODEP)malloc(sizeof(NODE));

new->value = value;

new->next = top->next;

top->next = new;

return ;

}

/*出栈*/

int stack_pop(NODEP top)

{

NODEP now = top->next;

int a;

top->next = now->next;

a = now->value;

free(now);

return a;

}

/*判断栈是否为空*/

int stack_is_empty(NODEP top)

{

return (top->next == NULL)?1:0;

}

/*入队*/

void enqueue(int value)

{

stack_push(a, value);

}

/*出队*/

int dequeue()

{

if(stack_is_empty(b) == 0)

return stack_pop(b);

else{

if(stack_is_empty(a) == 1) {

printf("QUEUE IS EMPTY\n");

return QUEUE_NULL;

}

else {

int value_node;

while(stack_is_empty(a) != 1) {

value_node = stack_pop(a);

stack_push(b, value_node);

}

return stack_pop(b);

}

}

}

int main()

{

int input, value;

a = init(a);

b = init(b);

while(1){

printf("ENTER:(1:EN;2:DE)\n");

scanf("%d", &input);

if((input != 1 )&&(input != 2)){

printf("INPUT ERROR:\n");

continue;

}

if( input == 1){

printf("ENTER THE VALUE:\n");

scanf("%d", &value);

enqueue(value);

}

else{

value = dequeue();

if (value != QUEUE_NULL)

printf("%d\n", value);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值