#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);
}
}
}