当前位置:网站首页>Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue
Sword finger offer:[day 1 stack and queue (simple)] --- > use two stacks to realize the queue
2022-06-28 21:46:00 【Love you, little pig】
List of articles
One 、 Title Description
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead, The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example 1:
Input :
["CQueue","appendTail","deleteHead","deleteHead"][[],[3],[],[]]
Output :[null,null,3,-1]
Example 2:
Input :
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"][[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
Tips :
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
Two 、 Thought analysis
notes : Some contents and pictures in the analysis of ideas are for reference. I'll make a self-help deduction for you, seniors , Thank them for their selfless dedication
Topic interpretation
["CQueue","appendTail","deleteHead","deleteHead"]
This line represents the operation of each line of code
[[],[3],[],[]]
This represents the parameters corresponding to each line of code operation .[] Represents that there are no parameters ,[3] The parameters representing this operation are 3
give an example :
CQueue Means to create a new CQueue object , The corresponding required parameters are [], That is, this operation does not require parameters .
appendTail It means to execute a appendTail() operation , The corresponding element to be operated is 3.
deleteHead It means to execute a deleteHead operation , The corresponding required parameters are [], That is, this operation does not require parameters .
deleteHead It means to execute a deleteHead operation , The corresponding required parameters are [], That is, this operation does not require parameters .
Thought analysis
Queue up operation of stack and queue 、 The out of queue operation is shown in the following figure :
It can be seen that :
① The queue operation is basically the same as the stack operation , Are inserted backward in turn .
② When the team , Pop up the team head element . And we only have stacks here , The element at the head of the queue corresponds to the element at the bottom of the stack , We can't directly pop up the elements at the bottom of the stack .
Solution : You can pop up all the elements of the stack in turn , Put it on another stack . Suppose you have a stack of three elementsA=[1,2,3]Empty stackB=[]. If theAAnd add the element to the stackB, Until the stackAIt's empty , beA=[],B=[3,2,1]. It can be seen that , The stackBThe top element of the stack is the stackAAt the bottom of the stack , The first element of the team . meanwhile , To continue popping the first element , You can continue to pop up directlyBTop element of .
3、 ... and 、 The overall code
The overall code is as follows
#define MAX 10000
typedef struct {
int *Stack1; // Stack 1
int *Stack2; // Stack 2
int top1; // Pointing stack 1 Top element of
int top2; // Pointing stack 2 Top element of
} CQueue;
CQueue* cQueueCreate() {
CQueue* C = (CQueue*)malloc(sizeof(CQueue)); // Create a queue
C->Stack1 = (int*)malloc(sizeof(int)*MAX); // Create a stack 1
C->top1 = -1; // Stack 1 There are no elements at this time ,top1=-1
C->Stack2 = (int*)malloc(sizeof(int)*MAX); // Create a stack 2
C->top2 = -1; // Stack 2 There are no elements at this time ,top2=-1
return C;
}
void cQueueAppendTail(CQueue* obj, int value) {
//top1+1, And will vlue Assign a value to Stack1[top+1]
obj->Stack1[++obj->top1] = value;
}
int cQueueDeleteHead(CQueue* obj) {
if(obj->top2 != -1){
//Stack2 Not empty , Go straight back to Stack2 Top element of , And let top2-1
return obj->Stack2[obj->top2--];
}
else{
while(obj->top1 != -1){
//Stack2 It's empty ,Stack1 Not empty , At this time will be Stack1 The elements of are pressed to Stack2 in
obj->Stack2[++obj->top2] = obj->Stack1[obj->top1--];
}
// take Stack1 Element pressed to Stack2 At the end of , here Stack1 It's empty
// Check Stack2 Is it empty , If it is empty, there are no elements in the queue . If it is not empty, return Stack2 Top element of
return obj->top2==-1? -1 : obj->Stack2[obj->top2--];
}
}
void cQueueFree(CQueue* obj) {
free(obj->Stack1);
free(obj->Stack2);
free(obj);
}
/** * Your CQueue struct will be instantiated and called as such: * CQueue* obj = cQueueCreate(); * cQueueAppendTail(obj, value); * int param_2 = cQueueDeleteHead(obj); * cQueueFree(obj); */
function , Verification passed 
边栏推荐
- Understanding web automated testing
- Alist+raidrive gives the computer a complete 8billion GB hard disk drive
- postman简介与安装步骤
- Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid
- Pie (poj3122) super detailed and easy to understand binary introduction
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- Go cryptobin common encryption and decryption Libraries
- Is it safe to open an account for stocks on mobile phones in 2022? Who can I ask?
- 16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
- Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
猜你喜欢

CORBA Architecture Guide (Common Object Request Broker Architecture)

The further application of Li Kou tree

Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival

视频号如何下载视频?来看超简单方法!

零基础自学SQL课程 | SQL中的日期函数大全

How do independent site sellers efficiently manage complex Facebook pages?

Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol

16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants

Lumiprobe protein labeling research scheme

QJsonObject的使用示例
随机推荐
零基础自学SQL课程 | SQL中的日期函数大全
什么是接口?什么是接口测试?
User network model and QoE
Lumiprobe proteorange protein gel dye instructions
【激活函数】
QT how the coordinates of one control are relatively fixed and displayed on another control (coordinate system)
河狸生存记:90后女博士与AI开发者们
Is the VIP securities account of qiniu school really safe and regular? How do I say this?
为什么要使用 Rust 语言?
GlobalSign的泛域名SSL证书
LeetCode117. Populate the next right node pointer for each node_ II
LeetCode213. 打家劫舍II
Web自动化工具选择
How can the sports app keep the end-to-side background alive to make the sports record more complete?
go-cryptobin 常用加密解密库
16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
Lumiprobe non fluorescent alkyne research - dbco NHS ester
认识Web自动化测试
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
Is it safe to open an account on great wisdom
