当前位置: 首页 > news >正文

栈的创建和基本操作

栈的创建一般有顺序栈和链栈,下面我将给大家一一介绍

顺序栈:顾名思义就是在数组中存储的栈,在内存中占据一块连续的内存空间

顺序栈代码实现:

#include <stdio.h>
#include <stdbool.h>
#define MaxSize 10  //假设顺序栈的最大长度是10
typedef struct Sqstack
{
int data[MaxSize];
int top;//指向栈顶
}Sqstack;
bool InitList(Sqstack* S)
{
S->top = 0;//指向栈底
return true;
}
bool Push(Sqstack* S)//进栈
{
if (S->top == MaxSize)//满栈,第一次满栈插入不了任何数据返回false
return false;
int i;
scanf("%d", &i);
while (i != 9999)//假设输出9999退出
{
if (S->top == MaxSize)//满栈了,第二次插入数据后满栈返回true
return true;
S->data[S->top] = i;
S->top++;
scanf("%d", &i);
}
return true;
}
bool Pop(Sqstack* S)//出栈
{
if (S->top == 0)//空栈
return false;
int n;
int i;
printf("请输入出栈个数:\n");
scanf("%d", &n);
if (n > S->top)//如果出栈个数大于总个数直接返回
return false;
for (i = 0; i < n; i++)
{
S->top--;//由于是静态数组存储无法手动释放内存
}
return true;
}
int GetTop(Sqstack S, int* x)//查找栈顶元素
{
if (S.top == 0)//空栈
{
printf("空栈\n");
return 0;
}
*x = S.data[S.top - 1];//必须减1
return *x;
}
bool destoryStack(Sqstack* S)
{
if (S->top == 0)//空栈
return false;
S->top = 0;//令栈顶为0

    return true;
}
bool PrintStack(Sqstack S)//打印栈
{
if (S.top == 0)//空栈
return false;
int i;
for (i = 0; i < S.top; i++)
{
printf("%d ", S.data[i]);
}
return true;
}
int main()
{
Sqstack S;
InitList(&S);//初始化栈
Push(&S);//进栈
Pop(&S);//出栈

    int x;
GetTop(S,&x);//查找栈顶指针
printf("栈顶指针为:%d\n", x);

int del;
printf("是否销毁栈:(0销毁,1不销毁)\n");
scanf("%d", &del);
destoryStack(&S);

    PrintStack(S);//打印栈
return 0;
}

链栈:链栈是由链表来实现的一种栈操作,一般用不带头结点的单链表实现

链栈的代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct SNode
{
int data;//栈的数据
struct SNode* next;//栈里的下一个结点
}SNode,*LinkStack;
bool InitList(LinkStack* S)//初始化链表
{
(*S) = NULL;//表示当前栈为空
return true;
}
bool Push(LinkStack* S)//进栈
{
//第一次入栈
SNode* p = (SNode*)malloc(sizeof(SNode));
if (p == NULL)
return false;
int i;
scanf("%d", &i);
p->data = i;
p->next = NULL;
(*S) = p;

    //后续入栈
scanf("%d", &i);//假设输入9999退出
while (i != 9999)
{
SNode* q = (SNode*)malloc(sizeof(SNode));
if (q == NULL)
return false;
q->data = i;
q->next = (*S);
(*S) = q;
scanf("%d", &i);
}
return true;
}
int Length(LinkStack S)//获取栈的长度
{
SNode* p = S;
int n = 0;
if (p == NULL)//空栈长度为0
return n;
while (p != NULL)
{
p = p->next;
n++;
}
return n;
}
bool Pop(LinkStack* S)//出栈
{
SNode* p = (*S);//指向栈顶
if (p == NULL)//空栈无法出栈
return false;
int n,i;
int len = Length(*S);//获取栈长度
printf("请输入要出栈几次\n");
scanf("%d", &n);
if (n > len)//出栈次数超过栈长度
return false;
for (i = 0; i < n; i++)
{
SNode* temp = p;
(*S) = (*S)->next;
free(temp);
p = (*S);
}
return true;

}
SNode* GetTop(LinkStack S)
{
SNode* p = S;
if (p == NULL)
return NULL;
return p;
}
bool Destory(LinkStack* S)
{
SNode* p = (*S);//指向栈顶
if (p == NULL)
return false;
while ((*S) != NULL)
{
SNode* temp = p;
(*S) = (*S)->next;
free(temp);
p = (*S);
}
return true;
}
bool PrintList(LinkStack S)//打印栈
{
if (S == NULL)//空栈
return false;
SNode* p = S;//指向栈顶
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
return true;
}
int main()
{
LinkStack S;
InitList(&S);//初始化
Push(&S);//进栈
Pop(&S);//出栈
//GetTop(S);//查找栈顶指针
//Destory(&S);//销毁栈
PrintList(S);//打印栈

    return 0;
}

http://www.dtcms.com/a/348672.html

相关文章:

  • Arbess V1.1.4版本发布,支持Mysql数据库,Ubuntu系统,新增SSH及Hadess上传下载任务
  • week4-[字符数组]月份
  • TCP连接与UDP协议
  • 构建现代前端工程:Webpack/Vite/Rollup配置解析与最佳实践
  • C++20: std::span
  • 目标检测数据集 第005期-基于yolo标注格式的PCB组件检测数据集(含免费分享)
  • 【Ollama】本地OCR
  • 基于SpringBoot的校园信息共享系统【2026最新】
  • pod管理
  • scanner、arrylist、反转数组
  • FPGA 时序分析(五)
  • 十、redis 入门 之 redis事务
  • (Redis)主从哨兵模式与集群模式
  • 【机器学习】7 Linear regression
  • VScode设置鼠标滚轮调节代码
  • 嵌入式第三十六天(网络编程(TCP))
  • springboot项目搭建步骤
  • 【Flink】部署模式
  • Maven项目中settings.xml终极优化指南
  • Excel 表格 - 乘法与除法处理(保留两位小数四舍五入实现、保留两位小数截断实现、添加百分号)
  • 单片机外设(七)RTC时间获取
  • 深入解析Java NIO多路复用原理与性能优化实践指南
  • 重置MySQL数据库的密码指南(Windows/Linux全适配)
  • 基于springboot的理商管理平台设计与实现、java/vue/mvc
  • 得物25年春招-安卓部分笔试题1
  • Linux camera 驱动流程介绍(rgb: ov02k10)(chatgpt version)
  • AlmaLinux 上 Python 3.6 切换到 Python 3.11
  • EP02:【DA】数据分析的价值创造与应用流程
  • 基于SpringBoot的新能源汽车租赁管理系统【2026最新】
  • 【Linux文件系统】Linux文件系统与设备驱动