当前位置:网站首页>栈的顺序存储结构,链式存储结构及实现
栈的顺序存储结构,链式存储结构及实现
2022-07-25 17:16:00 【进阶的小新】
【顺序存储结构】
栈空的条件:s->top == -1;
栈满的条件:s->top == MAX_SIZE-1;
进栈:先将栈顶指针top++,再将元素e放置在栈顶指针所指处;
出栈:先将栈顶元素赋值给e,再让top--。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct stack {
int data[MAX_SIZE];
int top;
}STACK, *PSTACK;
//初始化栈
void init(PSTACK &s) {
s = (STACK*)malloc(sizeof(STACK));
s->top = -1;
}
//销毁栈
void destroyStack(PSTACK s) {
free(s);
}
//判断栈是否为空
bool isEmpty(PSTACK s) {
return s->top==-1;
}
bool isFull(PSTACK s) {
return s->top == MAX_SIZE-1; //c++中面向对象另设成员变量size,因为有参构造时size可能与MAX_SIZE不一样
}
//进栈
//top指针先++,再压入元素
bool push(PSTACK s, int e) {
if(isFull(s))
return false;
s->top++;
s->data[s->top] = e;
return true;
}
//出栈并返回出栈元素
//先保存出栈元素,top指针再--
bool pop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->data[s->top];
s->top--;
return true;
}
//取栈顶元素
bool getTop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->data[s->top];
return true;
}
//从底部开始遍历栈
bool stackPrint(PSTACK s) {
if(isEmpty(s))
return false;
for(int i=0;i<=s->top;i++)
printf("%d ",s->data[i]); //可以另写一个函数用于输出
printf("\n");
return true;
}
int main() {
int e;
PSTACK s;
init(s);
if(isEmpty(s)) printf("栈为空!\n");
for(int i=0;i<=10;i+=2)
if(!isFull(s))
push(s,i);
if(isFull(s)) printf("栈满!\n");
for(int i=0;i<2;i++)
if(pop(s,e)) { //只需考虑出栈是否成功,栈是否为空 放在pop函数中判断
printf("本次出栈元素为%d\n",e);
}
if(!stackPrint(s)) printf("栈为空!\n"); //在调用函数时即会执行函数,也会返回状态,所以不用写else
return 0;
}【链式存储结构】
栈空条件:s->next == NULL;
链式栈不要考虑栈满条件
进栈:新增节点插入到头结点之后
出栈:取出首节点的值并释放首节点
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}STACK, *PSTACK;
//初始化栈
void init(PSTACK &s) {
s = (STACK*)malloc(sizeof(STACK));
s->next = NULL;
}
//销毁栈(连头结点一起销毁)
void destroy(PSTACK s) {
PSTACK q = s, p = s->next;
while(p) {
free(q);
q = p;
p = p->next;
}
free(q);
}
//判断栈是否为空
bool isEmpty(PSTACK s) {
return s->next == NULL;
}
//入栈
void push(PSTACK s, int e) {
PSTACK p = (STACK*)malloc(sizeof(STACK));
p->data = e;
p->next = s->next;
s->next = p;
}
//出栈
bool pop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
PSTACK p = s->next;
e = p->data;
s->next = p->next;
free(p);
return true;
}
//获取栈顶元素
bool getTop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->next->data;
return true;
}
//从栈顶开始遍历栈
bool print(PSTACK s) {
if(isEmpty(s))
return false;
PSTACK p = s->next;
while(p) {
printf("%d ",p->data); //可以另写一个函数用于输出
p = p->next;
}
printf("\n");
return true;
}
int main() {
int e;
PSTACK s;
init(s);
if(isEmpty(s)) printf("栈为空!\n");
for(int i=1;i<=10;i+=2)
push(s,i);
for(int i=0;i<2;i++)
if(pop(s,e)) {
printf("本次出栈元素为%d\n",e);
}
if(!print(s)) printf("栈为空");
destroy(s);
return 0;
}一些思考:
设计函数时可以添加一些细节,如销毁栈或判断栈是否为空时先判断栈是否创建,但一般在程序最开始和最末尾就是创建栈和销毁栈,所以本段代码不做此判断。
边栏推荐
- Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
- 聊聊如何用 Redis 实现分布式锁?
- Rainbow plug-in extension: monitor MySQL based on MySQL exporter
- [knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification
- 【南京航空航天大学】考研初试复试资料分享
- 新版selenium4.3在egde浏览器的无头模式
- Page table cache of Linux kernel source code analysis
- 2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
- Interface automation test postman+newman+jenkins
- 【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
猜你喜欢

Automatic reply of wechat official account development message

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用

在华为昇腾Ascend910上复现swin_transformer

Mindoc makes mind map

Wu Enda logistic regression 2

Starting from business needs, open the road of efficient IDC operation and maintenance

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

How to deploy applications on IPFs using 4everland cli

In the eyes of 100 users, there are 100 QQS

HCIP笔记十一天
随机推荐
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
Chapter III data types and variables
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
mindoc制作思维导图
Page table cache of Linux kernel source code analysis
Multi tenant software development architecture
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
Hcip notes 11 days
Does PgSQL have a useful graphical management tool?
[target detection] yolov5 Runtong voc2007 dataset (repair version)
After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China
Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time
Enumeration classes and magic values
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
【目标检测】YOLOv5跑通VOC2007数据集(修复版)