博客
关于我
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查询优化:LIMIT 1避免全表扫描
查看>>
MySQL查询优化之索引
查看>>
mysql查询储存过程,函数,触发过程
查看>>
mysql查询总成绩的前3名学生信息
查看>>
MySQL查询数据库所有表名及其注释
查看>>
mysql查询语句能否让一个字段不显示出来_天天写order by,你知道Mysql底层执行原理吗?
查看>>
MySQL死锁套路:一次诡异的批量插入死锁问题分析
查看>>
Mysql死锁问题Deadlock found when trying to get lock;try restarting transaction
查看>>
MySQL添加用户、删除用户与授权
查看>>
Mysql添加用户并授予只能查询权限
查看>>
mysql添加表注释、字段注释、查看与修改注释
查看>>
MySQL灵魂16问,你能撑到第几问?
查看>>
mysql状态分析之show global status
查看>>
mysql状态查看 QPS/TPS/缓存命中率查看
查看>>
mysql生成树形数据_mysql 实现树形的遍历
查看>>
mysql用于检索的关键字_Mysql全文搜索match...against的用法
查看>>
MySql用户以及权限的管理。
查看>>
MySQL用户权限配置:精细控制和远程访问的艺术!------文章最后有惊喜哦。
查看>>