目录-总 -分- 总结构
- 图片可视化 总
- 源码
- 1.头文件介绍---分
- 2.节点的实现
- 3.栈顶栈底
- 4.函数的提前声明
- 5. 栈 ---初始化栈
- 6. 栈 ---进栈
- 7.栈 --- 遍历
- 8.栈 --- 是否为空
- 9.栈 --- 出栈
- 10总结
图片可视化 总
源码
/*time 2023年6月12日12:39:06auther yzmcntent stract 栈
*/#include<stdio.h>//头文件
#include<malloc.h>//分配地址空间
#include<stdlib.h>//实现exit 终止程序//定义节点
typedef struct node {int Data;struct node* pNext;}NODE, * PNODE;//栈
typedef struct stract {PNODE stractTop;//指向栈顶PNODE stractBottom;//指向栈底部
} STRACT, * PSTRACT;//函数声明
PSTRACT init(PSTRACT pS);
void push(PSTRACT pS, int value);
void transverse(PSTRACT pS);
void pop(PSTRACT pS);
bool empty(PSTRACT pS);int main() {//创建栈顶 栈底STRACT S;//1.初始化栈PSTRACT pS =init(&S);//进栈push(pS, 1);push(pS, 2);push(pS, 3);push(pS, 4);push(pS, 5);push(pS, 6);push(pS, 7);//遍历输出transverse(pS);printf("出栈第1次");pop(pS);transverse(pS);printf("进栈第1次");push(pS, 74);transverse(pS);printf("出栈第2次");pop(pS);transverse(pS);printf("出栈第3次");pop(pS);transverse(pS);printf("进栈第2次");push(pS, 45);transverse(pS);printf("进栈第3次");push(pS, 54);transverse(pS);return 0;
}
//初始化栈
PSTRACT init(PSTRACT pS) {//申请地址PNODE phead = (PNODE)malloc(sizeof(NODE));phead->pNext = NULL;if (phead == NULL){printf("地址分配失败");exit(-1);}pS->stractTop = phead;pS->stractBottom = phead;return pS;
}
//进栈
void push(PSTRACT pS,int value) {//创建节点PNODE pNew = (PNODE)malloc(sizeof(NODE));if (pNew == NULL){printf("地址分配失败");exit(-1);}//pnew 赋值pNew->Data = value;//pnew 指向pNew->pNext = pS->stractTop;//修改pS->stractTop; 栈顶指针pS->stractTop = pNew;}
//遍历
void transverse(PSTRACT pS) {//创建 移动遍历的指针 指向栈顶PNODE p = pS->stractTop;while (p->pNext != NULL){printf("%d ", p->Data);p = p->pNext;}printf("\n");return;
}//是否为空
bool empty(PSTRACT pS) {if (pS->stractBottom == pS->stractTop){return true;}else {return false;}
}//出栈
void pop(PSTRACT pS) {//出栈//判空操作 if (empty(pS)){printf("空栈 无法出栈");return;}//1.t存放栈顶节点PNODE t = pS->stractTop;//2.修改栈顶的指针pS->stractTop = t->pNext;//释放 tfree(t);return;}
1.头文件介绍—分
#include<stdio.h>//头文件
#include<malloc.h>//分配地址空间
#include<stdlib.h>//实现exit 终止程序
2.节点的实现
//定义节点
typedef struct node {int Data;struct node* pNext;}NODE, * PNODE;
以上就是 完成 这样的结构
3.栈顶栈底
typedef struct stract {PNODE stractTop;//指向栈顶PNODE stractBottom;//指向栈底部
} STRACT, * PSTRACT;
4.函数的提前声明
PSTRACT init(PSTRACT pS);
void push(PSTRACT pS, int value);
void transverse(PSTRACT pS);
void pop(PSTRACT pS);
bool empty(PSTRACT pS);
把要实现的函数 提前
5. 栈 —初始化栈
//初始化栈
PSTRACT init(PSTRACT pS) {//申请地址PNODE phead = (PNODE)malloc(sizeof(NODE));phead->pNext = NULL;if (phead == NULL){printf("地址分配失败");exit(-1);}pS->stractTop = phead;pS->stractBottom = phead;return pS;
}
6. 栈 —进栈
//进栈
void push(PSTRACT pS,int value) {//创建节点PNODE pNew = (PNODE)malloc(sizeof(NODE));if (pNew == NULL){printf("地址分配失败");exit(-1);}//pnew 赋值pNew->Data = value;//pnew 指向pNew->pNext = pS->stractTop;//修改pS->stractTop; 栈顶指针pS->stractTop = pNew;}
7.栈 — 遍历
//遍历
void transverse(PSTRACT pS) {//创建 移动遍历的指针 指向栈顶PNODE p = pS->stractTop;while (p->pNext != NULL){printf("%d ", p->Data);p = p->pNext;}printf("\n");return;
}
8.栈 — 是否为空
//是否为空
bool empty(PSTRACT pS) {if (pS->stractBottom == pS->stractTop){return true;}else {return false;}
}
9.栈 — 出栈
//出栈
void pop(PSTRACT pS) {//出栈//判空操作 if (empty(pS)){printf("空栈 无法出栈");return;}//1.t存放栈顶节点PNODE t = pS->stractTop;//2.修改栈顶的指针pS->stractTop = t->pNext;//释放 tfree(t);return;}
10总结
数据结构–栈
就像是一个木桶
每一个节点就是砖头
进栈
就是把砖头放进去
出栈
就是把砖头拿出来
那也就一意味着
最先放进去的砖头 被压着 只能把上面的拿出来 才能那下面的
这也就是 先进后出 后进先出
的概念