当前位置:网站首页>[force buckle] realize stack with queue
[force buckle] realize stack with queue
2022-07-19 06:26:00 【Patrick star`】
225. Using queue to implement stack - Power button (LeetCode)
subject :
Please use only two queues to implement one LIFO (LIFO) The stack , And supports all four operations of ordinary stack (push、top、pop and empty).
Realization MyStack class :
void push(int x) Put the element x Press into the top of the stack .
int pop() Remove and return the top element of the stack .
int top() Back to top of stack element .
boolean empty() If the stack is empty , return true ; otherwise , return false .
Be careful :
You can only use the basic operations of the queue —— That is to say push to back、peek/pop from front、size and is empty These operations .
The language you use may not support queues . You can use list ( list ) perhaps deque( deque ) To simulate a queue , As long as it's a standard queue operation .
Ideas :
The queue is characterized by first in, first out , The characteristic of stack is first in first out
This question requires two queues
Code :
typedef int QueueDataType;
typedef struct QueueNode
{
QueueDataType data;
struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
while (cur)
{
QueueNode* temp = cur->next;
free(cur);
cur = temp;
}
pq->head = NULL;
pq->tail = NULL;
}
// The tail into the head
void QueuePush(Queue* pq, QueueDataType x)
{
assert(pq);
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
assert(newNode);
newNode->data = x;
newNode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;
pq->tail = newNode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
if (pq->head->next)
{
QueueNode* headNext = pq->head->next;
free(pq->head);
pq->head = headNext;
}
else
{
free(pq->head);
pq->head = NULL;
pq->tail = NULL;
}
}
size_t QueueSize(Queue* pq)
{
size_t size = 0;
QueueNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
bool QueueEmpty(Queue* pq)
{
return pq->head == NULL;
}
QueueDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
QueueDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->tail);
return pq->tail->data;
}
typedef struct
{
Queue Q1;
Queue Q2;
} MyStack;
MyStack* myStackCreate()
{
MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&stack->Q1);
QueueInit(&stack->Q2);
return stack;
}
void myStackPush(MyStack* obj, int x)
{
Queue* qNotNull = obj->Q1.head != NULL ? &obj->Q1 : &obj->Q2;// Queue that is not empty
QueuePush(qNotNull, x);
}
int myStackPop(MyStack* obj)
{
Queue* qNotNull ;
Queue* qNull;
if (obj->Q1.head)
{
qNotNull = &obj->Q1;
qNull = &obj->Q2;
}
else
{
qNotNull = &obj->Q2;
qNull = &obj->Q1;
}
QueueDataType temp = 0;
while (qNotNull->head != qNotNull->tail)
{
temp = qNotNull->head->data;
QueuePop(qNotNull);
QueuePush(qNull, temp);
}
temp = qNotNull->head->data;
QueuePop(qNotNull);
return temp;
}
int myStackTop(MyStack* obj)
{
Queue qNotNull;
Queue qNull;
if (obj->Q1.head)
{
qNotNull = obj->Q1;
qNull = obj->Q2;
}
else
{
qNotNull = obj->Q2;
qNull = obj->Q1;
}
QueueDataType temp = 0;
temp = qNotNull.tail->data;
return temp;
}
bool myStackEmpty(MyStack* obj)
{
if (obj->Q1.head == NULL && obj->Q2.head == NULL)
{
return true;
}
else
{
return false;
}
}
void myStackFree(MyStack* obj)
{
QueueDestroy(&obj->Q1);
QueueDestroy(&obj->Q2);
}
边栏推荐
- Leetcode string
- 【力扣】括号匹配
- 2022 robocom world robot developer competition - undergraduate group (provincial competition)
- TypeScript学习
- ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
- 【力扣】对称二叉树
- [bjoi2019] platoon formation (Group backpack)
- 【力扣】设计循环队列
- Longest bracket match (linear DP)
- MySQL workbench basically uses [create a data table]
猜你喜欢

Résoudre le problème de l'ambiguïté de la cible dans l'interaction de fixation 3D par l'estimation de la profondeur vor

Qt Creator闪退解决办法

【力扣】用栈实现队列

Cable TV network (tree grouping)

三维凝视估计,没有明确的个人校准2018

自动补全 & (自定义)拼音分词器 & 搜索时注意事项

2022/07/11 第五小组 丁帅 学习笔记 day04

【力扣】复制带随机指针的链表

Creation and implementation of WebService interface

2022/07/10 第五小组 丁帅 学习笔记 day03
随机推荐
Computational geometry (2)
【力扣】用栈实现队列
Antd is not defined
2022/07/12 学习笔记 (day05)JS内置函数
你的企业最适合哪种深度学习?
DSL实现自动补全查询
【牛客】二叉树遍历
TypeScript学习
Longest bracket match (linear DP)
量子三体问题: 数值计算概述
Single table query, add, update and delete data
uboot 编译前的配置命令make config分析
QT creator flashback solution
大龄程序员都去哪了?
Baby Ehab Partitions Again(dp,构造,位运算)
DSL实现Bucket聚合
RestAPI实现聚合(黑马教程)
WebService接口的创建与实现
【力扣】另一棵树的子树
开源在线的MarkDown编辑器 --【Editor.md】