当前位置:网站首页>栈-------
栈-------
2022-07-01 12:05:00 【Between the steps】
顺序栈,用顺序存储方式实现的栈,其基本操作:增删改查,判空,判满操作
1. 首先是初始化操作:
#include<stdio.h>
#define Maxsize 10 //栈中最大元素个数
typedef struct{
ElemType data[Maxsize]; //静态数组中放栈的元素
int top; //栈顶指针
}SqStack;
void InitStack(SqStack &S){
S.top=-1; //初始化栈顶指针
}
bool StackEmpty(SqStack S){
if(S.top==-1)
return true; //栈空
else
return false;
}
void testStack{
SqStack S; //声明一个顺序栈,为其分配空间
}
int main(){
SqStack S;
InitStack(S);
//后续增删改查操作
}

2.进栈操作
先判栈是否满了,不满就让栈顶指针加1,再让新元素入栈
bool push(SqStack &S,ElemType x){
if(S.top==Maxsize-1) //栈满,报错
return false;
S.top=S.top+1; //指针先加1
S.data[S.top]=x; //新元素入栈
return true;
}

3.出栈操作
bool Pop(SqStack &S,ElemType &x){
if(S.top==-1)
return false; //栈空,,报错
x=S.data[S.top]; //栈顶元素先出栈
S.top=S.top-1; //指针减1
return true;
}

4.读取栈顶元素
bool GetTop(SqStack S,ElemType &x){
if(S.top==-1)
return false;
x=S.data[S.top];
return true;
}

共享栈
共享栈
共享栈是栈底栈顶都有指针
#include<stdio.h>
#define Maxsize 10 //栈中最大元素个数
typedef struct{
ElemType data[Maxsize]; //静态数组中放栈的元素
int top0; //0号栈栈顶指针
int top1; //1号栈栈顶指针
}SqStack;
void InitStack(SqStack &S){
S.top0=-1; //初始化top0栈顶指针
S.top1=Maxsize; //初始化top1栈顶指针
}

判栈满的条件:top0+1==top1 ?
链栈
链栈
链栈类似于单链表的头插法,后插入的在右边 如
所以我们只需出栈在链头出栈即可
typedef struct{
ElemType data;
struct LinkNode *next;
}LinkNode,*LiStack;
和单链表一样
边栏推荐
- Machine learning - Data Science Library Day 3 - Notes
- Joint Time-Frequency and Time Domain Learning for Speech Enhancement
- MQ-防止消息丢失及重复消费
- 二叉堆(一) - 原理与C实现
- On recursion and Fibonacci sequence
- Mysql database knowledge collation
- Dataset partitioning script for classification tasks
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 2
- 陈珙:微服务,它还那么纯粹吗?
- usb peripheral 驱动 - cable connect/disconnect
猜你喜欢

Botu V15 add GSD file

Machine learning - Data Science Library - day two

Harbor webhook from principle to construction

LeetCode力扣(剑指offer 31-35)31. 栈的压入弹出序列32I.II.III.从上到下打印二叉树33. 二叉搜索树的后序遍历序列34. 二叉树中和为某一值的路径35. 复杂链表的复制
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7](/img/41/e3ecbd49e4bfeab6c6e7d8733fe33a.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7

【datawhale202206】pyTorch推荐系统:多任务学习 ESMM&MMOE
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6](/img/0e/0900e386f3baeaa506cc2c1696e22a.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6

Message queue monitoring refund task batch process

Leetcode force buckle (Sword finger offer 31-35) 31 Stack push pop-up sequence 32i II. 3. Print binary tree from top to bottom 33 Post order traversal sequence 34 of binary search tree The path with a

GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
随机推荐
Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs
邻接矩阵无向图(一) - 基本概念与C语言
C#依赖注入(直白明了)讲解 一看就会系列
Chen Gong: Micro service, is it still so pure?
二叉堆(一) - 原理与C实现
91.(cesium篇)cesium火箭發射模擬
Leetcode (Sword finger offer) - 58 - ii Rotate string left
Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
Emotion analysis based on IMDB comment data set
Mysql database knowledge collation
Learning summary on June 29, 2022
[classic example] classic list questions @ list
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 3
CPI tutorial - asynchronous interface creation and use
Technology sharing | MySQL: how about copying half a transaction from the database?
Force button homepage introduction animation
The Missing Semester
Understanding of MVVM and MVC
谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征
Abbirb120 industrial robot mechanical zero position

