[C语言][LeetCode][225]Implement Stack using Queues

题目

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值