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

#include <stdio.h>
#include <string.h>

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

}

相关文章:

  • 2022-12-23
  • 2021-05-31
  • 2021-07-06
  • 2021-05-26
  • 2021-12-29
  • 2022-12-23
  • 2021-08-22
猜你喜欢
  • 2021-05-01
  • 2021-11-08
  • 2021-07-31
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案