这道题思路很清晰明了
typedef struct {
struct stack{
int data[10000];
int top;
}stack[2];
} CQueue;
CQueue* cQueueCreate() {
CQueue *result = (CQueue*) malloc(sizeof(CQueue));
result->stack[0].top = -1;
result->stack[1].top = -1;
return result;
}
void cQueueAppendTail(CQueue* obj, int value) {
obj->stack[0].top += 1;
obj->stack[0].data[obj->stack[0].top] = value;
}
int cQueueDeleteHead(CQueue* obj) {
if(obj->stack[0].top == -1) return -1;
while(obj->stack[0].top>0)
{
obj->stack[1].top += 1;
obj->stack[1].data[obj->stack[1].top] = obj->stack[0].data[obj->stack[0].top--];
}
int temp = obj->stack[0].data[obj->stack[0].top--];
while(obj->stack[1].top>-1)
{
obj->stack[0].top += 1;
obj->stack[0].data[obj->stack[0].top] = obj->stack[1].data[obj->stack[1].top--];
}
return temp;
}
void cQueueFree(CQueue* obj) {
free(obj);
}
/**
* Your CQueue struct will be instantiated and called as such:
* CQueue* obj = cQueueCreate();
* cQueueAppendTail(obj, value);
* int param_2 = cQueueDeleteHead(obj);
* cQueueFree(obj);
*/
网友评论