数据结构 顺序栈

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;

#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2

typedef struct seqstack
{
    int* base; // 栈底指针 
    int* top;  // 栈顶指针
    int stacksize;   // 栈容量
}seqstack;

//1.初始化
void InitStack(seqstack* ps);
//2.栈的元素个数
size_t StackLength(const seqstack* ps);
//3.判空
bool IsEmpty(const seqstack* ps);
//4.判满
bool IsFull(const seqstack* ps);
//5.扩容
bool incem(seqstack* p);
//入栈
bool Push(seqstack* ps, int val);
//6.打印
void PrintStack(const seqstack* ps);
//7.出栈并获取栈顶元素
bool Pop(seqstack* ps, int* pval);
//8.获取栈顶元素
bool GetTop(seqstack* ps, int* pval);
//9.清空
void ClearStack(seqstack* ps);
//10.销毁
void DestroyStack(seqstack* ps);

源文件

cpp 复制代码
#include"顺序栈.h"

//1.初始化
void InitStack(seqstack* ps) {
	assert(ps != nullptr);
	int* p = (int*)malloc(sizeof(int) * STACK_INIT_SIZE);
	if (p == nullptr) {
		cout << "init err" << endl;
		return;
	}
	ps->base = ps->top = p;
	ps->stacksize = STACK_INIT_SIZE;
}
//2.栈的元素个数
size_t StackLength(const seqstack* ps) {
	assert(ps != nullptr);
	return ps->top - ps->base;
}
//3.判空
bool IsEmpty(const seqstack* ps) {
	assert(ps != nullptr);
	return ps->base == ps->top;
}
//4.判满
bool IsFull(const seqstack* ps) {
	assert(ps != nullptr);
	return (ps->top - ps->base) == ps->stacksize;
}
//扩容
bool incem(seqstack* ps) {
	assert(ps != nullptr);
	int newstacksize = ps->stacksize * STACKINCREMENT;
	int* p = (int*)realloc(ps->base,sizeof(int) * newstacksize);
	if (p == nullptr) {
		cout << "expand err" << endl;
		return false;
	}
	ps->base = p;
	ps->stacksize = newstacksize;
	ps->top = p + StackLength(ps);
	return true;
}
//5.入栈
bool Push(seqstack* ps, int val) {
	assert(ps != nullptr);
	if (IsFull(ps)&&!incem(ps))return false;
	*ps->top = val;
	ps->top++;
	return true;
}
//6.打印
void PrintStack(const seqstack* ps) {
	assert(ps != nullptr);
	if (IsEmpty(ps))return;
	for (int* p = ps->top - 1; p>=ps->base; p--) {
		cout << *p << " ";
	}cout << endl;
}
//7.出栈并获取栈顶元素
bool Pop(seqstack* ps, int* pval) {
	assert(ps != nullptr);
	if (IsEmpty(ps))return false;
	ps->top--;
	*pval = *(ps->top);
	return true;
}
//8.获取栈顶元素
bool GetTop(seqstack* ps, int* pval) {
	assert(ps != nullptr);
	if (IsEmpty(ps))return false;
	*pval = *(ps->top - 1);
	return true;
}
//9.清空
void ClearStack(seqstack* ps) {
	assert(ps != nullptr);
	if (IsEmpty(ps))return;
	ps->top = ps->base;
}
//10.销毁
void DestroyStack(seqstack* ps) {
	assert(ps != nullptr);
	free(ps->base);
	ps->base = ps->top = nullptr;
	ps->stacksize = 0;
}
int main() {

	return 0;
}
相关推荐
wuweijianlove1 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong1 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志1 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
黎阳之光2 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_112 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
wfbcg2 小时前
每日算法练习:LeetCode 209. 长度最小的子数组 ✅
算法·leetcode·职场和发展
_日拱一卒3 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode
计算机安禾3 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio
SatVision炼金士3 小时前
合成孔径雷达干涉测量(InSAR)沉降监测算法体系
算法