1. 队列和栈

2. 数组实现队列

  1. public class MyQueue {
  2. private int[] arr;
  3. private int pushi;
  4. private int polli;
  5. private int size;
  6. private final int limit;
  7. public MyQueue(int limit) {
  8. this.limit = limit;
  9. pushi = 0;// 头指针
  10. polli = 0;// 尾指针
  11. size = 0;
  12. arr = new int[limit];
  13. }
  14. public void push(int value) {
  15. if (size == limit) {
  16. throw new RuntimeException("队列已满 无法继续添加");
  17. }
  18. size++;
  19. arr[pushi] = value;
  20. pushi = nextIndex(pushi);
  21. }
  22. public int pop(int value) {
  23. if (size == 0) {
  24. throw new RuntimeException("队列为空");
  25. }
  26. size--;
  27. int ans = arr[polli];
  28. polli = nextIndex(polli);
  29. return ans;
  30. }
  31. public boolean isEmpty() {
  32. return size == 0;
  33. }
  34. private int nextIndex(int index) {
  35. // index = (index+1) % limit;
  36. return index < limit - 1 ? index + 1 : 0;
  37. }
  38. }

3. 栈实现队列

  1. // 使用两个栈实现
  2. public static class Myqueue {
  3. private Stack<Integer> StackPull;
  4. private Stack<Integer> StackPop;
  5. public Myqueue() {
  6. StackPull = new Stack<Integer>();
  7. StackPop = new Stack<Integer>();
  8. }
  9. public void push(int value) {
  10. StackPull.push(value);
  11. pushToPop();
  12. }
  13. private void pushToPop() {
  14. if (StackPop.isEmpty()) {
  15. while (!StackPull.isEmpty()) {
  16. StackPop.push(StackPull.pop()); // 将栈顶 压到 pop栈中
  17. }
  18. }
  19. }
  20. public int pop() {
  21. if (StackPull.isEmpty() && StackPop.isEmpty()) {
  22. throw new RuntimeException("队列中无元素");
  23. }
  24. pushToPop();
  25. return StackPop.pop();
  26. }
  27. public int peek() {
  28. if (StackPull.isEmpty() && StackPop.isEmpty()) {
  29. throw new RuntimeException("队列中无元素");
  30. }
  31. pushToPop();
  32. return StackPop.peek();
  33. }
  34. }
  35. public static void main(String[] args) {
  36. Myqueue test = new Myqueue();
  37. test.push(1);
  38. test.push(2);
  39. test.push(5);
  40. System.out.println(test.peek());
  41. System.out.println(test.pop());
  42. System.out.println(test.peek());
  43. System.out.println(test.pop());
  44. System.out.println(test.peek());
  45. System.out.println(test.pop());
  46. }

4. 队列实现栈

  1. // 两个队列实现
  2. public static class TwoQueueStack<T> {
  3. public Queue<T> queue;
  4. public Queue<T> help;
  5. public TwoQueueStack() {
  6. queue = new LinkedList<>();
  7. help = new LinkedList<>();
  8. }
  9. public void push(T value) {
  10. queue.offer(value); // 加入到队尾
  11. }
  12. public T pop() {
  13. while (queue.size() > 1) {
  14. help.offer(queue.poll()); // 除了队尾的元素 全部转移到另外一个队列中
  15. }
  16. T ans = queue.poll(); // 当前队尾元素为栈顶
  17. Queue<T> tep = queue; // 缓存当前空队列
  18. queue = help; // 重新转移到queue中
  19. help = tep;
  20. return ans;
  21. }
  22. public T peek() {
  23. while (queue.size() > 1) {
  24. help.offer(queue.poll());
  25. }
  26. T ans = queue.poll();
  27. help.offer(ans); // 由于是查看栈顶 所以获取到值后 重新添加到另外队列中
  28. Queue<T> tep = queue;
  29. queue = help;
  30. help = tep;
  31. return ans;
  32. }
  33. public boolean isEmpty() {
  34. return queue.isEmpty();
  35. }
  36. }