题目
Implement Stack using Queues
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
标签
Stack、Design
难度
简单
分析
题目意思是通过队列的操作,来实现栈的基本API。
实现思路是设计两个队列,栈的push操作时,先将队列A的元素从头到尾挪到队列B,然后再将新的元素放入队列A,然后再将队列B的元素从头到尾放回队列A,这样就完成了栈的push操作,相当于新插入的元素,放到了队列的front(栈的push操作,其实就是放到栈顶),其他操作类似队列的操作。
C代码实现
typedef int ElementType;
typedef struct QUEUE_T
{
ElementType value;
//int length;
struct QUEUE_T *next;
}QUEUE;
typedef struct QUEUE_T Node;
#define QUEUE_MAX_LEN 10
QUEUE * queue_create(void)
{
QUEUE * queue = (QUEUE *)malloc(sizeof(QUEUE));
if(NULL == queue)
{
printf("queue malloc fail\n");
return NULL;
}
memset(queue, 0, sizeof(QUEUE));
//queue->length = 0;
queue->next = NULL;
return queue;
}
bool queue_is_empty(QUEUE *queue)
{
return queue->next == NULL;
}
#if 0
bool queue_is_full(QUEUE *queue)
{
return queue->length == QUEUE_MAX_LEN;
}
#endif
ElementType queue_front(QUEUE *queue)
{
QUEUE * q = queue;
if(!q->next)
return -1;
while(q->next)
q = q->next;
return q->value;
}
ElementType queue_back(QUEUE *queue)
{
return queue->next->value;
}
int queue_push(QUEUE *queue, ElementType elem)
{
Node *node = (Node *)malloc(sizeof(Node));
if(NULL == node)
{
printf("node malloc fail\n");
return -1;
}
memset(node, 0, sizeof(Node));
memcpy(&node->value, &elem, sizeof(ElementType));
node->next = queue->next;
queue->next = node;
//queue->length++;
return 0;
}
void queue_pop(QUEUE *queue)
{
QUEUE *q,*temp;
q = temp = queue;
if(!queue || !queue->next)
return;
while(q->next)
{
temp = q;
q = q->next;
}
temp->next = NULL;
//queue->length--;
free(q);
}
typedef struct {
QUEUE * queueA;
QUEUE * queueB;
} Stack;
/* Create a stack */
void stackCreate(Stack *stack, int maxSize) {
stack->queueA = queue_create();
stack->queueB = queue_create();
}
/* Push element x onto stack */
void stackPush(Stack *stack, int element) {
while(!queue_is_empty(stack->queueA))
{
queue_push(stack->queueB, queue_front(stack->queueA));
queue_pop(stack->queueA);
}
queue_push(stack->queueA, element);
while(!queue_is_empty(stack->queueB))
{
queue_push(stack->queueA, queue_front(stack->queueB));
queue_pop(stack->queueB);
}
}
/* Removes the element on top of the stack */
void stackPop(Stack *stack) {
queue_pop(stack->queueA);
}
/* Get the top element */
int stackTop(Stack *stack) {
return queue_front(stack->queueA);
}
/* Return whether the stack is empty */
bool stackEmpty(Stack *stack) {
return queue_is_empty(stack->queueA);
}
/* Destroy the stack */
void stackDestroy(Stack *stack) {
if(stack)
{
free(stack->queueA);
free(stack->queueB);
}
}