顺序栈基本功能的实现

本文基于严蔚敏的《数据结构》教材,详细介绍了顺序栈的基本功能实现,包括入栈、出栈等操作,与书中的描述保持一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开始复习严蔚敏的《数据结构》,以下是顺序栈基本功能的实现,与书中完全对应。

/*
* 功能:顺序栈的实现
* 作者:风清扬
* 日期:2014-12-30
*/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define TRUE	1
#define FALSE	0
#define OK		1
#define ERROR	0

#define OVERFLOW -2

#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2

typedef int Status;
typedef int SElemType;

//==============================================================================
/*------------------------------定义顺序栈数据类型-----------------------------*/

typedef struct
{
	SElemType * bottom;//栈底
	SElemType * top;//栈顶
	int stackSize;//栈容量
} SqStack;

void InitStack_Sq(SqStack * S);
void DestroyStack_Sq(SqStack * S);
void ClearStack_Sq(SqStack * S);
Status StackEmpty_Sq(SqStack * S);
int StackLength_Sq(SqStack * S);
Status GetTop_Sq(SqStack * S, SElemType * e);
void Push(SqStack * S, SElemType e);
Status Pop(SqStack * S, SElemType * e);
void StackTraverse_Sq(SqStack * S, void (* vist)(SElemType));
//===============================================================================

void visit(SElemType e)
{
	printf("%d ", e);
}

//===========================================================================================================
int main(void)
{
	int index;
	SqStack stack;
	SElemType e;

	InitStack_Sq(&stack);
	for(index=1; index<=12; ++index)
		Push(&stack, index);
	printf("栈中元素依次为: ");
	StackTraverse_Sq(&stack, visit);
	Pop(&stack, &e);
	printf("出栈元素为: %d\n", e);
	printf("栈空否(1:空0:否):%d\n", StackEmpty_Sq(&stack));
	GetTop_Sq(&stack, &e);
	printf("栈顶元素为: %d\n栈的长度为: %d\n", e, StackLength_Sq(&stack));
	ClearStack_Sq(&stack);
	printf("清空栈后,栈空否(1:空0:否):%d\n",StackEmpty_Sq(&stack));
	DestroyStack_Sq(&stack);
	printf("销毁栈后,stack.top=%u stack.bottom=%u s.stackSize=%d\n",stack.top,stack.bottom, stack.stackSize);

	return 0;
}
//===========================================================================================================


//===========================================================================================================
/*------------------------------------------顺序栈基本功能的实现-------------------------------------------*/

//构造一个空栈,栈的初始容量为 STACK_INIT_SIZE
void InitStack_Sq(SqStack * S)
{
	S->bottom = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	if (!S->bottom)
	{
		printf("内存不足,初始化栈失败,程序终止!\n");
		exit(OVERFLOW);
	}
	S->top = S->bottom;
	S->stackSize = STACK_INIT_SIZE;
}

//销毁S指向的栈,栈不再存在
void DestroyStack_Sq(SqStack * S)
{
	free(S->bottom);
	S->bottom = S->top = NULL;
	S->stackSize = 0;
}

//把S指向的栈置为空栈
void ClearStack_Sq(SqStack * S)
{
	S->top = S->bottom;
}

//若S指向的栈为空,返回TRUE,否则返回FALSE
Status StackEmpty_Sq(SqStack * S)
{
	if (S->bottom == S->top)
		return TRUE;
	else
		return FALSE;
}

//返回S指向的栈中元素个数,即栈的长度
int StackLength_Sq(SqStack * S)
{
	return S->top-S->bottom;
}

//若栈不空,则用e返回栈顶元素,并返回OK;否则返回ERROR
Status GetTop_Sq(SqStack * S, SElemType * e)
{
	if (S->bottom == S->top)
		return ERROR;
	*e = *(S->top-1);

	return OK;
}

//入栈,如果栈满,则追加 STACK_INCREMENT 个元素空间
void Push(SqStack * S, SElemType e)
{
	if ((S->top-S->bottom) == S->stackSize)
	{
		S->bottom = (SElemType *)realloc(S->bottom, (S->stackSize+STACK_INCREMENT)*sizeof(SElemType));
		if (!S->bottom)
		{
			printf("内存分配失败,程序终止!\n");
			exit(OVERFLOW);
		}
		S->top = S->bottom+S->stackSize;
		S->stackSize += STACK_INCREMENT;
	}
	*(S->top)++ = e;
}

//出栈,若栈不空,则删除栈顶元素,用e返回其值,并返回OK;否则返回ERROR
Status Pop(SqStack * S, SElemType * e)
{
	if (S->bottom == S->top)
		return ERROR;
	*e = *--(S->top);

	return OK;
}

void StackTraverse_Sq(SqStack * S, void (* visit)(SElemType))
{
	SElemType * p = S->bottom;

	while (p != S->top)
		visit(*p++);
	printf("\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值