当前位置:网站首页>栈的顺序存储结构,链式存储结构及实现
栈的顺序存储结构,链式存储结构及实现
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;
}一些思考:
设计函数时可以添加一些细节,如销毁栈或判断栈是否为空时先判断栈是否创建,但一般在程序最开始和最末尾就是创建栈和销毁栈,所以本段代码不做此判断。
边栏推荐
- Hcip notes 11 days
- Unity is better to use the hot scheme Wolong
- Starting from business needs, open the road of efficient IDC operation and maintenance
- In the eyes of 100 users, there are 100 QQS
- WPF implements user avatar selector
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- Getting started with easyUI
- WPF 实现用户头像选择器
- 从数字化到智能运维:有哪些价值,又有哪些挑战?
- 8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
猜你喜欢

「数字安全」警惕 NFT的七大骗局

霸榜COCO!DINO: 让目标检测拥抱Transformer

基于redis6.2.4的redis cluster部署

Technical difficulties and applications of large humanoid robots

用秩讨论线性方程组的解/三个平面的位置关系

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

如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序

Outlook 教程,如何在 Outlook 中搜索日历项?

【目标检测】YOLOv5跑通VisDrone数据集

Add batch delete
随机推荐
Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
04. Find the median of two positive arrays
超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
PostgreSQL里有只编译语句但不执行的方法吗?
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
Page table cache of Linux kernel source code analysis
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
无聊发文吐槽工作生活
Enterprise live broadcast: witness focused products, praise and embrace ecology
Wu Enda logistic regression 2
C#入门基础教程
Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
失意的互联网人拼命叩开Web3大门
【redis】redis安装
Is the online account opening of Founder futures reliable and safe?
理财有保本产品吗?
更新|3DCAT实时云渲染 v2.1.2版本全新发布
博云容器云、DevOps平台斩获可信云“技术最佳实践奖”
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?