使用两个队列实现堆栈

Tec*_*ink 134 algorithm stack data-structures

之前一个类似的问题,但这里的问题与它相反,使用两个队列作为堆栈.问题......

鉴于两个队列与他们的标准操作(enqueue,dequeue,isempty,size),实现堆栈与它的标准操作(pop,push,isempty,size).

应该有两个版本的解决方案.

  • 版本A:推送项目时堆栈应该是高效的; 和
  • 版本B:弹出项目时堆栈应该是高效的.

我比任何特定的语言实现更感兴趣的算法.但是,我欢迎用我熟悉的语言表达的解决方案(,,,,,).

Sva*_*nte 191

版本A(高效推送):

  • 推:
    • 在队列1中排队
  • 流行:
    • 当queue1的大小大于1时,管道将队列从队列中排队到队列2中
    • 出列并返回queue1的最后一项,然后切换queue1和queue2的名称

版本B(高效流行音乐):

  • 推:
    • 在队列2中排队
    • 将queue1中queue1的所有项排入队列,然后切换queue1和queue2的名称
  • 流行:
    • 来自queue1的deqeue

  • 版本B似乎有问题:你的意思是将queue2的所有项目排队到queue1除了最后一个元素(然后切换q1和q2的名字)? (4认同)
  • @eeerahul:我再次检查过,答案是对的.当时Icerman似乎想要将queue2的所有项目排入queue1,queue2只包含新项目,因此注释确实没有意义. (2认同)

Sam*_*uel 68

最简单(也许是唯一)这样做的方法是将新元素推入空队列,然后将另一个队列出列并进入先前空的队列.通过这种方式,最新的总是在队列的前面.这将是版本B,对于版本A,您只需通过将元素出列到第二个队列(最后一个队列除外)来反转该过程.

第0步:

"Stack"
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
|   |   |   |   |   |  |   |   |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Run Code Online (Sandbox Code Playgroud)

步骤1:

"Stack"
+---+---+---+---+---+
| 1 |   |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
| 1 |   |   |   |   |  |   |   |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Run Code Online (Sandbox Code Playgroud)

第2步:

"Stack"
+---+---+---+---+---+
| 2 | 1 |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
|   |   |   |   |   |  | 2 | 1 |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Run Code Online (Sandbox Code Playgroud)

第3步:

"Stack"
+---+---+---+---+---+
| 3 | 2 | 1 |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
| 3 | 2 | 1 |   |   |  |   |   |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Run Code Online (Sandbox Code Playgroud)


pho*_*xis 50

我们可以用一个队列来做到这一点:

推:

  1. 入队新元素.
  2. 如果n是队列中的元素数,则删除并插入元素n-1时间.

流行:

  1. 出队

.

push 1


front                     
+----+----+----+----+----+----+
| 1  |    |    |    |    |    |    insert 1
+----+----+----+----+----+----+


push2

front                     
+----+----+----+----+----+----+
| 1  | 2  |    |    |    |    |    insert 2
+----+----+----+----+----+----+

     front                     
+----+----+----+----+----+----+
|    | 2  |  1 |    |    |    |    remove and insert 1
+----+----+----+----+----+----+




 insert 3


      front                     
+----+----+----+----+----+----+
|    | 2  |  1 |  3 |    |    |    insert 3
+----+----+----+----+----+----+

           front                     
+----+----+----+----+----+----+
|    |    |  1 |  3 |  2 |    |    remove and insert 2
+----+----+----+----+----+----+

                front                     
+----+----+----+----+----+----+
|    |    |    |  3 |  2 |  1 |    remove and insert 1
+----+----+----+----+----+----+
Run Code Online (Sandbox Code Playgroud)

示例实施:

int stack_pop (queue_data *q)
{
  return queue_remove (q);
}

void stack_push (queue_data *q, int val)
{
  int old_count = queue_get_element_count (q), i;

  queue_insert (q, val);
  for (i=0; i<old_count; i++)
  {
    queue_insert (q, queue_remove (q));
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 9

import java.util.*;

/**
 *
 * @author Mahmood
 */
public class StackImplUsingQueues {

    Queue<Integer> q1 = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();

    public int pop() {
        if (q1.peek() == null) {
            System.out.println("The stack is empty, nothing to return");
            int i = 0;
            return i;
        } else {
            int pop = q1.remove();
            return pop;
        }
    }

    public void push(int data) {

        if (q1.peek() == null) {
            q1.add(data);
        } else {
            for (int i = q1.size(); i > 0; i--) {
                q2.add(q1.remove());
            }
            q1.add(data);
            for (int j = q2.size(); j > 0; j--) {
                q1.add(q2.remove());
            }

        }
    }

    public static void main(String[] args) {
        StackImplUsingQueues s1 = new StackImplUsingQueues();
        //       Stack s1 = new Stack();
        s1.push(1);
        s1.push(2);
        s1.push(3);
        s1.push(4);
        s1.push(5);
        s1.push(6);
        s1.push(7);
        s1.push(8);
        s1.push(9);
        s1.push(10);
        // s1.push(6);
        System.out.println("1st = " + s1.pop());
        System.out.println("2nd = " + s1.pop());
        System.out.println("3rd = " + s1.pop());
        System.out.println("4th = " + s1.pop());
        System.out.println("5th = " + s1.pop());
        System.out.println("6th = " + s1.pop());
        System.out.println("7th = " + s1.pop());
        System.out.println("8th = " + s1.pop());
        System.out.println("9th = " + s1.pop());
        System.out.println("10th= " + s1.pop());
    }
}
Run Code Online (Sandbox Code Playgroud)