[LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)
来源:互联网 发布:,知其雄守其雌! 编辑:程序博客网 时间:2024/06/05 19:02
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.
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis 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).
实现一
思路
s1是入栈的,s2是出栈的。
- 入队列,直接压到s1是就行了
- 出队列,先把s1中的元素全部出栈压入到s2中,弹出s2中的栈顶元素;再把s2的所有元素全部压回s1中
实现二
思路
s1是入栈的,s2是出栈的。保证所有元素都在一个栈里面
- 入队列时:如果s1为空,把s2中所有的元素倒出压到s1中;否则直接压入s1
- 出队列时:如果s2不为空,把s2中的栈顶元素直接弹出;否则,把s1的所有元素全部弹出压入s2中,再弹出s2的栈顶元素
比较:与实现一相比较,出队列时不必每次都捣鼓了。
实现三
思路
s1是入栈的,s2是出栈的。
- 入队列:直接压入s1即可
- 出队列:如果s2不为空,把s2中的栈顶元素直接弹出;否则,把s1的所有元素全部弹出压入s2中,再弹出s2的栈顶元素
比较
与实现二相比较,入队直接入即可,感觉此时已是最优。
代码实现如下:// LeetCode232-Implement Queue using Stacks.cpp : 定义控制台应用程序的入口点。////两个栈模拟一个队列//Written by ZP1015//2015.10.21#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#include <string.h>struct STACK_NODE{ int* pData;/*数组,长度为StackLenMax*/ int top;//栈顶指针的位置 int StackLenMax;};typedef struct {struct STACK_NODE *pStackNode_First;struct STACK_NODE* pStackNode_Second;} Queue;struct STACK_NODE* alloc_stack(int StackSize){ if(StackSize <= 0) return NULL;struct STACK_NODE* pStackNode = NULL; pStackNode = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE)); if(NULL == pStackNode) {return NULL; } memset(pStackNode, 0, sizeof(struct STACK_NODE)); pStackNode->pData = (int *)malloc(sizeof(int)*StackSize); if(NULL == pStackNode->pData){goto malloc_failed; } memset(pStackNode->pData, 0, sizeof(int) * StackSize); pStackNode->top= -1; /*初始化从0开始*/pStackNode->StackLenMax = StackSize; return pStackNode;malloc_failed:free(pStackNode);return NULL;}int free_stack(struct STACK_NODE* pStackNode){ if(NULL == pStackNode) return -1; if(NULL == pStackNode->pData) {free(pStackNode);return -1;} free(pStackNode->pData); free(pStackNode); return 0;}int stack_push(struct STACK_NODE* pStackNode, int value){/*1.异常处理*/ if(NULL == pStackNode) return -1;if(NULL == pStackNode->pData) {return -1;}/*2.栈满,不能压入元素*/ if(pStackNode->top == pStackNode->StackLenMax-1) return -1;++pStackNode->top; pStackNode->pData[pStackNode->top] = value; return 0;}int stack_pop(struct STACK_NODE* pStackNode, int* value){ if(NULL == pStackNode || NULL == value) return -1; if(-1 == pStackNode->top) return -1; *value = pStackNode->pData[pStackNode->top];pStackNode->top--; return 0;}int count_stack_number(struct STACK_NODE* pStackNode){ return (pStackNode->top+1);}void print_stack_node(struct STACK_NODE *pStackNode) {/*1.输入的参数有误*/ if(NULL == pStackNode) {printf("[%d] pStackNode is illegal! \n",__LINE__);return; }/*2.输入的链式堆栈为空*/if(-1 == pStackNode->top) {printf("[%d] pStackNode is empty!\n",__LINE__);return ;}struct STACK_NODE *pStackNodeTemp = pStackNode;int count = 0;while(count <= pStackNode->top) {printf("%d ",pStackNodeTemp->pData[count]);count++;;}printf("\n");}/* Create a queue */void queueCreate(Queue *queue, int maxSize) {if(!queue) {return;}if(maxSize<=0)return;/*创建两个栈*/ queue->pStackNode_First = alloc_stack(maxSize);queue->pStackNode_Second = alloc_stack(maxSize);}/* Push element x to the back of queue */void queuePush(Queue *queue, int element) {if(!queue) return;/*1.栈1入队列,实现入栈功能*/ stack_push(queue->pStackNode_First,element);}/* Removes the element from front of queue */void queuePop(Queue *queue) {/*1.链表2为空*/int a = 0; if(queue->pStackNode_Second->top == -1) {while(queue->pStackNode_First->top != -1) {stack_pop(queue->pStackNode_First,&a);stack_push(queue->pStackNode_Second,a);}}if(queue->pStackNode_Second->top == -1)return;/*栈2弹栈,实现出队列功能*/stack_pop(queue->pStackNode_Second,&a);}/* Get the front element */int queuePeek(Queue *queue) {if(!queue) return -1; /*1.链表2为空*/int a = 0; if(queue->pStackNode_Second->top == -1) {while(queue->pStackNode_First->top != -1) {stack_pop(queue->pStackNode_First,&a);stack_push(queue->pStackNode_Second,a);}}if(queue->pStackNode_Second->top == -1)return -1;/*栈2弹栈,实现出队列功能*/return (queue->pStackNode_Second->pData[queue->pStackNode_Second->top]);} /* Return whether the queue is empty */bool queueEmpty(Queue *queue) {if(!queue) return 1;if(queue->pStackNode_Second->top == -1&&queue->pStackNode_First->top == -1)return 1;return 0;}/* Destroy the queue */void queueDestroy(Queue *queue) {if(!queue) return;free_stack(queue->pStackNode_First);free_stack(queue->pStackNode_Second); }void print_queue_node(Queue *queue) {/*1.输入的参数有误*/ if(!queue) return;print_stack_node(queue->pStackNode_First);print_stack_node(queue->pStackNode_Second);}
全部代码实现:
// LeetCode232-Implement Queue using Stacks.cpp : 定义控制台应用程序的入口点。////两个栈模拟一个队列//Written by ZP1015//2015.10.21#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#include <string.h>struct STACK_NODE{ int* pData;/*数组,长度为StackLenMax*/ int top;//栈顶指针的位置 int StackLenMax;};typedef struct {struct STACK_NODE *pStackNode_First;struct STACK_NODE* pStackNode_Second;} Queue;struct STACK_NODE* alloc_stack(int StackSize){ if(StackSize <= 0) return NULL;struct STACK_NODE* pStackNode = NULL; pStackNode = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE)); if(NULL == pStackNode) {return NULL; } memset(pStackNode, 0, sizeof(struct STACK_NODE)); pStackNode->pData = (int *)malloc(sizeof(int)*StackSize); if(NULL == pStackNode->pData){goto malloc_failed; } memset(pStackNode->pData, 0, sizeof(int) * StackSize); pStackNode->top= -1; /*初始化从0开始*/pStackNode->StackLenMax = StackSize; return pStackNode;malloc_failed:free(pStackNode);return NULL;}int free_stack(struct STACK_NODE* pStackNode){ if(NULL == pStackNode) return -1; if(NULL == pStackNode->pData) { free(pStackNode);return -1;} free(pStackNode->pData); free(pStackNode); return 0;}int stack_push(struct STACK_NODE* pStackNode, int value){/*1.异常处理*/ if(NULL == pStackNode) return -1;if(NULL == pStackNode->pData) {return -1;}/*2.栈满,不能压入元素*/ if(pStackNode->top == pStackNode->StackLenMax-1) return -1;++pStackNode->top; pStackNode->pData[pStackNode->top] = value; return 0;}int stack_pop(struct STACK_NODE* pStackNode, int* value){ if(NULL == pStackNode || NULL == value) return -1; if(-1 == pStackNode->top) return -1; *value = pStackNode->pData[pStackNode->top];pStackNode->top--; return 0;}int count_stack_number(struct STACK_NODE* pStackNode){ return (pStackNode->top+1);}void print_stack_node(struct STACK_NODE *pStackNode) {/*1.输入的参数有误*/ if(NULL == pStackNode) {printf("[%d] pStackNode is illegal! \n",__LINE__);return; }/*2.输入的链式堆栈为空*/if(-1 == pStackNode->top) {printf("[%d] pStackNode is empty!\n",__LINE__);return ;}struct STACK_NODE *pStackNodeTemp = pStackNode;int count = 0;while(count <= pStackNode->top) {printf("%d ",pStackNodeTemp->pData[count]);count++;;}printf("\n");}/* Create a queue */void queueCreate(Queue *queue, int maxSize) {if(!queue) {return;}if(maxSize<=0)return;/*创建两个栈*/ queue->pStackNode_First = alloc_stack(maxSize);queue->pStackNode_Second = alloc_stack(maxSize);}/* Push element x to the back of queue */void queuePush(Queue *queue, int element) {if(!queue) return;/*1.栈1入队列,实现入栈功能*/ stack_push(queue->pStackNode_First,element);}/* Removes the element from front of queue */void queuePop(Queue *queue) {/*1.链表2为空*/int a = 0; if(queue->pStackNode_Second->top == -1) {while(queue->pStackNode_First->top != -1) {stack_pop(queue->pStackNode_First,&a);stack_push(queue->pStackNode_Second,a);}}if(queue->pStackNode_Second->top == -1)return;/*栈2弹栈,实现出队列功能*/stack_pop(queue->pStackNode_Second,&a);}/* Get the front element */int queuePeek(Queue *queue) {if(!queue) return -1; /*1.链表2为空*/int a = 0; if(queue->pStackNode_Second->top == -1) {while(queue->pStackNode_First->top != -1) {stack_pop(queue->pStackNode_First,&a);stack_push(queue->pStackNode_Second,a);}}if(queue->pStackNode_Second->top == -1)return -1;/*栈2弹栈,实现出队列功能*/return (queue->pStackNode_Second->pData[queue->pStackNode_Second->top]);} /* Return whether the queue is empty */bool queueEmpty(Queue *queue) {if(!queue) return 1;if(queue->pStackNode_Second->top == -1&&queue->pStackNode_First->top == -1)return 1;return 0;}/* Destroy the queue */void queueDestroy(Queue *queue) {if(!queue) return;free_stack(queue->pStackNode_First);free_stack(queue->pStackNode_Second); }void print_queue_node(Queue *queue) {/*1.输入的参数有误*/ if(!queue) return;print_stack_node(queue->pStackNode_First);print_stack_node(queue->pStackNode_Second);}int main(){Queue pStackNode;queueCreate(&pStackNode,20);int i = 0;for (i = 0;i<10;i++) {queuePush(&pStackNode,i);/*入栈*/}print_queue_node(&pStackNode);/*出栈*/queuePop(&pStackNode);print_queue_node(&pStackNode);/*获得栈顶元素*/printf("top %d\n",queuePeek(&pStackNode));queueDestroy(&pStackNode);getchar();getchar();return 0;}
1 0
- [LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)
- leetcode:用两个栈实现一个队列 Implement Queue using Stacks
- LeetCode(23)-Implement Queue using Stacks(栈实现队列)
- Implement Queue using Stacks 两个栈实现队列
- LeetCode Implement Queue using Stacks 栈&队列
- LeetCode 232 Implement Queue using Stacks(利用栈实现队列)
- leetcode 232. Implement Queue using Stacks 双栈实现队列
- 队列-Implement Queue using Stacks(用栈实现队列)
- Implement Queue using Stacks(用栈实现队列)
- 232. Implement Queue using Stacks (用栈实现队列)
- LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
- LeetCode OJ 之 Implement Queue using Stacks(使用栈实现队列)
- LeetCode 232. Implement Queue using Stacks(用栈实现队列)
- LeetCode(232) Implement Queue using Stacks
- leetcode 232. Implement Queue using Stacks-栈模拟队列
- (LeetCode)Implement Queue using Stacks --- 实现队列和栈第一部分,队列
- Implement Queue using Stacks 用俩栈实现队列
- 232.Implement Queue using Stacks(用栈实现队列)
- 1003. 我要通过!(20)
- UITextField去除用户SB而输入空格,和设置智能输入数字的方法
- LaTeX新人教程,30分钟从完全陌生到基本入门
- 抄袭、借鉴与创新,有什么区别?
- springmvc + shiro 配置(二、其他配置文件)
- [LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)
- 未来蓝牙将会分为三种:。Bluetooth Smart Ready、Bluetooth Smart、以及标准 Bluetooth
- 紫金桥软件与上润仪表通讯的调试
- centos mysql 基本操作
- OpenStack日志管理&配置管理安全性分析
- matlab中bwareaopen函数详细介绍及应用
- Activiti 5.18 mysql配置
- 快速高效学习Java编程在线资源Top 20
- windows和linux系统文件传输