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

来源:互联网 发布:算法导论 22.3 10 编辑:程序博客网 时间:2024/06/07 08:48

题目

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);    }}
0 0