【数据结构】C语言实现共享栈

本文介绍了一种共享栈的数据结构实现方法,通过在一个连续的内存空间内创建两个栈,并分别从两端向中间生长的方式实现。文章提供了完整的源代码,包括初始化、压栈、弹栈、获取栈顶元素和销毁栈等操作,并通过几个测试用例验证了共享栈的功能。
摘要由CSDN通过智能技术生成


共享栈是两个栈在一个顺序的存储空间中。两个栈的栈底分别是存储空间的首尾地址。

代码如下:

stack.h

#pragma once

#include<stdio.h>


#define STACKMAXSIZE 1000
typedef char StackType;

/*定义一个共享栈的结构*/
typedef struct {
	StackType data[STACKMAXSIZE];
	/*top1和top2分别是两个栈的栈顶*/
	int top1;
	int top2;
}ShareStack;

/*初始化共享栈*/
void ShareStackInit(ShareStack* stack);
/*往栈里压入元素*/
void ShareStackPush(ShareStack* stack, StackType value, int StackNum);
/*出栈一个元素*/
void ShareStackPop(ShareStack* stack, int StackNum);
/*打印栈*/
void PrintStack(ShareStack* stack, int StackNum, char* msg);
/*取栈顶元素*/
StackType ShareStackGetTopValue(ShareStack* stack, int StackNUm);
/*销毁栈*/
void ShareStackDestory(ShareStack* stack, int StackNum);


stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"ShareStack.h"

/*初始化共享栈*/
void ShareStackInit(ShareStack* stack) {

	if (stack == NULL) {
		return;
	}
	stack->top1 = -1;
	stack->top2 = STACKMAXSIZE;
}

/*往栈里压入元素*/
void ShareStackPush(ShareStack* stack, StackType value, int StackNum) {

	if (stack == NULL) {
		return;
	}
	/*判断栈满*/
	if (stack->top1 + 1 == stack->top2) {
		printf("栈满了");
		return;
	}
	switch (StackNum) {
	case 1:
		/*往左边的栈压入元素*/
		++stack->top1;
		stack->data[stack->top1] = value;
		break;
	case 2:
		--stack->top2;
		stack->data[stack->top2] = value;
		break;
	default:
		exit(0);
		break;
	}
}

/*出栈一个元素*/
void ShareStackPop(ShareStack* stack, int StackNum) {

	if (stack == NULL) {
		return;
	}

	switch (StackNum) {
	case 1:
		if (stack->top1 == -1) {
			printf("栈1为空");
			return;
		}
		--stack->top1;
		break;
	case 2:
		if (stack->top2 == STACKMAXSIZE) {
			printf("栈2为空");
			return;
		}
		++stack->top2;
		break;
	default:
		break;
	}
}


/*打印栈*/
void PrintStack(ShareStack* stack, int StackNum, char* msg) {

	if (stack == NULL) {
		return;
	}
	switch (StackNum) {
	case 1:

		if (stack->top1 == -1) {
			printf("栈为空\n");
			return;
		}
		printf("%s\n", msg);
		printf("[top]->");
		int index = stack->top1;

		while (index != -1) {
			printf("[%c]->", stack->data[index]);
			--index;
		}
		printf("[bottom]\n\n");

		break;
	case 2:

		if (stack->top2 == STACKMAXSIZE) {
			printf("栈为空\n");
			return;
		}
		printf("%s\n", msg);
		printf("[top]->");
		int index2 = stack->top2;

		while (index2 != STACKMAXSIZE) {
			printf("[%c]->", stack->data[index2]);
			++index2;
		}
		printf("[bottom]\n\n");
		break;
	default:
		break;
	}
}

/*取栈顶元素*/
StackType ShareStackGetTopValue(ShareStack* stack, int StackNUm) {

	if (stack == NULL) {
		return;
	}
	switch (StackNUm) {
	case 1:
		if (stack->top1 == -1) {
			printf("栈1为空");
			return;
		}
		return stack->data[stack->top1];
		break;
	case 2:
		if (stack->top2 == STACKMAXSIZE) {
			printf("栈2为空");
			return;
		}
		return stack->data[stack->top2];
		break;
	default:
		break;
	}
}

/*销毁栈*/
void ShareStackDestory(ShareStack* stack, int StackNum) {

	if (stack == NULL) {
		return;
	}
	switch (StackNum) {
	case 1:
		stack->top1 = -1;
		break;
	case 2:
		stack->top2 = STACKMAXSIZE;
		break;
	case 3:
		stack->top1 = -1;
		stack->top2 = STACKMAXSIZE;
		break;
	default:
		break;
	}
}


test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"ShareStack.h"

#define TESTHEAD printf("----------------%s-----------------\n",__FUNCTION__);

void TestPush() {
	ShareStack stack;
	TESTHEAD;
	ShareStackInit(&stack);
	/*开始测试push函数*/
	ShareStackPush(&stack, 'a', 1);
	ShareStackPush(&stack, 'b', 1);
	ShareStackPush(&stack, 'c', 1);
	ShareStackPush(&stack, 'd', 1);
	ShareStackPush(&stack, 'e', 1);

	ShareStackPush(&stack, 'e', 2);
	ShareStackPush(&stack, 'd', 2);
	ShareStackPush(&stack, 'c', 2);
	ShareStackPush(&stack, 'b', 2);
	ShareStackPush(&stack, 'a', 2);

	PrintStack(&stack, 1, "压栈五个元素");
	PrintStack(&stack, 2, "压栈五个元素");
}

void TestPop() {
	ShareStack stack;
	TESTHEAD;
	ShareStackInit(&stack);
	/*开始测试push函数*/
	ShareStackPush(&stack, 'a', 1);
	ShareStackPush(&stack, 'b', 1);
	ShareStackPush(&stack, 'c', 1);
	ShareStackPush(&stack, 'd', 1);
	ShareStackPush(&stack, 'e', 1);

	ShareStackPush(&stack, 'e', 2);
	ShareStackPush(&stack, 'd', 2);
	ShareStackPush(&stack, 'c', 2);
	ShareStackPush(&stack, 'b', 2);
	ShareStackPush(&stack, 'a', 2);

	PrintStack(&stack, 1, "压栈五个元素");
	PrintStack(&stack, 2, "压栈五个元素");
	
	ShareStackPop(&stack, 1);
	ShareStackPop(&stack, 1);

	ShareStackPop(&stack, 2);
	ShareStackPop(&stack, 2);

	PrintStack(&stack, 1, "出栈两个元素");
	PrintStack(&stack, 2, "出栈两个元素");
}

void TestGetTop() {
	ShareStack stack;
	TESTHEAD;
	ShareStackInit(&stack);
	/*开始测试push函数*/
	ShareStackPush(&stack, 'a', 1);
	ShareStackPush(&stack, 'b', 1);
	ShareStackPush(&stack, 'c', 1);
	ShareStackPush(&stack, 'd', 1);
	ShareStackPush(&stack, 'e', 1);

	ShareStackPush(&stack, 'e', 2);
	ShareStackPush(&stack, 'd', 2);
	ShareStackPush(&stack, 'c', 2);
	ShareStackPush(&stack, 'b', 2);
	ShareStackPush(&stack, 'a', 2);

	PrintStack(&stack, 1, "压栈五个元素");
	PrintStack(&stack, 2, "压栈五个元素");

	StackType value1 = ShareStackGetTopValue(&stack, 1);
	StackType value2 = ShareStackGetTopValue(&stack, 2);

	printf("expect e, actual: %c\n", value1);
	printf("expect a, actual: %c\n", value2);

}

void TestDestory() {
	ShareStack stack;
	TESTHEAD;
	ShareStackInit(&stack);
	/*开始测试push函数*/
	ShareStackPush(&stack, 'a', 1);
	ShareStackPush(&stack, 'b', 1);
	ShareStackPush(&stack, 'c', 1);
	ShareStackPush(&stack, 'd', 1);
	ShareStackPush(&stack, 'e', 1);

	ShareStackPush(&stack, 'e', 2);
	ShareStackPush(&stack, 'd', 2);
	ShareStackPush(&stack, 'c', 2);
	ShareStackPush(&stack, 'b', 2);
	ShareStackPush(&stack, 'a', 2);

	PrintStack(&stack, 1, "压栈五个元素");
	PrintStack(&stack, 2, "压栈五个元素");

	ShareStackDestory(&stack, 1);
	PrintStack(&stack, 1, "销毁栈1");

	ShareStackDestory(&stack, 2);
	PrintStack(&stack, 2, "销毁栈1");
}

int main() {
	TestPush();
	TestPop();
	TestGetTop();
	TestDestory();
	system("pause");
	return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值