当前位置:网站首页>【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
2022-06-30 05:16:00 【XiYang-DING】
subject

Ideas
The characteristics of the two structures :① team : Tail insertion + Head deletion ② Stack : Tail insertion + Deletion at the end
The idea of tail insertion : If both teams are empty , Pick any team
Push; If one of the two teams is not empty , Then choose the team that is not emptyPush
The idea of tail deletion : Find a team that is not empty --> Before the n − 1 n-1 n−1 ElementsPushTo the air force --> Will be the first n n n ElementsPop
law : In fact, it is not difficult to see that there must be an empty team .
Code
typedef int DataType;
typedef struct QueueNode
{
DataType data;
struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
// initialization
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
// The destruction
void QueueDestroy(Queue* pq)
{
assert(pq);
// The first layer is deleted : Delete the node in the linked list
QueueNode* cur = pq->head;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
// The second layer is deleted : Put... In the structure head and tail empty
pq->head = pq->tail = NULL;
}
// The team
void QueuePush(Queue* pq, DataType x)
{
assert(pq);
// New node
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
newNode->data = x;
newNode->next = NULL;
// There are no nodes in the queue
if (pq->head == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;//tail Link with new node
pq->tail = newNode;// to update tail
}
}
// Out of the team
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head);// When the queue is empty
QueueNode* next = pq->head->next;// The next node of the tag header
free(pq->head);
pq->head = next;
// If the queue is empty ,head along with next empty , but tail Became a wild pointer
if (pq->head == NULL)
{
pq->tail = NULL;
}
}
// Take the team leader element
DataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
// Take the tail element
DataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->tail->data;
}
// Size of team
int QueueSize(Queue* pq)
{
assert(pq);
assert(pq->head);
QueueNode* cur = pq->head;
int n = 0;
while (cur)
{
++n;
cur = cur->next;
}
return n;
}
// Determines if the queue is empty
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* ms=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&ms->q1);
QueueInit(&ms->q2);
return ms;
}
void myStackPush(MyStack* obj, int x) {
if(QueueEmpty(&obj->q1))
{
QueuePush(&obj->q2,x);
}
else{
QueuePush(&obj->q1,x);
}
}
int myStackPop(MyStack* obj) {
Queue*emptyQ=&obj->q1;
Queue*notEmptyQ=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
emptyQ=&obj->q2;
notEmptyQ=&obj->q1;
}
while(QueueSize(notEmptyQ)>1)
{
QueuePush(emptyQ,QueueFront(notEmptyQ));
QueuePop(notEmptyQ);
}
int top=QueueFront(notEmptyQ);
QueuePop(notEmptyQ);
return top;
}
int myStackTop(MyStack* obj) {
Queue*notEmptyQ=&obj->q1;
if(QueueEmpty(&obj->q1))
{
notEmptyQ=&obj->q2;
}
return QueueBack(notEmptyQ);
}
bool myStackEmpty(MyStack* obj) {
// Both tables are empty
return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
// The first layer releases
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
// Second Cen release
free(obj);
}
/** * Your MyStack struct will be instantiated and called as such: * MyStack* obj = myStackCreate(); * myStackPush(obj, x); * int param_2 = myStackPop(obj); * int param_3 = myStackTop(obj); * bool param_4 = myStackEmpty(obj); * myStackFree(obj); */
边栏推荐
- Sourcetree usage
- LxC and LXD container summary
- Network communication problem locating steps
- Solution to the 292 week match of Li Kou
- Nestjs configures static resources, template engine, and post examples
- Unity3d Google Earth
- Unity mobile end sliding screen rotation
- GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>
- Intellj idea jars projects containing external lib to other project reference methods - jars
- Ripple effect of mouse click (unity & shader)
猜你喜欢

One command to run rancher
![[note] usage model tree of the unity resource tree structure virtualizingtreeview](/img/3e/fe5610c797a14554ad735172c3ab54.jpg)
[note] usage model tree of the unity resource tree structure virtualizingtreeview

Ripple effect of mouse click (unity & shader)

14x1.5cm竖向标签有点难,VFP调用BarTender来打印

Redis cluster concept

Unity Logitech steering wheel access

中文版PyCharm改为英文版PyCharm

LXC 和 LXD 容器总结

pycharm 数据库工具

LxC and LXD container summary
随机推荐
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Unity shader flat shadow
Sourcetree usage
[typescript] defines the return value type of promise
虚析构和纯虚析构
Unity limited time use limited trial time and use times
Unity3d packaging and publishing APK process
Unity 3D model operation and UI conflict Scrollview
Force buckle 704 Binary search
Win10 vs2015 compiling curaengine
[typescript] cannot redeclare block range variables
炒美原油的国际交易平台如何能保障资金安全呢?
Passing values between classes using delegates and events
Revit Secondary Development - - Project use Panel features not opened
Modbus protocol register
Unity Logitech steering wheel access
Unity gets the resolution of the game view
Solution to the 292 week match of Li Kou
【VCS+Verdi联合仿真】~ 以计数器为例
LXC 和 LXD 容器总结
