数据结构 顺序栈

头文件

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;
}
相关推荐
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者2 天前
J6B vio scenario sample
算法
BothSavage2 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn2 天前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法