LinkedIn

  www.linkedin.com
  www.linkedin.com

Interview Question

Interview Question Interview

coding: use two stacks to implement a queue

Tags:
technical
Answer

Interview Answer

1 Answer

0

public class Queue<E>{
  private Stack<E> inbox = new Stack<E>();
  private Stack<E> outbox = new Stack<E>();

  public void queue(E item) {
    inbox.push(item);
  }

  public E dequeue() {
    if (outbox.isEmpty()) {
      while (!inbox.isEmpty()) {
        outbox.push(inbox.pop());
      }
    }
  return outbox.pop();
  }
}

Anonymous on Aug 8, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.