栈和队列相关面试题(3)

5、一个数组实现两个栈
思路:双向增长法
      两个栈的栈底分别指向数组的两端,栈顶不断向另一个栈的栈底靠近。
过程:
      1、假设以数组首端为栈底的栈叫Stack1,以数组尾端为栈底的栈Stack2,那么当Stack1的栈顶指针大于Stack2的栈顶指针时,需扩容
      2、在实现Push与Pop操作是需要多传一个参数flag以确定是对哪一个栈进行操作


思路2:交替索引法/交叉索引-->缺点:空间的浪费,若是一个栈满,另一个栈还没存东西,此时本来还可以向数组里面存东西,不过要是向
                                   已经栈满的栈插入元素,那么就会因空间不够而开辟空间,这就是空间的浪费
                             优点:不仅可以用一个数组实现两个栈,还可以实现多个栈

 

 

StackAndQueue.h

<strong><span style="font-size:18px;">#pragma once

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

template<class T>
class ArrayForTwoStack
{
public:
	ArrayForTwoStack()
		:_array(NULL)
		,_capacity(0)
		,top1(-2)
		,top2(-1)
	{}

	ArrayForTwoStack(const ArrayForTwoStack& stack)
	{
		_array = new T[stack._capacity];
		_capacity = stack._capacity;
		top1 = stack.top1;
		top2 = stack.top2;
		int count = top1 > top2 ? top1 : top2;

		for (size_t index = 0;index < count; ++index)
		{
			_array[index] = stack._array[index];
		}
	}

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

	void Push(size_t flag,const T& data)
	{
		CheckCapacity();
		//用flag来判断是哪一个栈中存放数据
		if (flag == 1)//flag为1时,下标为偶数存放栈1的数据
		{
			top1 += 2;
			_array[top1] = data;
		} 
		else//当flag不为1时,下标为奇数存放栈2的数据
		{
			top2 += 2;
			_array[top2] = data;
		}
	}

	void Pop(size_t flag)
	{
		if (flag == 1)
		{
			assert(top1 != -2);
			top1 -= 2;
		} 
		else
		{
			assert(top2 != -1);
			top2 -= 2;
		}
	}

	T& Top(size_t flag)
	{
		if (flag == 1)
		{
			assert(top1 != -2);
			return _array[top1];
		} 
		else
		{
			assert(top2 != -1);
			return _array[top2];
		}
	}

	bool Empty(size_t flag)
	{
		if (flag == 1)
		{
			if (top1 == -2)
			{
				return true;
			} 
			else
			{
				return false;
			}
		} 
		else
		{
			if (top2 == -1)
			{
				return true;
			} 
			else
			{
				return false;
			}
		}
	}

	size_t Size(size_t flag)
	{
		if (flag == 1)
		{
			assert(top1 != -2);
			return top1/2 + 1;
		} 
		else
		{
			assert(top2 != -1);
			return (top2 + 1)/2;
		}
	}
protected:
	void CheckCapacity()
	{
		if (((top1 + 2) >= _capacity) || ((top2 + 2) >= _capacity))
		{
			_capacity = _capacity * 2 + 3;
			T* tmp = new T[_capacity];

			int count = top1 > top2 ? top1 : top2;
			for (int index = 0;index <= count; ++index)
			{
				tmp[index] = _array[index];
			}

			delete[] _array;
			_array = tmp;
		}
	}
private:
	T* _array;
	size_t _capacity;
	int top1;//stack1的栈顶下标
	int top2;//stack2的栈顶下标
};
</span></strong>


 

test.cpp

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1
#include "StackAndQueue.h"

void Test5()
{
	//一个数组实现两个栈
	ArrayForTwoStack<int> stack;
	stack.Push(1,1);
	stack.Push(1,2);
	stack.Push(1,3);
	stack.Push(1,4);
	stack.Push(1,5);
	cout<<stack.Top(1)<<endl;
	cout<<stack.Empty(1)<<endl;
	cout<<stack.Size(1)<<endl;

	stack.Pop(1);
	stack.Pop(1);
	cout<<stack.Top(1)<<endl;
	cout<<stack.Empty(1)<<endl;
	cout<<stack.Size(1)<<endl;

	stack.Push(2,6);
	stack.Push(2,7);
	stack.Push(2,8);
	stack.Push(2,9);
	stack.Push(2,10);
	cout<<stack.Top(2)<<endl;
	cout<<stack.Empty(2)<<endl;
	cout<<stack.Size(2)<<endl;

	stack.Pop(2);
	stack.Pop(2);
	cout<<stack.Top(2)<<endl;
	cout<<stack.Empty(2)<<endl;
	cout<<stack.Size(2)<<endl;
}


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

</span></strong>


 

 

 

你可能感兴趣的:(模板,面试题,栈和队列)