棧和隊列面試題(四)

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;
	}

兩種方法都存在浪費空間的問題,如果還有什麼好的方法,大家可以一起討論哦!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章