当前位置:网站首页>The mutual realization of C L stack and queue in I
The mutual realization of C L stack and queue in I
2022-07-05 07:23:00 【MT_ one hundred and twenty-five】
Catalog
One 、 Stack with pairs of columns
Two 、 Using stack to realize queue
One 、 Stack with pairs of columns
Question stem requirements :
Detail analysis : The queue is first in, first out ; The stack to be implemented is first in and last out .
Their thinking : hypothesis : First use a queue to store data N individual , And then N-1 Data is imported into another queue ,
here , There is only one left in the original queue , Is the last remaining data , You can export it , This is a last in, first out .
Minutiae : Every time you export data , You need one queue to transfer data to another queue , So input queues and output queues Rotation required , It should be judged .
The specific process gif The dynamic diagram is as follows :
Code implementation :
1. Initialization stack : First initialize two queues
// The stack structure is composed of two queues
typedef struct Nystack{
Quetail q1;
Quetail q2;
} MyStack;
// Initialization of stack
MyStack* myStackCreate() {
MyStack* Newstack = (MyStack*)malloc(sizeof(MyStack));
Que_Init(&Newstack->q1);
Que_Init(&Newstack->q2);
return Newstack;
}
2. insert data
Because the queue for storing data is not fixed , Therefore, on the premise that a queue has data , Continue to insert data into the queue ,
The empty queue is responsible for rotation when exporting data .( Which is not empty, which is inserted )
// insert data
void myStackPush(MyStack* obj, int x) {
//if((&obj->q1)->head == NULL) // Law 1 : Directly judge whether it is empty
if(Que_Empty(&obj->q1)) // Law two : Reuse of subsequent functions
Que_push(&obj->q2,x);
else
Que_push(&obj->q1,x);
}
3. Derived data ( Achieve first in and last out )
First step : Divide the last data in the queue with data , Import to another empty queue in turn ;
Import empty queue , Delete the original queue , Keep the last data .
Second cloth : Export the last data in the original queue .
notes : Here we first assume two queues , One is the original queue and the other is the empty queue , And then judge , If it is inconsistent with the actual , be In exchange for .
int myStackPop(MyStack* obj) {
int temp = 0;
// Suppose the original queue and the empty queue
Quetail* existque = &obj->q1,*nullque = &obj->q2;
if((&obj->q1)->head == NULL) // Judge whether it is consistent with the actual
{
existque = nullque;
nullque = &obj->q1;
}
for(;existque->head->Next;) // Keep the last data
{
Que_push(nullque,existque->head->data); // Import data to an empty queue
Que_pop(existque); // Delete the original queue data
}
temp = existque->head->data;
Que_pop(existque); // Export the last entered data
return temp;
}
4. Find the data at the top of the stack
Find a queue that is not empty >> Return the data at the end of the queue
int myStackTop(MyStack* obj) {
if((&obj->q1)->head == NULL)
{
return (&obj->q2)->tail->data;
}
return (&obj->q1)->tail->data;
}
5. Judge whether the stack is empty :
Judge whether both queues are empty
bool myStackEmpty(MyStack* obj) {
assert(obj);
// Law 1 : Direct judgment
//if((&obj->q1)->head == NULL&& (&obj->q2)->head == NULL)
// Law two : Reuse queue empty function
if(Que_Empty(&(obj->q1))&&Que_Empty(&(obj->q2)))
return true;
return false;
}
6. Destroy the stack :
Destroy two queues
void myStackFree(MyStack* obj) {
Que_Destory(&obj->q1);
Que_Destory(&obj->q2);
free(obj);
}
Two 、 Using stack to realize queue
Question stem requirements :
Detail analysis : This time we use two stacks , Achieve first in first out .
Their thinking : First , Divide the two stacks into entry stack and exit stack ,
secondly , Data positive order entry stack , Because the stack is first in and last out , Therefore, enter the data into the exit stack in reverse order ,
then , At this time, the data is in reverse order in the exit stack , therefore , Then it can be discharged from the exit stack in positive order .
Code implementation :
1. Initialize the dual stack queue
typedef struct {
Stack T1;
Stack T2;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue *Q1;
Q1 = (MyQueue*)malloc(sizeof(MyQueue));
Stack_init(&(Q1->T1)); // T1 Do the entry stack
Stack_init(&(Q1->T2)); // T2 Make a mouth stack
return Q1;
}
2. insert data
void myQueuePush(MyQueue* obj, int x) {
Stack_push(&obj->T1,x); // Here will stack T1 As an entry stack
}
3. Delete data ( fifo )
Record the entry stack data >> Delete entry stack data >> Import exit stack >> Export data from exit stack
int myQueuePop(MyQueue* obj) {
if(Stack_Empty(&obj->T2)) // Determine whether it is null
{
int k = 0;
for(;!Stack_Empty(&obj->T1);)
{
k = Stack_Top(&obj->T1); // Record entry station data
Stack_pop(&obj->T1); // Delete entry stack data
Stack_push(&obj->T2,k); // Import exit stack
}
}
int temp = 0;
temp = Stack_Top(&obj->T2);
Stack_pop(&obj->T2); // Export data from exit stack
return temp; // The stem of the question requires that the exported value be returned
}
4. Find queue header data
The header data here is the header data in positive order , Therefore, first import the reverse order data in the entry stack into the exit stack ,
Become positive , Then return the top data of the exit stack .
int myQueuePeek(MyQueue* obj) {
if(Stack_Empty(&obj->T2)) // Judge whether there is data in the exit stack
{
int k = 0;
for(;!Stack_Empty(&obj->T1);) // Import data to the exit stack
{
k = Stack_Top(&obj->T1);
Stack_pop(&obj->T1);
Stack_push(&obj->T2,k);
}
}
return Stack_Top(&obj->T2); // Return exit stack top data
}
5. Determines if the queue is empty And Destroy queue
// Determines if the queue is empty
bool myQueueEmpty(MyQueue* obj) {
// Determine whether both stacks are empty
return Stack_Empty(&obj->T1)&&Stack_Empty(&obj->T2);
}
// Destroy the release queue
void myQueueFree(MyQueue* obj) {
Stack_pop(&obj->T1);
Stack_pop(&obj->T2);
free(obj);
}
Thank you for your support !!!
边栏推荐
- SD_ CMD_ SEND_ SHIFT_ REGISTER
- ImportError: No module named ‘Tkinter‘
- Word import literature -mendeley
- Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
- [vscode] search using regular expressions
- Tshydro tool
- [tf1] save and load parameters
- 2022.06.27_每日一题
- 【Node】npm、yarn、pnpm 区别
- 1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
猜你喜欢
[untitled]
Machine learning Seaborn visualization
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
DelayQueue延迟队列的使用和场景
PHY drive commissioning - phy controller drive (II)
Shadowless cloud desktop - online computer
Using GEE plug-in in QGIS
2022年PMP项目管理考试敏捷知识点(7)
U-Boot初始化及工作流程分析
剑指 Offer 56 数组中数字出现的次数(异或)
随机推荐
目标检测系列——Faster R-CNN原理详解
[software testing] 06 -- basic process of software testing
苏打粉是什么?
The SQL implementation has multiple records with the same ID, and the latest one is taken
Rough notes of C language (2) -- constants
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
SD_ CMD_ SEND_ SHIFT_ REGISTER
UNIX commands often used in work
Binary search (half search)
Matlab在线性代数中的应用(四):相似矩阵及二次型
C#学习笔记
Unity ugui how to match and transform coordinates between different UI panels or uis
Basic operation of external interrupt (keil5)
611. 有效三角形的个数
DataGrid offline installation of database driver
并查集理论讲解和代码实现
list. files: List the Files in a Directory/Folder
[software testing] 02 -- software defect management
Docker installs MySQL and uses Navicat to connect
Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no