博客
关于我
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 备份 Xtrabackup
查看>>
mysql 复杂查询_mysql中复杂查询
查看>>
mYSQL 外键约束
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>