【数据结构】共享栈

栈的实现:https://blog.csdn.net/weixin_41892460/article/details/82973851

SharedStack.h

#ifndef __SHAREDSTACK_H__
#define __SHAREDSTACK_H__



#include<string.h>
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
#include<stdlib.h>
#include<Windows.h>


typedef int DataType;
#define MAX 10

typedef struct SharedStack
{
	DataType a[MAX];
	int top1;
	int top2;
}SharedStack;

void SharedStackInit(SharedStack* s);//
void SharedStackPush(SharedStack* s, int which, DataType x);//
void SharedStackPop(SharedStack* s, int which);//
size_t SharedStackSize(SharedStack* s, int which);//
int SharedStackEmpty(SharedStack* s, int which);
void SharedStackDestory(SharedStack* s, int which);
DataType SharedStackTop(SharedStack* s, int which);//
void text();//

#endif

SharedStack.c

#include "SharedStack.h"


void SharedStackInit(SharedStack* s)
{
	assert(s);
	s->top1 = 0;//偶数
	s->top2 = 1;//奇数
}
void SharedStackDestory(SharedStack* s, int which)
{
	assert(1 == which || 2 == which);
	if (1 == which)
	{
		assert(s);
		s->top1 = 0;
		free(s);
		s = NULL;
	}
	else if (2 == which)
	{
		assert(s);
		s->top2 = 0;
		free(s);
		s = NULL;
	}
}
void SharedStackPush(SharedStack* s, int which, DataType x)
{
	assert(1 == which || 2 == which);
	if (1 == which)
	{
		assert(s);
		if (s->top1 < MAX)
		{
			s->a[s->top1] = x;
			s->top1 += 2;
		}
	}
	else if (2 == which)
	{
		assert(s);
		if (s->top2 < MAX)
		{
			s->a[s->top2] = x;
			s->top2 += 2;
		}
	}
}
void SharedStackPop(SharedStack* s, int which)
{
	assert(1 == which || 2 == which);
	if (1 == which)
	{
		assert(s);
		assert(s->top1 > 0);
		s->top1 -= 2;
	}
	else if (2 == which)
	{
		assert(s);
		assert(s->top2 > 0);
		s->top2 -= 2;
	}
}
DataType SharedStackTop(SharedStack* s, int which)
{
	assert(1 == which || 2 == which);
	if (1 == which)
	{
		assert(s);
		assert(s->top1 > 0);
		return s->a[s->top1 - 2];
	}
	else if (2 == which)
	{
		assert(s);
		assert(s->top2 > 0);
		return s->a[s->top2 - 2];
	}
}
size_t SharedStackSize(SharedStack* s, int which)
{
	assert(s);
	assert(1 == which || 2 == which);
	return which == 1 ? (s->top1) / 2 : (s->top2 - 1) / 2;
}
int SharedStackEmpty(SharedStack* s, int which)
{
	assert(1 == which || 2 == which);
	if (1 == which)
	{
		assert(s);
		return s->top1 == 0 ? 0 : 1;
	}
	else if (2 == which)
	{
		assert(s);
		return  s->top2 == 1 ? 0 : 1;
	}

}

Test.c

#include "SharedStack.h"


void text()
{
	SharedStack s;
	SharedStackInit(&s);

	//SharedStackPush(&s, 1, 1);
	//SharedStackPush(&s, 1, 2);
	//SharedStackPush(&s, 1, 3);
	//SharedStackPush(&s, 1, 4);

	SharedStackPush(&s, 2, 7);
	SharedStackPush(&s, 2, 8);
	SharedStackPush(&s, 2, 9);
	SharedStackPush(&s, 2, 6);



	/*printf("%d\n", SharedStackEmpty(&s, 1));*/
	printf("%d\n", SharedStackEmpty(&s, 2));

	/* SharedStackDestory(&s, 2);*/

	//printf("%d\n", SharedStackTop(&s, 1));
	//printf("%d\n", SharedStackTop(&s, 2));


	//printf("%d\n", SharedStackSize(&s, 1));
	//printf("%d\n", SharedStackSize(&s, 2));
	//while (SharedStackEmpty(&s, 1) != 0)
	//{
	//	printf("%d ", SharedStackTop(&s,1));
	//	SharedStackPop(&s, 1);
	//}
	//printf("\n");


	while (SharedStackEmpty(&s, 2) != 0)
	{
		printf("%d ", SharedStackTop(&s, 2));
		SharedStackPop(&s, 2);
	}
	printf("\n");
}
int main()
{
	text();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值