博客
关于我
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进阶索引篇03——2个新特性,11+7条设计原则教你创建索引
查看>>
mysql远程连接设置
查看>>
MySql连接出现1251Client does not support authentication protocol requested by server解决方法
查看>>
Mysql连接时报时区错误
查看>>
MySql连接时提示:unknown Mysql server host
查看>>
MySQL连环炮,你扛得住嘛?
查看>>
mysql逗号分隔的字符串如何搜索
查看>>
MySQL通用优化手册
查看>>
Mysql通过data文件恢复
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
mysql重新安装?忘记root密码?重装Windows、Linux系统导致mysql没法用吗? 这里有你想要的答案
查看>>