当前位置:网站首页>[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);
}
边栏推荐
- ACWing每日一题.3511
- 【力扣】用队列实现栈
- 带透明png转换成c数组
- Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe
- 大龄程序员都去哪了?
- Baby Ehab Partitions Again(dp,构造,位运算)
- LeetCode树
- DSL实现Metrics 聚合
- 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
- MySQL workbench basically uses [create a database]
猜你喜欢

结合图片看常用串口通信UART

Talking about several solutions of cross domain

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

Bottom up and top-down attention: different processes and overlapping nervous systems 2014sci

Introduction to basic knowledge of Minio

【力扣】用栈实现队列

Interview review nth time

2021-09-15

RestAPI实现聚合(黑马教程)

【力扣】用队列实现栈
随机推荐
Markdown语法和常用快捷键
虚拟现实中的眼睛跟踪
Antd is not defined
【力扣】括号匹配
Chrome browser settings [display the translation language icon in the upper right corner]
2022/07/14 learning notes (day07) array
[bjoi2019] platoon formation (Group backpack)
用头部运动学习无姿态注视偏差
mapping索引属性 & 创建索的操作
Acwing daily question three thousand five hundred and eleven
Volatile function of embedded C language
c语言 指定日期开始多少天 显示
Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization
2021-09-15
2022/07/09 第五小组 丁帅 学习笔记 day02
Make menuconfig missing ncurses
本地makefile 编译其他文件夹文件 指定obj目录
LeetCode字符串
[transfer] Darwin streaming server core code analysis
MySQL workbench basically uses [create a database]