静态数组实现双向栈

// DouDirectionStack.cpp : 定义控制台应用程序的入口点。
//静态数组实现双向栈//拷贝构造函数有点问题
#include "stdafx.h"
#include <iostream>
using namespace std;

/*静态数组实现双向栈:两栈底在数组两端,栈顶相向,迎面增长,栈顶指针指向栈顶元素
栈空:左栈空 top[0]=-1; 右栈空top[1]=maxSize; 全栈空top[0]=-1&&top[1]=maxSize
栈满:栈顶相邻为栈满。top[1]-top[0]=1
栈顶初始值:两栈全空:左栈空 top[0]=-1; 右栈空top[1]=maxSize;
栈顶:总是指向刚刚压入的值
入左栈: data[++top[0]] = NewItem;
入右栈:data[--top[1]] = NewItem
出左栈:newItem = data[top[0]--];
出右栈:newItem = data[top[1]++];
*/
const int MaxSize = 50;
template<class Type>
class DoubleStack
{
public:
	DoubleStack();//默认构造函数
	DoubleStack(const DoubleStack<Type>& otherDoubleStack);//拷贝构造函数
	const DoubleStack<Type>& operator=(const DoubleStack<Type>& otherDoubleStack);//重载赋值运算符
	void initDoubleStack();
	bool isEmptyLStack() const;
	bool isEmptyRStack() const;
	bool IsFullDoubleStack() const;;
	void destoryDoubleStack();
	void pop(int i,Type& popdata);//0指示左栈,1指示右栈
	void push(int i,Type pushdata);
private:
	Type data[MaxSize];
	int top[2];
};

template<class Type>
DoubleStack<Type>::DoubleStack()//默认构造函数
{
	top[0] = -1;
	top[1] = MaxSize;
}

template<class Type>
DoubleStack<Type>::DoubleStack(const DoubleStack<Type>& otherDoubleStack)//拷贝构造函数
{
	top[0] = otherDoubleStack.top[0];
	top[1] = otherDoubleStack.top[1];

	if (!otherDoubleStack.isEmptyLStack() && !otherDoubleStack.isEmptyRStack())
	{		
        memcpy(data,otherDoubleStack.data,MaxSize);
	}
}

template<class Type>
const DoubleStack<Type>& DoubleStack<Type>::operator=(const DoubleStack<Type>& otherDoubleStack)//重载赋值运算符
{
	if (this != &otherDoubleStack)
	{
		if (!otherDoubleStack.isEmptyLStack() && !otherDoubleStack.isEmptyRStack())
		{
			top[0] = otherDoubleStack.top[0];
			top[1] = otherDoubleStack.top[1];
			memcpy(data,otherDoubleStack.data,MaxSize);
		}
	}
	return *this;
}

template<class Type>
void DoubleStack<Type>::initDoubleStack()
{
	top[0] = -1;
	top[1] = MaxSize;
}

template<class Type>
bool DoubleStack<Type>::isEmptyLStack() const
{
	return (top[0] == -1);
}

template<class Type>
bool DoubleStack<Type>::isEmptyRStack() const
{
	return (top[1] == MaxSize);
}

template<class Type>
bool DoubleStack<Type>::IsFullDoubleStack() const
{
	return (top[1] - top[0] == 1);
}

template<class Type>
void DoubleStack<Type>::destoryDoubleStack()
{
	top[0] = -1;
	top[1] = MaxSize;
}

template<class Type>
void DoubleStack<Type>::pop(int i,Type& popdata)//0指示左栈,1指示右栈
{	
	if (i<0 || i>1)
	{
		cout<<"栈号不对!"<<endl;
		return;
	}
	switch(i)
	{
	case 0:
		if (isEmptyLStack())
		{
			cout<<"左栈为空!"<<endl;
		}
		else
		{
			popdata = data[top[0]--];
		}
		break;
	case 1:
		if (isEmptyRStack())
		{
			cout<<"右栈为空!"<<endl;
		}
		else
		{
			popdata = data[top[1]++];
		}
		break;
	}	
}

template<class Type>
void DoubleStack<Type>::push(int i,Type pushdata)
{
	if (i<0 || i>1)
	{
		cout<<"栈号不对!"<<endl;
		return;
	}
	if (IsFullDoubleStack())
	{
		cout<<"双向栈满!"<<endl;
		return;
	}
	else
	{
		switch(i)
		{
		case 0:
			data[++top[0]] = pushdata;
			break;
		case 1:
			data[--top[1]] = pushdata;
			break;
		}	
	}	
}

int _tmain(int argc, _TCHAR* argv[])
{
	DoubleStack<int> stack;
	int a[4] = {1,2,3,4};
	int b[4] = {5,6,7,8};
	for (int i=0; i<4; i++)
	{
		stack.push(0,a[i]);
		stack.push(1,b[i]);
	}
	//DoubleStack<int> stack1 = stack;
	//DoubleStack<int> stack1(stack);
	int popdata = 0;
	cout<<"左栈:";
	for (int j=0; j<4; j++)
	{
		stack.pop(0,popdata);
		cout<<popdata<<" ";
	}
	cout<<endl;
	cout<<"右栈:";
	for (int j=0; j<4; j++)
	{
		stack.pop(1,popdata);
		cout<<popdata<<" ";
	}
	cout<<endl;
    system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值