博客
关于我
leetcode-用栈实现队列-27
阅读量:278 次
发布时间:2019-03-01

本文共 2832 字,大约阅读时间需要 9 分钟。

题目要求

  请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
  void push(int x) 将元素 x 推到队列的末尾
  int pop() 从队列的开头移除并返回元素
  int peek() 返回队列开头的元素
  boolean empty() 如果队列为空,返回 true ;否则,返回 false。
思路
  因为本题是按照c语言来写的,因此需要自己写对于栈的相关功能的函数进行调用,由于栈是先进后出的,我们可以先构建上两个栈,第一个栈用来入栈,一次将所有的数据全部入栈之后,然后我们只要把第一个栈中的数据全部导入到第二个栈中,这样我们就可以把全部的数据逆序,然后只需要每次从第二个栈一个一个出栈,当第二个栈中的数据为空时,再重新引入第一个栈中的数据,如果两个栈中均没有数据,那么说明我们所建的队列为空。
代码实现

typedef int SLDataType;typedef struct SeqList{	SLDataType* a;	int top;	int capacity;}Stack;//栈的初始化void StackInit(Stack* pst);//栈的销毁void StackDestory(Stack* pst);//增加栈顶元素void StackPush(Stack* pst, SLDataType x);//移除栈顶元素void StackPop(Stack* pst);//返回栈中元素的数目int StackSize(Stack* pst);//返回栈顶元素SLDataType StackTop(Stack* pst);//栈内判空int StackEmpty(Stack* pst);//栈的初始化void StackInit(Stack* pst){	assert(pst);	pst->a = (SLDataType*)malloc(sizeof(SLDataType)* 4);	pst->top = 0;	pst->capacity = 4;}//栈的销毁void StackDestory(Stack* pst){	assert(pst);	free(pst->a);	pst->a = NULL;	pst->top = pst->capacity = 0;}//增加栈顶元素void StackPush(Stack* pst, SLDataType x){	assert(pst);	if (pst->top == pst->capacity)	{		SLDataType* tmp = realloc(pst->a, pst->capacity * 2 * sizeof(SLDataType));		if (tmp == NULL)		{			printf("relloc fail\n");			exit(-1);		}		pst->a = tmp;		pst->capacity *= 2;	}	pst->a[pst->top] = x;	pst->top++;}//移除栈顶元素void StackPop(Stack* pst){	assert(pst);	assert(!StackEmpty(pst));	pst->top--;}//返回栈中元素的数目int StackSize(Stack* pst){	assert(pst);	return pst->top;}//返回栈顶元素SLDataType StackTop(Stack* pst){	assert(pst);	assert(!StackEmpty(pst));	return pst->a[pst->top - 1];}//栈内判空int StackEmpty(Stack* pst){	assert(pst);	return pst->top == 0 ? 1 : 0;}typedef struct {	Stack popST;	Stack pushST;} MyQueue;/** Initialize your data structure here. */MyQueue* myQueueCreate() {	MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));	StackInit(&q->pushST);	StackInit(&q->popST);	return q;}/** Push element x to the back of queue. */void myQueuePush(MyQueue* obj, int x) {	StackPush(&obj->pushST, x);}/** Removes the element from in front of queue and returns that element. */int myQueuePop(MyQueue* obj) {	//1.如果为空	//2.如果不为空	if (StackEmpty(&obj->popST))	{		while (!StackEmpty(&obj->pushST))		{			StackPush(&obj->popST, StackTop(&obj->pushST));			StackPop(&obj->pushST);		}	}	int top = StackTop(&obj->popST);	StackPop(&obj->popST);	return top;}/** Get the front element. */int myQueuePeek(MyQueue* obj) {	if (StackEmpty(&obj->popST))	{		while (!StackEmpty(&obj->pushST))		{			StackPush(&obj->popST, StackTop(&obj->pushST));			StackPop(&obj->pushST);		}	}	return StackTop(&obj->popST);}/** Returns whether the queue is empty. */bool myQueueEmpty(MyQueue* obj) {	return StackEmpty(&obj->pushST) && StackEmpty(&obj->popST);}void myQueueFree(MyQueue* obj) {	StackDestory(&obj->pushST);	StackDestory(&obj->popST);	free(obj);	//置空也不会把实参置空,无意义}

转载地址:http://djno.baihongyu.com/

你可能感兴趣的文章
mysql性能测试工具选择 mysql软件测试
查看>>
MySQL慢查询-开启慢查询
查看>>
MySQL慢查询问题排查
查看>>
mysql截取sql语句
查看>>
mysql手工注入
查看>>
Mysql执行update by id的过程
查看>>
mysql执行计划
查看>>
MySQL执行计划 EXPLAIN参数
查看>>
MySQL执行计划【explain】,看这一篇就够啦!
查看>>
Mysql执行计划字段解释
查看>>
mysql执行计划怎么看
查看>>
MySQL执行计划解读
查看>>
mysql执行顺序与索引算法
查看>>
mysql技能梳理
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql支持表情
查看>>
MySQL改动rootpassword的多种方法
查看>>
mysql数据分组索引_MYSQL之索引配置方法分类
查看>>
mysql数据取差,mysql屏蔽主外键关联关系
查看>>