顺序栈的基本操作

本文介绍了如何使用顺序栈进行初始化、销毁、清空、判断空、获取栈顶元素以及加入和删除栈顶操作。重点展示了栈的内存管理,包括动态分配和释放。通过实例演示了 SqStack 结构和其在 main.cpp 中的应用。
摘要由CSDN通过智能技术生成
//constant.h
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TURE 1
#define FALSE 0 
typedef int Status;
//SqStack.h
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 5
struct SqStack{
	SElemType *base;
	SElemType *top;
	int stacksize;
};//顺序栈 

//SqStack_Operation.cpp
// 1 初始化
void InitStack(SqStack &S){
	if(!(S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType))))
		exit(OVERFLOW);
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
} 

// 2 销毁
void DestroyStack(SqStack &S){
	free(S.base);
	S.base = NULL;
	S.top = NULL;
	S.stacksize = 0;
} 

// 3 清空
void ClearStack(SqStack &S){
	S.top = S.base;
} 

// 4 判空
Status StackEmpty(SqStack S){
	if(S.top == S.base)
		return TURE;
	return FALSE;
} 

// 5 栈长
int StackLength(SqStack S){
	return S.top - S.base;
} 

// 6 取栈顶
Status GetTop(SqStack S, SElemType &e){
	if(S.top > S.base){
		e = *(S.top - 1);
		return OK;
	}
	return ERROR;
} 

// 7 加入栈顶
void Push(SqStack &S, SElemType e){
	if(S.top - S.base == S.stacksize){
		S.base = (SElemType *)realloc( S.base, ( S.stacksize + STACK_INCREMENT) * sizeof(SElemType));
		if(!S.base)
			exit(OVERFLOW);
		S.top = S.base + S.stacksize;
		S.stacksize += STACK_INCREMENT;
	}
	*(S.top)++ = e;
} 

// 8 删除栈顶 
Status Pop(SqStack &S, SElemType &e){
	if(S.top == S.base)
		return ERROR;
	e = *--S.top;
	return OK; 
}

// 9 遍历
void StackTraverse(SqStack S, void(*visit) (SElemType)){
	while(S.top > S.base)
		visit(*S.base++);
	printf("\n");
} 

//main.cpp
#include <iostream>
#include "constant.h"
typedef int SElemType;
#include "SqStack.h"
#include "SqStack_Operation.cpp"
using namespace std;

void print(SElemType c){
	cout << c << ' ';
}

int main() {
	SqStack S;
	int e;
	
	InitStack(S);
	
	for(int i = 1; i <= 10; i ++)
		Push( S, i);
	cout << "stack is " << endl;
	StackTraverse(S, print);
	cout << "StackLength is " << StackLength( S) << endl;
	cout << "S.stacksize is " << S.stacksize << endl;
	GetTop( S, e);
	cout << "Top is " << e << endl << endl;
	
	Push( S, 11);
	cout << "after Push 11" << endl;
	cout << "stack is " << endl;
	StackTraverse(S, print);
	cout << "StackLength is " << StackLength( S) << endl;
	cout << "S.stacksize is " << S.stacksize << endl;
	GetTop( S, e);
	cout << "Top is " << e << endl << endl;
	
	Pop( S, e);
	cout << "after Pop" << endl;
	cout << "stack is " << endl;
	StackTraverse(S, print);
	cout << "StackLength is " << StackLength( S) << endl;
	cout << "S.stacksize is " << S.stacksize << endl;
	GetTop( S, e);
	cout << "Top is " << e << endl << endl;
	
	ClearStack( S);
	cout << "after ClearStack" << endl;
	cout << "stack is emtpy? (1 YES, 2 NO)" << endl << StackEmpty( S) << endl << endl;
	
	DestroyStack( S);
	cout << "after Destroy" << endl;
	printf("S.top = %d, S.base = %d, S.stacksize = %d", S.top, S.base, S.stacksize);
	cout << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值