[C语言][LeetCode][232]Implement Queue using Stacks

题目

Implement Queue using Stacks
Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.

Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

标签

Stack、Design

难度

简单

分析

题目意思是通过栈的操作,来实现队列的基本API。
实现思路是设计两个栈,队列的push操作时,先将栈A的元素挪到栈B,然后再将新的元素放入栈A,然后再将栈B的元素放回栈A,这样队列的push操作,就放到了栈底(类似队列的尾),其他操作类似栈的操作。

C代码实现

typedef int ElemType;

typedef struct STACK_T 
{
    ElemType value;
    struct STACK_T * next;
}STACK;

typedef struct STACK_T NODE;


STACK * stack_init(void)
{
    STACK * stack = (STACK *)malloc(sizeof(STACK));
    if(!stack)
    {
        printf("malloc stack fail\n");
        return NULL;
    }

    memset(stack, 0, sizeof(STACK));
    stack->next = NULL;

    return stack;
}

bool stack_is_empty(STACK * stack)
{
    return (stack->next == NULL);
}

ElemType stack_pop(STACK * stack)
{
    ElemType retValue;

    STACK * temp = NULL;

    if(!stack_is_empty(stack))
    {
        temp = stack->next;
        stack->next = stack->next->next;
        retValue = temp->value;
        free(temp);
    }
    else
    {
        printf("stack is empty\n");
        return 0;
    }

    return retValue;
}

int stack_push(STACK * stack, ElemType ele)
{
    NODE * node = (NODE *)malloc(sizeof(NODE));

    node->value = ele;
    node->next = stack->next;
    stack->next = node;

    return 0;
}

ElemType stack_top(STACK * stack)
{
    if(!stack_is_empty(stack))
    {
        return stack->next->value;
    }

    return (ElemType)(-1);
}

int stack_traverse(STACK * stack)
{    
    printf("\nStack have follow elements : \n");

    while(stack && stack->next)
    {
        printf("element=%d\n", stack->next->value);
        stack = stack->next;
    }

    return 0;
}


typedef struct {
    STACK * stackA;
    STACK * stackB;
} Queue;

/* Create a queue */
void queueCreate(Queue *queue, int maxSize) {
    queue->stackA = stack_init();
    queue->stackB = stack_init();
}

/* Push element x to the back of queue */
void queuePush(Queue *queue, int element) {
    while(!stack_is_empty(queue->stackA))
    {
        stack_push(queue->stackB, stack_top(queue->stackA));
        stack_pop(queue->stackA);
    }

    stack_push(queue->stackA, element);

    while(!stack_is_empty(queue->stackB))
    {
        stack_push(queue->stackA, stack_top(queue->stackB));
        stack_pop(queue->stackB);
    }
}

/* Removes the element from front of queue */
void queuePop(Queue *queue) {
    stack_pop(queue->stackA);
}

/* Get the front element */
int queuePeek(Queue *queue) {
    return stack_top(queue->stackA);
}

/* Return whether the queue is empty */
bool queueEmpty(Queue *queue) {
    return stack_is_empty(queue->stackA);
}

/* Destroy the queue */
void queueDestroy(Queue *queue) {
    if(queue)
    {
        free(queue->stackA);
        free(queue->stackB);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值