现在的位置: 首页 > 综合 > 正文

Q3.1

2018年04月29日 ⁄ 综合 ⁄ 共 901字 ⁄ 字号 评论关闭
#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;
}

【上篇】
【下篇】

抱歉!评论已关闭.