当前位置:网站首页>Adding, deleting, checking and modifying stack - dynamic memory
Adding, deleting, checking and modifying stack - dynamic memory
2022-06-29 01:35:00 【qzt__ l0ve】
Stack access is “ First in first out or last in first out ”.
Catalog
One 、 The header file 、 structure 、 function
3、 ... and 、 Running results :
One 、 The header file 、 structure 、 function
The header files that need to be used are :
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>Type of structure :
typedef int STDataType;// It is convenient to change the structure type (int char double...)
typedef struct stack
{
STDataType *a;// Integer data
int top;// Identify the position of the top of the stack
int capacity;// Capacity
}ST;The main functions used :
void StackInit(ST* ps);// initialization
void StackDestory(ST* ps);// The destruction
void StackPush(ST* ps,STDataType x);// insert data
void StackPop(ST* ps);// Delete
STDataType StackTop(ST* ps);// Stack top ID
bool StackEmpty(ST* ps);// Judge the space capacity
int STackSize(ST* ps);// size Two 、 Function analysis :
(1) initialization
First, the initial data is 0.
void StackInit(ST* ps)
{
assert(ps);// Assertion , Judge ps Is it empty
ps->a = NULL;// initialization
ps->top = 0;// initialization
ps->capacity = 0;// initialization
}(2) The destruction
We must remember to destroy the dynamic space .
void StackDestory(ST* ps)
{
free(ps->a);// Release space
ps->a = NULL;// The pointer becomes null
ps->top = ps->capacity = 0;// Zeroing
}(3) Insert
Insert data into it : Because the stack is in the form of last in first out , Therefore, you can only insert them in sequence .
void StackPush(ST* ps, STDataType x)// insert data
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;// If the capacity is zero, it will be opened up 4 Space , Each time 4 Multiple development
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);// Open up space size
if (tmp == NULL)// Development failure reminder
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;// Inserted data
ps->top++;// Every time you insert a data top+1
}(4) Delete
Because the stack is in the form of last in first out , Therefore, you can only delete from the last data , So direct top-- That's it
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}(5) Stack top ID
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];// Return data subscript position
}(6) Judge the space capacity
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;//top=0 by false, It's not equal to 0 That is to say true
}(7) The size of the stack
Used to count the amount of data in the stack
int STackSize(ST* ps)
{
assert(ps);
return ps->top;// The total number of data in the stack
}(8) Operation of stack
Add and change the stack through the following code .
void TestStack()
{
ST st;
StackInit(&st);// initialization
// Insert
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));// Print the last entered data
StackPop(&st);// Delete the last data
printf("%d ", StackTop(&st));// Print the last data after deletion
StackPop(&st);// Then delete the last data
// Insert
StackPush(&st, 4);
StackPush(&st, 5);
StackPush(&st, 6);
printf("\n");
// Print the rest of the stack
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
StackDestory(&st);
}
int main()
{
TestStack();// call
return 0;
}3、 ... and 、 Running results :

边栏推荐
- The function of Schottky diode in preventing reverse connection of power supply
- Typescript (5) class, inheritance, polymorphism
- 测试只能干到35岁?35岁+的测试就会失业?
- Installing Oracle database in docker
- Rasa dialogue robot helpdesk (V)
- 我想今天买股票,可以么?现在网上开户安全么?
- 【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell
- AHA C language, C language programming introductory books and PPT (PDF version) download website
- Do280 allocating persistent storage
- Callback function of unity after importing resources
猜你喜欢

TypeScript(5)类、继承、多态

TypeScript(4)接口

Test a CSDN free download software

Design and development of VB mine sweeping game

测试只能干到35岁?35岁+的测试就会失业?

ASP. Design and implementation of net+sql online alumni list

Pytorch -- use and modification of existing network model
![[RRT 3D path planning] rapid expansion of random tree UAV 3D path planning based on MATLAB [including Matlab source code phase 1914]](/img/85/a2c159e7051cca01fe7ea177a5b1eb.png)
[RRT 3D path planning] rapid expansion of random tree UAV 3D path planning based on MATLAB [including Matlab source code phase 1914]

SRAM和DRAM之间的异同

TypeScript(7)泛型
随机推荐
【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell
Exclusive analysis | about resume and interview
Rasa dialogue robot helpdesk (V)
我想今天买股票,可以么?现在网上开户安全么?
[image enhancement] manual multiple exposure fusion amef image defogging based on MATLAB [including Matlab source code 1916]
The function of Schottky diode in preventing reverse connection of power supply
TypeScript(7)泛型
How many locks are added to an update statement? Take you to understand the underlying principles
QT基於RFID管理系統(可應用於大多數RFID管理系統)
After easycvr creates a new user, the video access page cannot be clicked. Fix the problem
[temperature detection] thermal infrared image temperature detection system based on Matlab GUI [including Matlab source code 1920]
3D, point cloud splicing
With this tool, automatic identification and verification code is no longer a problem
免疫组化和免疫组学之间的区别是啥?
最新版CorelDRAW Technical Suite2022
Kuboardv3与监控套件安装
[proteus simulation] 4x4 matrix keyboard interrupt mode scanning + nixie tube display
Similarities and differences between SRAM and DRAM
How can multidimensional analysis pre summary work?
Brief description of 802.1x Protocol