数据结构 顺序栈

头文件

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;
}
相关推荐
此生决int6 分钟前
算法从入门到精通——前缀和
c++·算法·蓝桥杯
我星期八休息11 分钟前
Linux系统编程—库制作与原理
linux·运维·服务器·数据结构·人工智能·python·散列表
大大杰哥22 分钟前
leetcode hot100(4)矩阵
算法·leetcode·矩阵
小白|26 分钟前
cmake:昇腾CANN构建系统完全指南
java·c++·算法
nebula-AI27 分钟前
人工智能导论:模型与算法(未来发展与趋势)
人工智能·神经网络·算法·机器学习·量子计算·automl·类脑计算
炽烈小老头28 分钟前
【每天学习一点算法 2026/05/21】课程表
学习·算法
luoganttcc32 分钟前
大模型是否即将到达算法极限
算法
叶小鸡1 小时前
小鸡玩算法-力扣HOT100-动态规划(上)
算法·leetcode·动态规划
LuminousCPP1 小时前
数据结构 - 线性表第三篇:基于顺序表实现 C 语言通讯录(基础功能篇)
c语言·数据结构·经验分享·笔记·算法
_日拱一卒1 小时前
LeetCode:114二叉树展开为链表
java·开发语言·算法