数据结构 顺序栈

头文件

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;
}
相关推荐
阿贵---18 分钟前
C++中的备忘录模式
开发语言·c++·算法
setmoon21434 分钟前
C++中的观察者模式实战
开发语言·c++·算法
2403_8355684736 分钟前
C++代码规范化工具
开发语言·c++·算法
tankeven1 小时前
HJ138 在树上游玩
c++·算法
北顾笙9801 小时前
测开准备-day01数据结构力扣
数据结构
lihihi1 小时前
P1209 [USACO1.3] 修理牛棚 Barn Repair
算法
博界IT精灵2 小时前
栈在表达式求值中的应用(暂时看到视频3.3.2_1的25min19秒)
数据结构
weixin_387534222 小时前
Ownership - Rust Hardcore Head to Toe
开发语言·后端·算法·rust
庞轩px2 小时前
MinorGC的完整流程与复制算法深度解析
java·jvm·算法·性能优化
Queenie_Charlie2 小时前
Manacher算法
c++·算法·manacher