栈和队列面试题(四)

5.一个数组实现两个栈

有两种方法:

(1),数组单号下标为一个栈,双号下标为一个栈

(2)开辟一个数组,从0号下标往右为1号栈,从N号下标网左为2号栈

下面先来实现第一种方法:

#include <iostream>
#include <assert.h>
using namespace std;

template<class T>
class DoubleStack2
{
public:
	DoubleStack2()
		:_array(NULL)
		, _capacity(0)
		, _top1(-2)
		, _top2(-1)
	{}

	DoubleStack2(const DoubleStack2& dstack)
	{
		_array = new T[dstack._capacity];
		_capacity = dstack._capacity;
		_top1 = dstack._top1;
		_top2 = dstack._yop2;
		int count = _top1 > _top2 ? _top1 : _top2;
		int index = 0;
		for (index = 0; index < count;++index)
		{
			_array[index] = dstack._array[index];
		}
	}

	~DoubleStack2()
	{
		if (_array)
		{
			delete[] _array;
			_array = NULL;
		}
	}
	void Push(int flag,T data)
	{
		if (flag != 1 && flag != 2)
		{
			return;
		}
		else
		{
			_CheckCapacity();
			if (flag == 1)
			{
				_top1 += 2;
				_array[_top1] = data;
			}
			else if (flag == 2)
			{
				_top2 += 2;
				_array[_top2] = data;
			}
		}
	}

	void Pop(int flag)
	{
		if (flag != 1 && flag != 2)
		{
			return;
		}
		else if (flag == 1)
		{
			if (_top1 == -2)
			{
				printf("the stack1 is empty!\n");
				return;
			}
			else
			{
				_top1 -= 2;
			}
		}
		else if (flag == 2)
		{
			if (_top2 == -2)
			{
				printf("the stack2 is empty!\n");
				return;
			}
			else
			{
				_top2 -= 2;
			}
		}

	}

	size_t Size(int flag)
	{
		//assert(flag != 1 && flag != 2);
		if (flag == 1)
		{
			return (_top1 / 2 + 1);
		}
		else
		{
			return (_top2 / 2 + 1);
		}
	}

	bool Empty(int flag)
	{
		if (flag != 1 && flag != 2)
		{
			return false;
		}
		else if (flag == 1)
		{
			if (_top1 == -2)
				return true;
		}
		else if (flag == 2)
		{
			if (_top2 == -1)
			{
				return true;
			}
		}
		return false;
	}

	T& Top(int flag)
	{
		//assert(flag != 1 && flag != 2);
		if (flag == 1)
		{
			return _array[_top1];
		}
		else if (flag == 2)
		{
			return _array[_top2];
		}
	}
protected:
	void _CheckCapacity()
	{
		int count = _top1 > _top2 ? _top1 : _top2;


		if (count < 0||_capacity == count)
		{
			size_t index = 0;
			_capacity = 2 * _capacity + 3;
			T* _tmp = new T[_capacity];
			if (_tmp && count>=0)
			{
				for (index = 0; index <= count; ++index)
				{
					_tmp[index] = _array[index];
				}
			}
			delete[] _array;
			_array = _tmp;
		}
		
	}
private:
	T* _array;
	T _capacity;
	T _top1;
	T _top2;
};

Test.cpp

void Test()
{
	DoubleStack2<int> d;
	d.Push(1, 0);
	d.Push(1, 1);
	d.Push(1, 2);
	d.Push(1, 3);
	d.Push(1, 4);
	
	d.Push(2, 0);
	d.Push(2, 1);
	d.Push(2, 2);
	d.Push(2, 3);
	
	cout << d.Top(1) << endl;
	cout << d.Top(2) << endl;

	cout << d.Size(1) << endl;
	cout << d.Size(2) << endl;


	d.Pop(1);
	d.Pop(1);
	d.Pop(1);
	d.Pop(1);
	
	d.Pop(2);
	d.Pop(2);
	d.Pop(2);
	cout << d.Empty(1) <<endl;
	cout << d.Empty(2) << endl;


}


第二种方法实现:

#define N 15
template<class T>
class DoubleStack
{
public:
	DoubleStack(size_t size = N)
		:_array(new T[N])
		, _capacity(N)
		, _top1(0)
		, _top2(N)
	{}

	DoubleStack(const DoubleStack& dstack)
	{
		_array = new T[dstack._capacity];
		_capacity = dstack._capacity;
		_top1 = dstack._top1;
		_top2 = dstack._top2;

		int count1 = 0;
		for (; count1 < _top1; ++count1)
		{
			_array[count1] = dstack._array[count1];
		}
		int count2 = _capacity - 1;
		for (count2 = _capacity - 1; count2>_top2; --count)
		{
			_array[count2] = dstack._array[count2];
		}

	}

	~DoubleStack()
	{
		if (_array)
		{
			delete[] _array;
			_array = NULL;
		}
	}

	void Push(int flag, const T data)
	{
		if (flag != 1 && flag != 2)
		{
			return;
		}
		if (flag == 1)
		{
			if (_top1 == _top2 - 1)
			{
				printf("the stack of one if full!\n");
				return;
			}
			else
			{
				_array[_top1++] = data;
			}
		}
		else if (flag == 2)
		{
			if (_top1 == _top2 - 1)
			{
				printf("the stack of two is full!\n");
				return;
			}
			else
			{
				_array[_top2--] = data;
			}
		}
	}


		void Pop(size_t flag)
		{
			if (flag != 1 && flag != 2)
			{
				return;
			}
			else
			{
				if (flag == 1)
				{
					--_top1;
				}
				else if (flag == 2)
				{
					++_top2;
				}
			}
		}

		size_t Size(size_t flag)
		{
			assert(flag != 1 && flag != 2);
			if (flag == 1)
			{
				return _top1;
			}
			else if (flag == 2)
			{
				return _capacity - _top2;
			}
		}
		size_t Top(size_t flag)
		{
			assert(flag != 1 && flag != 2);
			if (flag == 1)
				return _array[_top1];
			else if (flag == 2)
				return _array[_top2];
		}

		size_t Empty(size_t flag)
		{
			assert(flag != 1 && flag != 2);
			if (flag == 1)
				return _top1 == 0;
			else if (flag == 2)
				return _top2 == N;
		}

	private:
		T* _array;
		T _capacity;
		T _top1;
		T _top2;
	};

Test.cpp

void Test2()
	{
		DoubleStack<int> d;
		d.Push(1, 0);
		d.Push(1, 1);
		d.Push(1, 2);
		d.Push(1, 3);
		d.Push(1, 4);

		d.Push(2, 0);
		d.Push(2, 1);
		d.Push(2, 2);
		d.Push(2, 3);

		d.Pop(1);
		d.Pop(1);
		d.Pop(1);
		d.Pop(1);

		d.Pop(2);
		d.Pop(2);
		d.Pop(2);

	}

	int main()
	{
		Test();
		system("pause");
		return 0;
	}

两种方法都存在浪费空间的问题,如果还有什么好的方法,大家可以一起讨论哦!






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值