C语言用两个栈来实现队列

剑指offer的面试题7.用两个栈来实现队列

#include
#include

#define MAX 1000
typedef struct Stacks{
int date[MAX];
int top;
}Stack;

Stack stack1,stack2;

void CreateStack(){
stack1.top = -1;
stack2.top = -1;
}
void PushStack(int value){
if(stack1.top!=MAX){
stack1.date[++stack1.top] = value;
}
}
int PopStack(){
int value;
if(stack1.top!=-1&&stack2.top==-1){
while(stack1.top!=-1){
stack2.date[++stack2.top] = stack1.date[stack1.top--];
}
value = stack2.date[stack2.top];
stack2.top--;
}
else if(stack2.top!=-1){
value = stack2.date[stack2.top--];
}
return value;
}
int main()
{
CreateStack();
int i;
for(i = 0 ; i < 3; i++){
PushStack(i);
}
printf("%d",PopStack());
printf("\n");
PushStack(3);
for(i = 0 ;i < 3; i++){
printf("%d",PopStack());
}

}

转载于:https://www.cnblogs.com/CloudStrife/p/7212156.html

你可能感兴趣的:(C语言用两个栈来实现队列)