如何使用两个堆栈实现队列_使用两个队列实现堆栈

如何使用两个堆栈实现队列

Stack and Queue at a glance...

堆叠和排队一目了然...

Stack:

堆栈:

The stack is an ordered list where insertion and deletion are done from the same end, top. The last element that entered first is the first one to be deleted (the basic principle behind the LIFO). That means it is a data structure which is implemented as LIFO.

堆栈是一个有序列表,其中插入和删除从同一末端开始。 首先输入的最后一个元素是要删除的第一个元素(LIFO的基本原理)。 这意味着它是一个实现为LIFO的数据结构。

The main stack operations are (basic ADT operations)...

主堆栈操作为(基本ADT操作)...

  1. push (int data): Insertion at top

    push(int数据):在顶部插入

  2. int pop(): Deletion from top

    int pop():从顶部删除

Queue:

队列:

The queue is an ordered list in which insertions are done at one end (rear) and deletions are done from another end (front). The first element that got inserted is the first one to be deleted (basic principle of FIFO). That means it is a data structure which is implemented as FIFO.

队列是一个有序列表,其中插入是在一端(后部)完成,而删除是从另一端(前部)完成。 插入的第一个元素是要删除的第一个元素(FIFO的基本原理)。 这意味着它是一个实现为FIFO的数据结构。

The main Queue operations are (basic ADT operations)...

主要的Queue操作是(基本ADT操作)...

  1. EnQueue (int data): Insertion at rear end

    EnQueue(int数据):在后端插入

  2. int DeQueue(): Deletion from front end

    int DeQueue():从前端删除

使用两个队列实现堆栈 (Implementation of a stack using two queues)

Likewise, a queue can be implemented with two stacks, a stack can also be implemented using two queues. The basic idea is to perform stack ADT operations using the two queues.

同样,一个队列可以用两个堆栈实现,一个堆栈也可以用两个队列实现。 基本思想是使用两个队列执行堆栈ADT操作。

So, we need to implement push(),pop() using DeQueue(), EnQueue() operations available for the queues.

因此,我们需要使用队列可用的DeQueue()和EnQueue()操作来实现push(),pop()。

Implementation:

实现方式:

Let q1 and q2 be the two queues...

设q1和q2为两个队列...

struct stack{
	struct queue *q1;
	struct queue *q2;
}

Algorithm to implement push and pop:

实现推送和弹出的算法:

Push operation algorithm:

推送操作算法:

  1. Check whether q1 is empty or not. If q1 is empty then EnQueue the element to q2.

    检查q1是否为空。 如果q1为空,则将元素排队到q2。

  2. Otherwise EnQueue to q1.

    否则,排队到q1。

push(struct stack *s,int data){
	if(isempty(s->q1))
		EnQueue(s->q2,data);
	else
		EnQueue(s->q1,data);
}

Pop operation algorithm

弹出操作算法

The basic idea is to transfer n-1 elements (let n be the total no of elements) to other queue and delete the last one from a queue to perform the pop operation.

基本思想是将n-1个元素(使n为元素总数)转移到其他队列,并从队列中删除最后一个元素以执行弹出操作。

  1. If q1 is not empty then transfer n-1 elements from q1 to q2 and DeQueue the last element and return it.

    如果q1不为空,则将n-1个元素从q1传输到q2,并对最后一个元素进行DeQueue并返回。

  2. If q2 is not empty then transfer n-1 elements from q2 to q1 and DeQueue the last element and return it.

    如果q2不为空,则将n-1个元素从q2传输到q1,并对最后一个元素进行DeQueue并返回。

int pop(struct stack *s){
	int count,size;
	if(isempty(s->q2)){
		size=Size(s->q1);
		count=0;
		while(count<size-1){
			//transferring n-1 elements
			EnQueue(s->q2,DeQueue(s->q1));
			count++;
		}
		//last element to be popped
		return DeQueue(s->q1);
	}
	else{
		size=Size(s->q2);
		count=0;
		while(count<size-1){
			EnQueue(s->q1,DeQueue(s->q2));
			count++;
		}
		return DeQueue(s->q1);
	}
}

Time complexity analysis:

时间复杂度分析:

  1. Push: O(1)

    推:O(1)

  2. Pop: O(n) , since transferring n-1 elements

    弹出:O(n),因为传输了n-1个元素

使用C ++的实现代码(使用STL) (Implementation code using C++ (using STL))

#include <bits/stdc++.h>

using namespace std;

struct Stack{
	queue<int> q1,q2;

	void push(int x){
		if(q1.empty()){
			q2.push(x);    //EnQueue operation using STL
		}
		else{
			q1.push(x);    //EnQueue operation using STL
		}
	}

	int pop(){
	int count,size,item;
	if(q2.empty()){
		size=q1.size();            //size=no of elements;
		count=0;
		while(count<size-1){         //transfering n-1 elements
			q2.push(q1.front());     // DeQueue operation using STL
			q1.pop();
			count++;
		}
		item=q1.front();
		q1.pop();
		return item;                 //popping out the element
	}
	else{
		size=q2.size();
		count=0;
		while(count<size-1){
			q1.push(q2.front());
			q2.pop();
			count++;
		}
		item=q2.front();
		q2.pop();
		return item;
		}
	}
};

int main()
{
	cout<<"implementing stack with two queues"<<endl;

	cout<<"enter any integer to push and 0 to stop pushing"<<endl;

	Stack s;
	int x,count=0;
	
	cin>>x;
	
	while(x){
		s.push(x);
		cin>>x;
		count++;
	}
	
	cout<<"now popping......."<<endl;

	while(count){
		cout<<s.pop()<<endl;
		count--;
	}
	
	cout<<"executed successfully!!!"<<endl;

	return 0;
}

Output

输出量

implementing stack with two queues
enter any integer to push and 0 to stop pushing
1
2
3
0
now popping.......
3
2
1
executed successfully!!!


翻译自: https://www.includehelp.com/data-structure-tutorial/implementation-of-stack-using-two-queues.aspx

如何使用两个堆栈实现队列

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用两个堆栈实现队列的算法如下: 1. 创建两个堆栈stack1和stack2,分别用于存储队列中的元素。 2. 元素入队操作:将元素压入stack1中。 3. 元素出队操作:先检查stack2是否为空,如果不为空,则直接弹出stack2的顶元素作为队列的出队元素;如果stack2为空,则将stack1中的所有元素依次弹出并压入stack2中,然后再从stack2中弹出顶元素作为队列的出队元素。 4. 元素获取操作:同出队操作,只是不弹出顶元素。 下面是使用Java语言实现的代码: ```java import java.util.Stack; public class QueueWithTwoStacks<T> { private Stack<T> stack1 = new Stack<>(); private Stack<T> stack2 = new Stack<>(); public void enqueue(T item) { stack1.push(item); } public T dequeue() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } public T peek() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.peek(); } public boolean isEmpty() { return stack1.isEmpty() && stack2.isEmpty(); } public int size() { return stack1.size() + stack2.size(); } } ``` 在上面的代码中,enqueue()方法将元素压入stack1中,dequeue()方法先检查stack2是否为空,如果不为空,则直接弹出stack2的顶元素作为队列的出队元素;如果stack2为空,则将stack1中的所有元素依次弹出并压入stack2中,然后再从stack2中弹出顶元素作为队列的出队元素。peek()方法也是同理。isEmpty()方法检查队列是否为空,size()方法返回队列中元素的个数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值