当前位置:网站首页>栈-------
栈-------
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;
和单链表一样
边栏推荐
- Binary stack (I) - principle and C implementation
- Value/hush in redis
- 使用set_handler过滤掉特定的SystemC Wraning &Error Message
- Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
- 指定的服务已标记为删除
- leetcode 406. Queue reconstruction by height
- On recursion and Fibonacci sequence
- Message queue monitoring refund task batch process
- Golang des-cbc
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
猜你喜欢

GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021

91.(cesium篇)cesium火箭發射模擬

Summary of JFrame knowledge points 1

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

C knowledge point form summary 2

ACLY与代谢性疾病

自组织是管理者和成员的双向奔赴

Build yocto system offline for i.mx8mmini development board

Harbor webhook from principle to construction

Software project management 9.2 Software project configuration management process
随机推荐
Sum of factor numbers of interval product -- prefix sum idea + fixed one shift two
Consolidate -c operator
[MCU] [nixie tube] nixie tube display
Le semester manquant
leetcode 406. Queue reconstruction by height
The Missing Semester
Comment Cao définit la décimale de dimension
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
CAD如何設置標注小數比特
想问问,证券开户有优惠吗手机开户是安全么?
C summary of knowledge points 1
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
NOV Schedule for . Net to display and organize appointments and recurring events
Unity XLua 协程封装
How to set decimal places in CAD
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 4
深入理解 grpc part1
S7-1500PLC仿真
技术分享 | MySQL:从库复制半个事务会怎么样?
MQ-防止消息丢失及重复消费

