您的位置:首页 > 产品设计 > UI/UE

[LeetCode-225] Implement Stack using Queues(两个队列实现栈)

2015-10-21 18:07 441 查看
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).

Update (2015-06-11):

The class name of the Java function had been updated to MyStack instead of Stack.

思路

用两个队列模拟一个堆栈:

队列a和b

(1)取栈顶元素: 返回有元素的队列的首元素

(2)判栈空:若队列a和b均为空则栈空

(3)入栈:a队列当前有元素,b为空(倒过来也一样)则将需要入栈的元素先放b中,然后将a中的元素依次出列并入列倒b中。(保证有一个队列是空的)

(4)出栈:将有元素的队列出列即可。

比如先将1插入队a中 ,现在要将2入栈,则将2插入b总然后将a中的1出列入到b中,b中的元素变为 2 ,1

a为空,现在要压入3 则将3插入a中 ,依次将b中的2 ,1 出列并加入倒a中 ,a中的元素变为 3,2,1 b为空

总结:两个栈有一个为空,另一个有元素,或者两个都为空,只要哪个栈有元素,先把元素插入到这个队列。弹栈:将有元素的队列全部元素除去要弹出的元素,先从队列1删除,压入队列2,然后再将队列1最后一个元素删除。算法保证在任何时候都有一队列为空

struct QUEUE_NODE
{
int* pData;
int QueueLenMax;//队列最大长度
int head ;//队头指针
int tail;//队尾指针
int QueueCurLen;//队列元素当前个数
};

typedef struct {
struct QUEUE_NODE *pQueueNode_First;
struct QUEUE_NODE *pQueueNode_Second;
} Stack;

struct QUEUE_NODE* alloc_queue(int Queue_Max_Size)
{

if(Queue_Max_Size <= 0)
return NULL;

struct QUEUE_NODE* pQueueNode = NULL;

pQueueNode = (struct QUEUE_NODE*)malloc(sizeof(struct QUEUE_NODE));
if(NULL == pQueueNode) {
return NULL;
}
memset(pQueueNode, 0, sizeof(struct QUEUE_NODE));

pQueueNode->pData = (int*)malloc(sizeof(int) * Queue_Max_Size);
if(NULL == pQueueNode->pData) {
goto malloc_failed;
}

pQueueNode->QueueLenMax = Queue_Max_Size;//队列长度
pQueueNode->head = 0;
pQueueNode->tail = 0;
pQueueNode->QueueCurLen = 0;

return pQueueNode;

malloc_failed:
free(pQueueNode);
return NULL;
}

int free_queue(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return -1;

if(NULL == pQueueNode->pData) {
free(pQueueNode);
return -1;
}

free(pQueueNode->pData);
free(pQueueNode);

return 0;
}

int queue_push(struct QUEUE_NODE* pQueueNode, int value)
{
if(NULL == pQueueNode)
return -1;

if(pQueueNode->QueueLenMax == pQueueNode->QueueCurLen)//判断是不是溢出
return -1;

pQueueNode->pData[pQueueNode->tail] = value;
pQueueNode->tail = (pQueueNode->tail + 1) % pQueueNode->QueueLenMax; //队尾指针位置
pQueueNode->QueueCurLen ++;

return 0;
}

int queue_pop(struct QUEUE_NODE* pQueueNode, int* value)
{
if(NULL == pQueueNode || NULL == value)
return -1;

if(0 == pQueueNode->QueueCurLen)
return -1;

*value = pQueueNode->pData[pQueueNode->head];
pQueueNode->pData[pQueueNode->head] = 0; //被弹出的位置赋值为0

pQueueNode->QueueCurLen --;
pQueueNode->head = (pQueueNode->head + 1) % pQueueNode->QueueLenMax;//重新定位队头指针的位置

return 0;
}

int get_queue_curlen(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return -1;

return pQueueNode->QueueCurLen;
}

int get_queue_maxlen(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;

return pQueueNode->QueueLenMax;
}

void print_queue_node(struct QUEUE_NODE *pQueueNode)
{
/*1.输入的参数有误*/
if(NULL == pQueueNode) {
printf("[%d] pQueueNode is illegal! \n",__LINE__);
return;
}
/*2.输入的链式堆栈为空*/
if(0 == pQueueNode->QueueCurLen) {
printf("[%d] pQueueNode is empty!\n",__LINE__);
return ;
}

struct QUEUE_NODE *pQueueNodeTemp = pQueueNode;
int count = 0;
printf("The pQueueNode is: ");
while(count < pQueueNode->QueueCurLen) {
printf("%d ",pQueueNode->pData[pQueueNode->head+count]);
count++;;
}
printf("\n");
}

/* Create a stack */
void stackCreate(Stack *stack, int maxSize)
{
if(!stack) {
return;
}
if(maxSize<=0)
return;
/*创建两个队列*/
stack->pQueueNode_First = alloc_queue(maxSize);
stack->pQueueNode_Second= alloc_queue(maxSize);
}

/* Push element x onto stack */
void stackPush(Stack *stack, int element)
{
if(!stack)
return;
if(!stack->pQueueNode_First||!stack->pQueueNode_Second)
return;

/**
两个栈有一个为空,另一个有元素,或者两个都为空
只要哪个栈有元素,先把元素插入到这个队列
**/
if(stack->pQueueNode_First->QueueCurLen == 0 &&
stack->pQueueNode_Second->QueueCurLen == 0) {

queue_push(stack->pQueueNode_First,element);

return;
}
if(stack->pQueueNode_First->QueueCurLen != 0) {
queue_push(stack->pQueueNode_First,element);
}
if(stack->pQueueNode_Second->QueueCurLen != 0) {
queue_push(stack->pQueueNode_Second,element);
}
}

/* Removes the element on top of the stack */
void stackPop(Stack *stack)
{
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

int a = 0;

/*栈1为空*/
if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {

return;
}

/*栈1保存数据*/
if(stack->pQueueNode_First->QueueCurLen != 0) {
/*栈1剩下最后一个要删除数据*/
while(1 != stack->pQueueNode_First->QueueCurLen) {
queue_pop(stack->pQueueNode_First,&a);
queue_push(stack->pQueueNode_Second,a);
}
/*实现栈弹出数据*/
queue_pop(stack->pQueueNode_First,&a);

return; /*及时退出*/
}
/*栈2保存数据*/
if(stack->pQueueNode_Second->QueueCurLen != 0) {
while(1 != stack->pQueueNode_Second->QueueCurLen) {
queue_pop(stack->pQueueNode_Second,&a);
queue_push(stack->pQueueNode_First,a);
}
/*实现栈弹出数据*/
queue_pop(stack->pQueueNode_Second,&a);
}

}

/* Get the top element */
int stackTop(Stack *stack)
{
if(!stack)
return -1;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return -1;

int a = 0;
/*栈1为空*/
if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {

return -1;
}

/*栈1保存数据*/
if(stack->pQueueNode_First->QueueCurLen != 0) {
/*栈1剩下最后一个要删除数据*/
while(0 != stack->pQueueNode_First->QueueCurLen) {
queue_pop(stack->pQueueNode_First,&a);
queue_push(stack->pQueueNode_Second,a);
}

return a; /*及时退出*/
}
/*栈2保存数据*/
if(stack->pQueueNode_Second->QueueCurLen != 0) {
while(0 != stack->pQueueNode_Second->QueueCurLen) {
queue_pop(stack->pQueueNode_Second,&a);
queue_push(stack->pQueueNode_First,a);
}

}

return a;
}

/* Return whether the stack is empty */
bool stackEmpty(Stack *stack)
{
if(!stack)
return 1;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return 1;

if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {
return 1;
}

return 0;
}

/* Destroy the stack */
void stackDestroy(Stack *stack)
{
/*1.输入的参数有误*/
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

free_queue(stack->pQueueNode_First);
free_queue(stack->pQueueNode_Second);
}

/* print the stack */
void print_stack_node(Stack *stack)
{
/*1.输入的参数有误*/
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

print_queue_node(stack->pQueueNode_First);
print_queue_node(stack->pQueueNode_Second);

}


全部代码如下所示:

// LeetCode225-Implement Stack using Queues.cpp : 定义控制台应用程序的入口点。
//

//两个队列实现一个栈
//Written by ZP1015
//2015.10.21

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct QUEUE_NODE
{
int* pData;
int QueueLenMax;//队列最大长度
int head ;//队头指针
int tail;//队尾指针
int QueueCurLen;//队列元素当前个数
};

typedef struct {
struct QUEUE_NODE *pQueueNode_First;
struct QUEUE_NODE *pQueueNode_Second;
} Stack;

struct QUEUE_NODE* alloc_queue(int Queue_Max_Size)
{

if(Queue_Max_Size <= 0)
return NULL;

struct QUEUE_NODE* pQueueNode = NULL;

pQueueNode = (struct QUEUE_NODE*)malloc(sizeof(struct QUEUE_NODE));
if(NULL == pQueueNode) {
return NULL;
}
memset(pQueueNode, 0, sizeof(struct QUEUE_NODE));

pQueueNode->pData = (int*)malloc(sizeof(int) * Queue_Max_Size);
if(NULL == pQueueNode->pData) {
goto malloc_failed;
}

pQueueNode->QueueLenMax = Queue_Max_Size;//队列长度
pQueueNode->head = 0;
pQueueNode->tail = 0;
pQueueNode->QueueCurLen = 0;

return pQueueNode;

malloc_failed:
free(pQueueNode);
return NULL;
}

int free_queue(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return -1;

if(NULL == pQueueNode->pData) {
free(pQueueNode);
return -1;
}

free(pQueueNode->pData);
free(pQueueNode);

return 0;
}

int queue_push(struct QUEUE_NODE* pQueueNode, int value)
{
if(NULL == pQueueNode)
return -1;

if(pQueueNode->QueueLenMax == pQueueNode->QueueCurLen)//判断是不是溢出
return -1;

pQueueNode->pData[pQueueNode->tail] = value;
pQueueNode->tail = (pQueueNode->tail + 1) % pQueueNode->QueueLenMax; //队尾指针位置
pQueueNode->QueueCurLen ++;

return 0;
}

int queue_pop(struct QUEUE_NODE* pQueueNode, int* value)
{
if(NULL == pQueueNode || NULL == value)
return -1;

if(0 == pQueueNode->QueueCurLen)
return -1;

*value = pQueueNode->pData[pQueueNode->head];
pQueueNode->pData[pQueueNode->head] = 0; //被弹出的位置赋值为0

pQueueNode->QueueCurLen --;
pQueueNode->head = (pQueueNode->head + 1) % pQueueNode->QueueLenMax;//重新定位队头指针的位置

return 0;
}

int get_queue_curlen(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return -1;

return pQueueNode->QueueCurLen;
}

int get_queue_maxlen(struct QUEUE_NODE* pQueueNode)
{
if(NULL == pQueueNode)
return 0;

return pQueueNode->QueueLenMax;
}

void print_queue_node(struct QUEUE_NODE *pQueueNode)
{
/*1.输入的参数有误*/
if(NULL == pQueueNode) {
printf("[%d] pQueueNode is illegal! \n",__LINE__);
return;
}
/*2.输入的链式堆栈为空*/
if(0 == pQueueNode->QueueCurLen) {
printf("[%d] pQueueNode is empty!\n",__LINE__);
return ;
}

struct QUEUE_NODE *pQueueNodeTemp = pQueueNode;
int count = 0;
printf("The pQueueNode is: ");
while(count < pQueueNode->QueueCurLen) {
printf("%d ",pQueueNode->pData[pQueueNode->head+count]);
count++;;
}
printf("\n");
}

/* Create a stack */
void stackCreate(Stack *stack, int maxSize)
{
if(!stack) {
return;
}
if(maxSize<=0)
return;
/*创建两个队列*/
stack->pQueueNode_First = alloc_queue(maxSize);
stack->pQueueNode_Second= alloc_queue(maxSize);
}

/* Push element x onto stack */
void stackPush(Stack *stack, int element)
{
if(!stack)
return;
if(!stack->pQueueNode_First||!stack->pQueueNode_Second)
return;

/**
两个栈有一个为空,另一个有元素,或者两个都为空
只要哪个栈有元素,先把元素插入到这个队列
**/
if(stack->pQueueNode_First->QueueCurLen == 0 &&
stack->pQueueNode_Second->QueueCurLen == 0) {

queue_push(stack->pQueueNode_First,element);

return;
}
if(stack->pQueueNode_First->QueueCurLen != 0) {
queue_push(stack->pQueueNode_First,element);
}
if(stack->pQueueNode_Second->QueueCurLen != 0) {
queue_push(stack->pQueueNode_Second,element);
}
}

/* Removes the element on top of the stack */
void stackPop(Stack *stack)
{
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

int a = 0;

/*栈1为空*/
if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {

return;
}

/*栈1保存数据*/
if(stack->pQueueNode_First->QueueCurLen != 0) {
/*栈1剩下最后一个要删除数据*/
while(1 != stack->pQueueNode_First->QueueCurLen) {
queue_pop(stack->pQueueNode_First,&a);
queue_push(stack->pQueueNode_Second,a);
}
/*实现栈弹出数据*/
queue_pop(stack->pQueueNode_First,&a);

return; /*及时退出*/
}
/*栈2保存数据*/
if(stack->pQueueNode_Second->QueueCurLen != 0) {
while(1 != stack->pQueueNode_Second->QueueCurLen) {
queue_pop(stack->pQueueNode_Second,&a);
queue_push(stack->pQueueNode_First,a);
}
/*实现栈弹出数据*/
queue_pop(stack->pQueueNode_Second,&a);
}

}

/* Get the top element */
int stackTop(Stack *stack)
{
if(!stack)
return -1;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return -1;

int a = 0;
/*栈1为空*/
if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {

return -1;
}

/*栈1保存数据*/
if(stack->pQueueNode_First->QueueCurLen != 0) {
/*栈1剩下最后一个要删除数据*/
while(0 != stack->pQueueNode_First->QueueCurLen) {
queue_pop(stack->pQueueNode_First,&a);
queue_push(stack->pQueueNode_Second,a);
}

return a; /*及时退出*/
}
/*栈2保存数据*/
if(stack->pQueueNode_Second->QueueCurLen != 0) {
while(0 != stack->pQueueNode_Second->QueueCurLen) {
queue_pop(stack->pQueueNode_Second,&a);
queue_push(stack->pQueueNode_First,a);
}

}

return a;
}

/* Return whether the stack is empty */
bool stackEmpty(Stack *stack)
{
if(!stack)
return 1;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return 1;

if(stack->pQueueNode_First->QueueCurLen == 0
&& stack->pQueueNode_Second->QueueCurLen == 0) {
return 1;
}

return 0;
}

/* Destroy the stack */
void stackDestroy(Stack *stack)
{
/*1.输入的参数有误*/
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

free_queue(stack->pQueueNode_First);
free_queue(stack->pQueueNode_Second);
}

/* print the stack */
void print_stack_node(Stack *stack)
{
/*1.输入的参数有误*/
if(!stack)
return;
if(!stack->pQueueNode_First&&!stack->pQueueNode_Second)
return;

print_queue_node(stack->pQueueNode_First);
print_queue_node(stack->pQueueNode_Second);

}

int main()
{
Stack pStackNode;
stackCreate(&pStackNode,20);
print_stack_node(&pStackNode);
int i = 0;
for (i = 0;i<10;i++) {
stackPush(&pStackNode,i);/*入栈*/
}
print_stack_node(&pStackNode);
/*出栈*/
stackPop(&pStackNode);
print_stack_node(&pStackNode);
/*获得栈顶元素*/
printf("top %d\n",stackTop(&pStackNode));
printf("stackEmpty %d\n",stackEmpty(&pStackNode));
stackDestroy(&pStackNode);
getchar();
getchar();

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode 堆栈 队列