doublestack.cpp

来自「数据结构测试程序」· C++ 代码 · 共 170 行

CPP
170
字号
// doublestack.cpp: implementation of the doublestack class.
//
//////////////////////////////////////////////////////////////////////

#include "doublestack.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

doublestack::doublestack()
{

}

doublestack::~doublestack()
{

}

void doublestack::initiate()
{

	top[0]=-1;
	top[1]=max;
}

int doublestack::empty(int i)
{

	if(i<0 || i>2)
	{
		printf("Error 'i'!\n");
		return 2;
	}
	else if(i==0)
	{
		if(top[0]==-1) return 1;
		else return 0;
	}
	else if(i==1)
	{
		if(top[1]==max) return 1;
		else return 0;
	}
	else
	{
		if(top[0]==-1 && top[1]==max) return 1;
		else return 0;
	}
}

int doublestack::push(elemt elm, int i)
{
	int inc;
	if((top[1])-(top[0])==1)
	{
		printf("Stack is full already!\n");
		return 0;
	}
	else if(i<0 || i>1)
	{
		printf("Error 'i'!\n");
		return 0;
	}
	else
	{
		if(i==0)
		{
			top[0]++;
			data[top[0]]=elm;
			return 1;
		}
		else if(i==1)
		{
			top[1]--;
			data[top[1]]=elm;
			return 1;
		}
	}
}

int doublestack::pop(elemt *elm, int i)
{
	int inc;
	if(i<0 || i>1)
	{
		printf("Error 'i'!\n");
		return 0;
	}
	else if(i==0 && top[0]==-1 || i==1 && top[1]==max)
	{
		printf("Stack empty!\n");
		return 0;
	}
	else
	{
		if(i==0) inc=1;
		else inc=-1;
		top[i]-=inc;
		*elm=data[top[i]+inc];
		return 1;
	}
}

elemt doublestack::gettop(int i)
{
	if(i<0 || i>1)
	{
		printf("Error 'i'!\n");
		return 0;
	}
	else if(i==0 && top[0]==-1 || i==1 && top[1]==max)
	{
		printf("Stack empty!\n");
		return 0;
	}
	else
	{
		return data[top[i]];
	}
}

int doublestack::clear(int i)
{
		if(i<0 || i>2)
	{
		printf("Error 'i'!\n");
		return 0;
	}
	else if(i==0)
	{
		top[0]=-1;
		return 1;
	}
	else if(i==1)
	{
		top[1]=max;
		return 1;
	}
	else
	{
		top[0]=-1;
		top[1]=max;
		return 1;
	}

}

int doublestack::size(int i)
{
	if(i<0 || i>2)
	{
		printf("Error 'i'!\n");
		return 0;
	}
	else if(i==0)
	{
		return top[0]+1;
	}
	else if(i==1)
	{
		return max-top[1];
	}
	else
	{
		return top[0]+1+max-top[1];
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?