leetcode 232. 用栈实现队列
  TEZNKK3IfmPf 2023年11月12日 35 0

题目

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头的元素 boolean empty() 如果队列为空,返回 true ;否则,返回 false 说明: 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。   示例 1: 输入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 1, 1, false] 解释: MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false

代码

typedef int datatype;
typedef struct stack
{
	datatype* a;
	int top;
	int capacity;
}ST;
void stackinit(ST* p);
void stackpush(ST* p,datatype x);
 datatype stacktop(ST* p);
void stackpop(ST* p);
int stacksize(ST* p);
bool stackempty(ST* p);
void stackdestroy(ST* p);
void stackinit(ST* p)//栈的初始化
{
	assert(p);
	p->a = NULL;
	p->top = 0;
	p->capacity = 0;
}
void stackpush(ST* p, datatype x)//入栈
{
	assert(p);
	if (p->top == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		datatype* tmp = (datatype*)realloc(p->a, sizeof(datatype)*newcapacity);
		if (tmp != NULL)
		{
			p->a = tmp;
			p->capacity = newcapacity;
		}
	}
	p->a[p->top] = x;
	p->top++;
}
void stackpop(ST* p)//移除栈顶元素
{
	assert(p);
	assert(p->top > 0);
	p->top--;
}
datatype  stacktop(ST* p)//出栈
{
	assert(p);
	assert(p->top>0);
	return p->a[p->top - 1];
}
bool  stackempty(ST* p)//是否为空
{
	return p->top == 0;
}
int stacksize(ST* p)//栈中元素个数
{
	assert(p);
	return p->top;
}
void stackdestroy(ST* p)//内存销毁
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->top = 0;
	p->capacity = 0;
}
typedef struct {
   ST popst;
   ST pushst;
} MyQueue;


MyQueue* myQueueCreate() {
   MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));
   stackinit(&obj->popst);
   stackinit(&obj->pushst);
   return obj;
}

void myQueuePush(MyQueue* obj, int x) {
      stackpush(&obj->pushst,x);
}
int myQueuePeek(MyQueue* obj);
int myQueuePop(MyQueue* obj) {
     int peek=myQueuePeek(obj);
     stackpop(&obj->popst);
     return peek;
}
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);
}

bool myQueueEmpty(MyQueue* obj) {
  return stackempty(&obj->pushst)&&stackempty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
  stackdestroy(&obj->pushst);
  stackdestroy(&obj->popst);
  free(obj);
}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/

#过程

入队列

leetcode 232. 用栈实现队列

直接将数据入到 pushst中

peek返回队列开头的元素

分为两种情况:

1.若popst无数据

leetcode 232. 用栈实现队列

leetcode 232. 用栈实现队列

将pushst的所有数据传入popst中

2.若popst中有数据

leetcode 232. 用栈实现队列

直接返回popst的栈顶元素即对头数据

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月12日   35   0   0 算法leetcodeC++
  TEZNKK3IfmPf   2024年04月19日   51   0   0 leetcode位运算
TEZNKK3IfmPf