当前位置:网站首页>04_ Stack
04_ Stack
2022-07-02 15:13:00 【Listen to the rain】
Stack

What is stack?

Why must it be the end of the sequence table , Header of linked list :

The header of the linked list can be accessed directly , The time complexity is O(1)
It is essentially a linear table ( Access to linear tables with certain restrictions )
principle : Last in, first out , Later, you need to serve first
Stack 2 A design :
- Sequence table design , Called sequential stack
- Chain design , It is called chain stack
1 Indefinite length sequential stack
Mode one :
The stack can only be inserted and deleted at one end , The end where the insertion and deletion are performed is called To the top of the stack , The other end is called At the bottom of the stack ( The top of the stack is the end of the sequence table )
top Yes 2 Layer meaning :
- 1: Number of currently saved data ; As shown in the figure above, the number of valid data is 5 individual
- 2: The subscript of the next data to insert ; As shown in the figure above, the next data should be inserted into 5 Subscript no.
// Stack : Last in, first out , Later, you need to serve first ( Access restricted linear table )
// Stacks are divided into sequential stacks , Chain stack
// This document is an indefinite length sequence stack , It can automatically expand capacity
// The end of the stack that can only be inserted and deleted at one end is called the top of the stack , The other end is called the bottom of the stack
// The top of the sequence stack is at the end , Because the time complexity of entering and leaving the stack is O(1)
// Indefinite length sequential stack ( Access to linear tables with certain restrictions )
typedef struct Stack
{
int* base; // Point to dynamic memory
int stackSize; // Total stack size ( Total number of cells )
int top; // Top pointer of stack ; There is no need to design the pointer at the top of the stack of the sequence table as a pointer , Subscripts are more practical
// The subscript that can store data at present
}Stack,*PStack;
// initialization ( similar C++ Middle constructor )
void InitStack(PStack ps);
// Full sentence
bool IsFull(PStack ps);
// Stack expansion function , Capacity expansion 2 times
void Inc(PStack ps);
// Put data into the stack ( Into the stack, )
bool Push(PStack ps, int val);
// Get the value of the top of the stack element , But don't delete
//int GetTop(PStack ps);// There's something wrong with the design
bool GetTop(PStack ps,int* rtval);//rtval: Output parameters
// int *rtval usage : Define a variable , Send the address in , Function internal dereference , The value of the parameter can be brought out
// Similar to int a ;scanf_s("%d",&a);
// Get the value of the top of the stack element , And delete
//int Pop(PStack ps);
bool Pop(PStack ps, int* rtval);
// Sentenced to empty
bool IsEmpty(PStack ps);
// Get the number of stack valid data
int GetLength(PStack ps);
// Clear all data
void Clear(PStack ps);
// The destruction
void Destory(PStack ps);
Mode two :
data structure :
Initialization function :
Empty stack :
Destroy function :
Get the number of data in the stack :
Stack capacity :
Stack empty :
Stack full :
Stack expansion :
Push :
Take the top element of the stack :
Out of the stack :
Merge the elements at the top of the stack and out of the stack :

Non recursive implementation of fast scheduling :

2 Chain stack
// Use the single linked list of the leading node to realize
// To the top of the stack : Push and pull , Stack top in header ( The time complexity of entering and leaving the stack is O(1)),
// The top of the stack is not fixed at that end , Adjust according to the structural characteristics of the current linked list
typedef struct LSNode
{
int data;
struct LSNode* next;
}LSNode,*PLStack;// The node of the chain stack , Because the top of the stack is at the first data node , So no need top The pointer
// initialization
void InitLStack(PLStack ps);
// Full sentence
bool IsFull(PLStack ps);
// Stack expansion function , Capacity expansion 2 times
void Inc(PLStack ps);
// Put data into the stack ( Into the stack, )
bool Push(PLStack ps, int val);
// Get the value of the top of the stack element , But don't delete
bool GetTop(PLStack ps, int* rtval);//rtval: Output parameters
// Get the value of the top of the stack element , And delete
bool Pop(PLStack ps, int* rtval);
// Sentenced to empty
bool IsEmpty(PLStack ps);
// Get the number of stack valid data
int GetLength(PLStack ps);
// Clear all data
void Clear(PLStack ps);
// The destruction
void Destory(PLStack ps);
边栏推荐
- info [email protected]: The platform “win32“ is incompatible with this module.
- SQL 后计算的利器 SPL
- IE 浏览器正式退休
- Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
- 02_线性表_顺序表
- 牛客练习赛101
- 使用 TiUP 部署 TiDB 集群
- Kityformula editor configure font size and spacing
- 记一次报错解决经历依赖重复
- How to conduct TPC-C test on tidb
猜你喜欢

Map介绍

【无标题】LeetCode 2321. 拼接数组的最大分数

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

Introduction to C language -- array

Application and practice of Jenkins pipeline

Mavn 搭建 Nexus 私服
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

Dragonfly low code security tool platform development path

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

08_ 串
随机推荐
btrace-(字节码)动态跟踪工具
Advanced C language (realize simple address book)
LeetCode_字符串_简单_412.Fizz Buzz
Implementation of n queen in C language
C# 线程传参
Application and practice of Jenkins pipeline
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
原则、语言、编译、解释
使用 TiUP 部署 TiDB 集群
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
2021-2022學年編譯原理考試重點[華僑大學]
Table responsive layout tips
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
CDN 在游戏领域的应用
【NOI模拟赛】伊莉斯elis(贪心,模拟)
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
MFC 定时器使用
Tidb data migration scenario overview
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
Tidb data migration tool overview