用双端队列实现双端栈的底层实现,附有例子~

#include
#include 
using namespace std;

enum stackNumber
{
	one, two
};

template
class dualStack
{
public:
	dualStack();
	void push(const T & item, stackNumber n );
	void pop(stackNumber n);
	const T & top(stackNumber n) const;
	bool empty(stackNumber n) const;
	int size(stackNumber n) const;
private:
	deque dualStackElements;
	int count1, count2;
};

template
dualStack::dualStack(){ count1 = 0; count2 = 0; dualStackElements.push_front(NULL); }


template
void dualStack::push(const T & item, stackNumber n)
{
	if (n == one)
	{
		dualStackElements.push_front(item);
		count1++;
	}

	if (n == two)
	{
		dualStackElements.push_back(item);
		count2++;
	}
}

template
void dualStack::pop(stackNumber n)
{
	if (n == one)
	{
		if (count1 == 0)
		{
			cout << "栈已空\n";
			return;
		}
		dualStackElements.pop_front();
		count1--;

	}

	if (n == two)
	{
		if (count2 == 0)
		{
			cout << "栈已空\n";
			return;
		}
		dualStackElements.pop_back();
		count2--;

	}

}


template
const T & dualStack::top(stackNumber n) const
{
	const T temp = NULL;
	if (n == one)
	{
		if (this->empty(one))
		{
			cout << "栈已空";
			return temp;
		}

		return dualStackElements.front();


	}

	if (n == two)
	{
		if (this->empty(two))
		{
			cout << "栈已空";
			return temp;
		}

	return dualStackElements.back();
		

	}

}


template
bool dualStack::empty(stackNumber n) const
{
	if (n == one)
		return dualStackElements.front()==NULL;

	if (n == two)
		return dualStackElements.back()==NULL;
}

template
int dualStack::size(stackNumber n) const
{
	if (n == one)
		return count1;

	if (n == two)
		return count2;
}
//以下测试main
#include"dualstk.h"
#include
#include


using namespace std;
 
int main()
{
    dualStack stk;
    int i = 1;
    int num;
    srand(unsigned int(time(0)));
    cout << "随机产生的从0~99的数字20个:" << endl;
    for (; i <= 20; i++)
    {
        num = rand() % 100;
        cout << num << " ";
        if (num % 2)
        {
            stk.push(num, one);
            continue;
        }
        stk.push(num, two);
    }
    cout << endl;
    system("pause");
    while (!stk.empty(one))
    {
        cout << stk.top(one) << " ";
        stk.pop(one);
    }
    cout << endl;
    while (!stk.empty(two))
    {
        cout << stk.top(two) << " ";
        stk.pop(two);
    }
    cout << endl;
    cout << stk.top(one);
    cout << endl;
    system("pause");


    return 0;
}
//其实都很简单,并没有使用双端队列的队列的性质,从两端看成两个栈,进行的操作。
//初始化时,push了NULL作为empty的标志。
//缺点:在top函数中,由于返回类型必须是const T & ,必须有返回值,而NULL的返回值对于Int就是0,肯定会被输出,甚为讨厌,原本想返回new T(),但是返回值类型对不//上,请大家指教如何消灭这个0,或者如何返回垃圾值!
 
  
//以下是实现的另一个版本,差别就在构造是并不添加NULL,是否empty仅用count1,count2是否为0作为判断,不知道哪个版本相对好些,请大家指教~~
//仅仅修改了构造函数以及top函数
#include
#include 
using namespace std;


enum stackNumber
{
	one, two
};


template
class dualStack
{
public:
	dualStack();
	void push(const T & item, stackNumber n );
	void pop(stackNumber n);
	const T & top(stackNumber n) const;
	bool empty(stackNumber n) const;
	int size(stackNumber n) const;
private:
	deque dualStackElements;
	int count1, count2;
};


template
dualStack::dualStack(){ count1 = 0; count2 = 0;  }




template
void dualStack::push(const T & item, stackNumber n)
{
	if (n == one)
	{
		dualStackElements.push_front(item);
		count1++;
	}


	if (n == two)
	{
		dualStackElements.push_back(item);
		count2++;
	}
}


template
void dualStack::pop(stackNumber n)
{
	if (n == one)
	{
		if (count1 == 0)
		{
			cout << "栈已空\n";
			return;
		}
		dualStackElements.pop_front();
		count1--;


	}


	if (n == two)
	{
		if (count2 == 0)
		{
			cout << "栈已空\n";
			return;
		}
		dualStackElements.pop_back();
		count2--;


	}


}




template
const T & dualStack::top(stackNumber n) const
{
	const T temp = NULL;
	if (n == one)
	{
		if (this->empty(one))
		{
			cout << "栈已空";
			return temp;
		}


		return dualStackElements.front();




	}


	if (n == two)
	{
		if (this->empty(two))
		{
			cout << "栈已空";
			return temp;
		}


	return dualStackElements.back();
		


	}


}




template
bool dualStack::empty(stackNumber n) const
{
	if (n == one)
		return count1==0;


	if (n == two)
		return count2==0;
}


template
int dualStack::size(stackNumber n) const
{
	if (n == one)
		return count1;


	if (n == two)
		return count2;
}

你可能感兴趣的:(底层实现)