当前位置:网站首页>Class implementation of linear stack and linear queue (another binary tree pointer version)
Class implementation of linear stack and linear queue (another binary tree pointer version)
2022-07-07 22:49:00 【Qingshan's green shirt】
Class implementation of stack and queue ( Instead of STL)
List of articles
distinguish
1. expression :count++—— operation : to count+1—— Value of expression :count The value of the original ( Not added 1 Previous value )
2. expression :++count —— operation : to count+1—— Value of expression :count Add 1 Later value
1. Stack
Linear stack
// Linear stack
class stack {
public:
//1. Pressing stack
void Push(int m) {
data[top++] = m;// Assign values first and then ++
}
//2. Pop up top element
int Pop() {
return data[--top];// First -- To assign a value
// If you don't need to return direct top-1 Return type int Change it to void
}
//3. Initialization of stack
void InitStack() {
top = 0;
}
//4. Sentenced to empty
bool Empty() {
if (top == 0) return true;
return false;
}
//5. Take the top element of the stack but don't delete
int Top(){
return data[top-1];//!!! Note that there !!!
}
private:
int top;// Top pointer of stack ( Point to the element to be put , This can be changed by yourself )
int data[1000];// The static array is put on the stack
};
Stack for storing binary tree pointers
// Put the linear stack of binary tree nodes
class stack{
public:
//1. initialization
void InitStack(){
top = 0;
}
//2. Binary tree pointer stack
void Push(TreeNode *p){
stack[top++] = p;
}
//3. Bomb stack
void Pop(){
top--;
}
//4. Sentenced to empty
bool Empty() {
if (top == 0)return true;
return false;
}
//5. Take the top element of the stack
TreeNode* Top( ){
return stack[top-1];
}
private:
int top;
TreeNode *stack[512];
};
2. queue
The pointer settings of the head and tail of the queue in the code below
front— Point to the team header element
rear— Point to the last position of the tail element ( The next place to insert )
Linear queue
// Linear queue
class Queue {
public:
//1. Initialization function
void InitQueue( ) {
rear = front = 0;
}
//2. Determines if the queue is empty ( Not too serious )
bool QueueEmpty() {
if (rear == front) return true; // If it's empty, return to true
else return false;
}
//3. The team
bool EnQueue(int e) {
if (rear == MAXSIZE) return false;// This sentence must have a meaning ! Or the array is out of bounds !( Array range MAXSIZE-1)
data[rear++] = e;
return true;
}
//4. Out of the team Direct output version
int DeQueue() {
// Out of the team is the top of the team
if (rear == front) return false;
int x = data[front++];
return x;
}
//4.5 Out of the team Only out of the team without output
void PopQueue(){
front = front + 1;// Note that there !
}
//5. Take the team leader element ( Return to the first element )
TreeNode* Front(){
return data[front];
}
private:
int front, rear;
int data[MAXSIZE];
};
A queue for storing binary tree pointers
// Put the queue of binary tree nodes
class Queue {
public:
//1. Initialization function
void InitQueue( ) {
rear = front = 0;
}
//2. Determines if the queue is empty
bool Empty() {
if (rear == front) return true; // If it's empty, return to true Not absolutely !
else return false;
}
//3. The team
bool Push(TreeNode* e) {
if (rear == MAXSIZE) return false;// There has to be Otherwise, the array will be out of bounds !
data[rear++] = e;
return true;
}
//4. Delete team leader element
void Pop(){
front = front + 1;// Note that there ! rear front Are increasing backwards
}
//5. Return to the first element
TreeNode* Front(){
// An array ?
return data[front];
}
private:
int front, rear;// Pointer to a party — Point to the last position of the tail element Team head pointer — Point to the team header element
TreeNode* data[MAXSIZE];// It stores pointers instead of nodes ! Don't save the whole node
};
边栏推荐
- Unity FAQ (I) lack of references
- Revit secondary development - get the project file path
- Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
- 行测-图形推理-5-一笔画类
- How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
- Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
- 行测-图形推理-9-线条问题类
- Firefox browser installation impression notes clipping
- Px4 autonomous flight
猜你喜欢

Robot autonomous exploration series papers environment code

CTF练习

OpenGL configuration vs2019

应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设

php 获取图片信息的方法
Apple further entered the financial sector through the 'virtual card' security function in IOS 16

Leetcode1984. Minimum difference in student scores

行测-图形推理-9-线条问题类
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
![[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
随机推荐
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Revit secondary development - get the project file path
Antd date component appears in English
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Xcode modifies the default background image of launchscreen and still displays the original image
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
Leetcode206. Reverse linked list
Debezium系列之:mysql墓碑事件
How to judge whether the input content is "number"
Robot autonomous exploration DSVP: code parsing
Debezium series: support the use of variables in the Kill Command
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
Revit secondary development - intercept project error / warning pop-up
Remove the default background color of chrome input input box
Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
Robot autonomous exploration series papers environment code
Debezium系列之:引入对 LATERAL 运算符的支持