栈的顺序存储结构——c语言描述

栈的顺序存储结构——c语言描述

1栈的定义

​ 先进后出的数据结构

2栈的顺序存储结构

**当栈为空栈时,**top = -1

代码:

#define SUCCESS			1
#define ERROR			0
#define MAXSIZE			5

typedef int ElemType;
typedef int LIST_STATUS;
typedef struct _SQ_STACK {
	ElemType Data[MAXSIZE];
	int Top;
}SQ_STACK;

2.1 操作顺序栈

代码:

void OperateSqStack(void) {
	LIST_STATUS Status;
	SQ_STACK *SqStack = (SQ_STACK *)malloc(sizeof(SQ_STACK));
	int CreateNum = 3;
	int PushData = 3;
	int *PopData = (int *)malloc(sizeof(int));
	
	Status = CreateSQStack(SqStack, CreateNum);
	if (SUCCESS == Status) {
		printf("CreateSQStack succeed!\n");
	}
	else {
		printf("CreateSQStack failed!\n");
	}

	PrintSQStack(SqStack);

	/*
	Status = PushSQStack(SqStack, PushData);
	if (SUCCESS == Status) {
		printf("PushSQStack succeed!\n");
	}
	else {
		printf("PushSQStack failed!\n");
	}
	*/

	PopSQStack(SqStack, PopData);
	if (SUCCESS == Status) {
		printf("PushSQStack succeed!\n");
	}
	else {
		printf("PushSQStack failed!\n");
	}

	PrintSQStack(SqStack);
}

2.2 创建顺序栈

LIST_STATUS CreateSQStack(SQ_STACK *SqStack, int CreateNum)

​ 创建个数为CreateNum的栈

代码:

LIST_STATUS CreateSQStack(SQ_STACK *SqStack, int CreateNum) {
	int TraIndex;

	printf("CreateSQStack start\n");

	if (NULL == SqStack || CreateNum > MAXSIZE) {
		return ERROR;
	}

	for (TraIndex = 0; TraIndex < CreateNum; ++TraIndex) {
		SqStack->Data[TraIndex] = TraIndex;
	}

	SqStack->Top = CreateNum - 1;

	printf("CreateSQStack end\n");
	return SUCCESS;
}

结果:

CreateSQStack start

CreateSQStack end

CreateSQStack succeed!

CreateSQStack start

TraStack->Top = 2

TraStack->Data[TraIndex] = 0

TraStack->Data[TraIndex] = 1

TraStack->Data[TraIndex] = 2

TraStack->Data[TraIndex] = -842150451

TraStack->Data[TraIndex] = -842150451

CreateSQStack end

2.3 打印顺序栈

void PrintSQStack(SQ_STACK * const SqStack)

代码:

void PrintSQStack(SQ_STACK * const SqStack) {
	SQ_STACK *TraStack = SqStack;
	int TraIndex;

	printf("CreateSQStack start\n");

	printf("TraStack->Top = %d\n", TraStack->Top);
	for (TraIndex = 0; TraIndex < MAXSIZE; ++TraIndex) {
		printf("TraStack->Data[TraIndex] = %d\n", TraStack->Data[TraIndex]);
	}

	printf("CreateSQStack end\n");
}

2.4 进栈

​ 栈顶增加一个元素

代码:

LIST_STATUS PushSQStack(SQ_STACK *SqStack, int PushData) {	
	printf("PushSQStack start\n");

	if (NULL == SqStack || SqStack->Top == MAXSIZE - 1) {
		return ERROR;
	}

	SqStack->Top++;
	SqStack->Data[SqStack->Top] = PushData;

	printf("PushSQStack end\n\n");

	return SUCCESS;
}

结果:

CreateSQStack start

CreateSQStack end

CreateSQStack succeed!

CreateSQStack start

TraStack->Top = 2

TraStack->Data[TraIndex] = 0

TraStack->Data[TraIndex] = 1

TraStack->Data[TraIndex] = 2

TraStack->Data[TraIndex] = -842150451

TraStack->Data[TraIndex] = -842150451

CreateSQStack end

PushSQStack start

PushSQStack end

PushSQStack succeed!

CreateSQStack start

TraStack->Top = 3

TraStack->Data[TraIndex] = 0

TraStack->Data[TraIndex] = 1

TraStack->Data[TraIndex] = 2

TraStack->Data[TraIndex] = 3

TraStack->Data[TraIndex] = -842150451

2.5出栈

代码:

LIST_STATUS PopSQStack(SQ_STACK *SqStack, int *PopData) {
	printf("PopSQStack start\n");

	if (NULL == SqStack || SqStack->Top == -1) {
		return ERROR;	
	}

	*PopData = SqStack->Data[SqStack->Top];
	SqStack->Top--;

	printf("PopSQStack end\n");
}

结果:

CreateSQStack start

CreateSQStack end

CreateSQStack succeed!

CreateSQStack start

TraStack->Top = 2

TraStack->Data[TraIndex] = 0

TraStack->Data[TraIndex] = 1

TraStack->Data[TraIndex] = 2

TraStack->Data[TraIndex] = -842150451

TraStack->Data[TraIndex] = -842150451

CreateSQStack end

PopSQStack start

PopSQStack end

PushSQStack succeed!

CreateSQStack start

TraStack->Top = 1

TraStack->Data[TraIndex] = 0

TraStack->Data[TraIndex] = 1

TraStack->Data[TraIndex] = 2

TraStack->Data[TraIndex] = -842150451

TraStack->Data[TraIndex] = -842150451

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值