[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); }}
- [C语言][LeetCode][232]Implement Queue using Stacks
- 【LeetCode-232】 Implement Queue using Stacks(C++)
- Leetcode-Implement Queue using Stacks(C++)
- leetcode 232:Implement Queue using Stacks
- LeetCode 232: Implement Queue using Stacks
- Implement Queue using Stacks(leetcode 232)
- [Leetcode 232, Easy] Implement Queue using Stacks
- LeetCode---(232)Implement Queue using Stacks
- Leetcode[232] Implement Queue using Stacks
- leetcode-232-Implement Queue using Stacks
- leetCode #232 Implement Queue using Stacks
- leetcode: (232) Implement Queue using Stacks
- leetcode 232: Implement Queue using Stacks
- leetcode 232 Implement Queue using Stacks
- Leetcode #232 Implement Queue using Stacks
- [LeetCode 232] Implement Queue using Stacks
- 【LeetCode 232: Implement Queue using Stacks】
- Leetcode NO.232 Implement Queue using Stacks
- 机器学习数据来源总结(不断积累更新)
- unity3D-游戏/AR/VR在线就业班 蓝鸥C#入门字典学习笔记
- 8面向对象高级--->@property
- adb logcat常用命令小结
- Python之队列queue模块使用 常见问题与用法
- [C语言][LeetCode][232]Implement Queue using Stacks
- AS400 DB2 SQL常用函数总结
- LeetCode - Validate Binary Search Tree
- Linux下使用math函数
- LVS
- 最长公共子序列LCS和最长回文子序列的动态规划算法
- unity3D-游戏/AR/VR在线就业班 蓝鸥C#入门栈和队列学习笔记
- Nginx+iis 负载均衡
- Struts2实现的吊炸天留言板系统(包含完整源+mysql数据库+bootstraps3)源码以及课程报告下载地址!