当前位置:网站首页>[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);
}
边栏推荐
- Single table query, add, update and delete data
- 虚拟现实中的眼睛跟踪
- 【力扣】对称二叉树
- Guess the string (dichotomy, interaction)
- Volatile function of embedded C language
- MCU single chip OTP
- Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
- 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
- 大龄程序员都去哪了?
- DSL实现Metrics 聚合
猜你喜欢
![Chrome browser settings [display the translation language icon in the upper right corner]](/img/87/018e0ab92f698b77ada98ef555bba8.png)
Chrome browser settings [display the translation language icon in the upper right corner]

面试复习第N次

数学基础课2_欧拉函数,线性筛,扩欧
![[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode](/img/d4/31272772b96d3e2f702da74478fd95.png)
[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode

【力扣】环形链表 II

Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;

Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe

Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位

用头部运动学习无姿态注视偏差

Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
随机推荐
2021-11-10 micropyton tb6600 step drive class
【牛客】二叉树遍历
Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
Peerless good problem (bit operation optimization DP)
MySQL workbench basically uses [create a data table]
QT creator flashback solution
Key points of embedded C language (const, static, volatile, bit operation)
Qtss callback routine
三维凝视估计,没有明确的个人校准2018
Acwing game 58 (AK)
RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
Qt Creator闪退解决办法
Interview review nth time
自下而上和自上而下的注意力:不同的过程和重叠的神经系统 2014sci
你的企业最适合哪种深度学习?
你见过的最差的程序员是怎样的?
Introduction to basic knowledge of Minio
XOR gun (bit operation, thinking, interval violence)
2022/07/12 学习笔记 (day05)JS内置函数
Acwing game 59 (AK)