Data structure: two stacks to realize a queue (and two queues to realize a stack)

Mutual realization of data structure stack, queue stack and queue

1. Two stacks implement a queue

#include<iostream>
#include<queue>
#include<assert.h>
using namespace std;
class Stack
{
public:
	//1.添加元素
	void push(int n);
	//2.移除栈顶的元素
	void pop();
	//3.判断栈是否为空
	bool empty();
	//4.返回栈顶的元素
	int top();
	//5.返回栈中元素的个数
	int size();
private:
	queue<int> queue1;//要用的队列
	queue<int> queue2;
};
//1.添加元素
void Stack::push(int n)
{
	//直接加入到队列
	queue1.push(n);
}
//2.移除栈顶的元素
void Stack::pop()
{
	//断言,判断栈是否为空
	assert(!this->empty());
	//1.把队列1里面的元素放到队列2里面去,取得队列1里面最后一个元素
	while (queue1.size()>1)
	{
		queue2.push(queue1.front());//用队列1得队头给队列2赋值,队列1只保留一个元素
		queue1.pop();
	}
	//2.删除队列1中留下得元素
	queue1.pop();
	//3.再将队列2中元素放回队列1中
	while (!queue2.empty())
	{
		queue1.push(queue2.front());
		queue2.pop();
	}

}
//3.判定栈是否为空
bool Stack::empty()
{	//空则返回true,非空则返回false
	if (queue1.empty())
	{
		return true;
	}
	return false;
}
//4.返回栈中元素的个数
int Stack::size()
{
	return queue1.size();
}
//5.返回栈顶的元素
int Stack::top()
{
	assert(!this->empty());
	return queue1.back();
}

int main()
{
	Stack s;
	for (int i = 0; i < 10; i++)
	{
		s.push(i);
	}
	cout << "栈是否为空?0为非空,1为空:" << s.empty() << endl;
	cout <<"栈里元素得个数为:"<< s.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		cout << s.top() << endl;
		s.pop();
	}
	cout << "栈是否为空?0为非空,1为空:" << s.empty() << endl;

	return 0;
}

2. Two queues implement a stack

#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;
class Queue
{
public:
	//1.判定队列是否为空
	bool empty();
	//2.返回队列中第一个元素(队头)
	int front();
	//3.返回队列中最后一个元素(队尾)
	int back();
	//4.删除队列中得第一个元素
	void pop();
	//5.返回队列中元素的个数
	int size();
	//6.在队尾后面添加一个元素
	void push(int n);

private:
	stack<int> stack1;//用于存储数据
	stack<int> stack2;//用于输出数据时把stack1得数据存放

};
//1.判定队列是否为空
bool Queue::empty()
{
	//如果为空则返回true,如果不为空则返回false
	if (stack1.empty())
	{
		return true;
	}
	return false;
}
//2.返回队列中第一个元素(队头)
int Queue::front()
{
	//断言:判断此时队列是否为空
	assert(!this->empty());
	//这里需要用到stack2,因为我们要拿到stack1栈底的元素
	//1.把stack1的元素放到stack2,最终stack1栈底的元素存放到了stack2的栈顶
	while (!this->empty())
	{
		stack2.push(stack1.top());
		stack1.pop();
	}
	//2.取stack2栈顶的元素(即stack1栈底的元素)
	int temp = stack2.top();
	//3.再放回stack1
	while (!stack2.empty())
	{
		stack1.push(stack2.top());
		stack2.pop();
	}
	return temp;
}
//3.返回队列中最后一个元素(队尾)
int Queue::back()
{
	//断言:如果队列为空的话,中断程序
	assert(!this->empty());
	//返回stack1栈顶的元素,就是队列中队尾的元素
	return stack1.top();
}

//4.删除队列中的第一个元素
void Queue::pop()
{
	//断言:判断队列是否为空,为空则中断程序
	assert(!this->empty());
	//先要将stack1中的元素存放到stack2中,则stack2中的栈顶的元素即为stack1的栈底元素,也就是队列第一个元素
	//1.把stack1中的元素存放到stack2中
	while (!stack1.empty())
	{
		stack2.push(stack1.top());
		stack1.pop();
	}
	//2.删除stack2中的栈顶的元素(也就是队列的第一个元素)。
	stack2.pop();
	//3.再把stack2中的元素放回stack1中
	while (!stack2.empty())
	{
		stack1.push(stack2.top());
		stack2.pop();
	}
}
//5.返回队列中元素的个数
int Queue::size()
{
	return stack1.size();
}
//6.在队尾后面添加一个元素
void Queue::push(int n)
{
	//直接添加到stack1中即可
	stack1.push(n);
}
int main()
{
	Queue q;
	for (int i = 0; i < 10; i++)
	{
		q.push(i);
	}
	cout << "队列是否为空?1则为空,0则为非空,结果为:" << q.empty() << endl;
	cout << "队列中的元素的个数为:" << q.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		cout << q.front() << endl;
	
		if (0 == i)
		{
			cout << "队列中队头元素为:" << q.front() << endl;
		}
		if (9 == i)
		{
			cout << "队列中队尾元素为:" <<q.back()<< endl;
		}
		q.pop();
	}
	cout << "队列是否为空?1则为空,0则为非空" << q.empty() << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/107983644