#define max 100
typedef struct stack
{
int top
;
int data
[max
];
}Stack
;
typedef struct {
Stack a1
;
Stack a2
;
} MyQueue
;
MyQueue
* myQueueCreate() {
MyQueue
*queue
=(MyQueue
*)malloc(sizeof(MyQueue
));
queue
->a1
.top
=-1;
queue
->a2
.top
=-1;
return queue
;
}
void myQueuePush(MyQueue
* obj
, int x
) {
if(obj
->a1
.top
<max
){
while(obj
->a1
.top
!=-1){
obj
->a2
.data
[++(obj
->a2
.top
)]=obj
->a1
.data
[(obj
->a1
.top
)--];
}
obj
->a1
.data
[++(obj
->a1
.top
)]=x
;
while(obj
->a2
.top
!=-1){
obj
->a1
.data
[++(obj
->a1
.top
)]=obj
->a2
.data
[(obj
->a2
.top
)--];
}
}
}
int myQueuePop(MyQueue
* obj
) {
if(obj
->a1
.top
!=-1){
return obj
->a1
.data
[obj
->a1
.top
--];
}
return 0;
}
int myQueuePeek(MyQueue
* obj
) {
return obj
->a1
.data
[obj
->a1
.top
];
}
bool
myQueueEmpty(MyQueue
* obj
) {
if(obj
->a1
.top
==-1) return true
;
return false
;
}
void myQueueFree(MyQueue
* obj
) {
free(obj
);
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-5944.html