Q3.1

#include <iostream>
using namespace std;

#define STACKSIZE 100
#define EMPTY     65535

typedef struct Node
{
	int data;
	int prefix;
}Node;

int fix = -1;
int pt1 = -1, pt2 = -1, pt3 = -1;

void push(Node* S, int data, int type)
{
	fix++;
	S[fix].data = data;
	
	if (type == 1)
	{
		S[fix].prefix = pt1;
		pt1 = fix;
	}
	else if (type == 2)
	{
		S[fix].prefix = pt2;
		pt2 = fix;
	}
	else
	{
		S[fix].prefix = pt3;
		pt3 = fix;
	}
}

int pop(Node* S, int type)
{
	int re;

	if (type == 1)
	{
		if (pt1 < -1)
		{
			return EMPTY;
		}

		re = S[pt1].data;
		pt1 = S[pt1].prefix;
	}
	else if (type == 2)
	{
		if (pt2 < -1)
		{
			return EMPTY;
		}

		re = S[pt2].data;
		pt2 = S[pt2].prefix;
	}
	else
	{
		if (pt1 < -1)
		{
			return EMPTY;
		}

		re = S[pt3].data;
		pt3 = S[pt3].prefix;
	}
	return re;
}

int main(void)
{
	Node Stack[STACKSIZE];

	for (int i = 0; i < STACKSIZE; ++i)
	{
		Stack->prefix = -1;
	}
	

	push(Stack, 3, 3);
	push(Stack, 1, 1);
	push(Stack, 2, 2);

	push(Stack, 5, 2);
	push(Stack, 4, 1);
	push(Stack, 6, 3);

	cout<<pop(Stack, 1)<<endl;
	cout<<pop(Stack, 2)<<endl;
	cout<<pop(Stack, 3)<<endl;

	cout<<pop(Stack, 1)<<endl;
	cout<<pop(Stack, 2)<<endl;
	cout<<pop(Stack, 3)<<endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值