当前位置:   article > 正文

【Java数据结构】栈和队列(概念+练题)_java实现栈和队列数据结构练习题

java实现栈和队列数据结构练习题

目录

1. 实现栈(数组)

MyStack.java

2. 实现队列(链式)

MyQueueLinked.java

3. 实现循环队列

MyCircularQueue.java

4. LeetCode习题

4.1 括号匹配问题

4.2 用队列实现栈

4.3 用栈实现队列

4.4 实现一个最小栈


  • 栈:先进后出
  • 队列:先进先出

1. 实现栈(数组)

MyStack.java

  1. public class MyStack {
  2. public int[] elem;
  3. public int usedSize;
  4. public MyStack() {
  5. this.elem = new int[10];
  6. }
  7. //判断是否栈满
  8. public boolean isFull() {
  9. if (this.usedSize == this.elem.length) {
  10. return true;
  11. }
  12. return false;
  13. }
  14. //push
  15. public void push(int item) {
  16. if(isFull()) {
  17. //扩容
  18. this.elem = Arrays.copyOf(this.elem,2*2*this.elem.length);
  19. }
  20. this.elem[this.usedSize] = item;
  21. this.usedSize++;
  22. }
  23. //pop
  24. public int pop() {
  25. if(empty()) {
  26. throw new RuntimeException("栈空了!");
  27. }
  28. int val = this.elem[this.usedSize-1];
  29. this.usedSize--;
  30. return val;
  31. }
  32. //peek
  33. public int peek() {
  34. if(empty()) {
  35. throw new RuntimeException("栈空了!");
  36. }
  37. return this.elem[this.usedSize-1];
  38. }
  39. //判断为空
  40. public boolean empty() {
  41. if(this.usedSize == 0) return true;
  42. return false;
  43. }
  44. }

2. 实现队列(链式)

MyQueueLinked.java

  1. class Node {
  2. public int data;
  3. public Node next;
  4. public Node(int data) {
  5. this.data = data;
  6. }
  7. }
  8. public class MyQueueLinked {
  9. private Node front;
  10. private Node rear;
  11. private int usedSize = 0;
  12. //入队列
  13. public void offer(int val) {
  14. Node node = new Node(val);
  15. if (this.front == null) {
  16. this.front = node;
  17. this.rear = node;
  18. } else {
  19. this.rear.next = node;
  20. this.rear = node;
  21. }
  22. this.usedSize++;
  23. }
  24. //出队头元素
  25. public int poll() {
  26. if(isEmpty()) {
  27. throw new RuntimeException("队列为空!");
  28. }
  29. int val = this.front.data;
  30. if(this.front.next == null) {
  31. //只有一个结点
  32. this.front = null;
  33. this.rear = null;
  34. } else {
  35. this.front = this.front.next;
  36. }
  37. this.usedSize--;
  38. return val;
  39. }
  40. //得到队头元素
  41. public int peek() {
  42. if(isEmpty()) {
  43. throw new RuntimeException("队列为空!");
  44. }
  45. return this.front.data;
  46. }
  47. public boolean isEmpty() {
  48. return this.usedSize == 0;
  49. }
  50. public int size() {
  51. return this.usedSize;
  52. }
  53. }

3. 实现循环队列

MyCircularQueue.java

  1. public class MyCircularQueue {
  2. private int[] elem;
  3. private int usedSize;
  4. private int front;
  5. private int rear;
  6. public MyCircularQueue(int k) {
  7. this.elem = new int[k+1];
  8. }
  9. //入队
  10. public boolean enQueue(int value) {
  11. if (isFull()) {
  12. return false;
  13. }
  14. this.elem[this.rear] = value;
  15. this.rear = (this.rear+1) % this.elem.length;
  16. return true;
  17. }
  18. //出队
  19. public boolean deQueue() {
  20. if (isEmpty()) {
  21. return false;
  22. }
  23. this.front = (this.front+1) % this.elem.length;
  24. return true;
  25. }
  26. //得到对头元素
  27. public int Front() {
  28. if ((isEmpty())) {
  29. return -1;
  30. }
  31. return this.elem[this.front];
  32. }
  33. public int Rear() {
  34. if (isEmpty()) {
  35. return -1;
  36. }
  37. if (this.rear == 0) {
  38. return this.elem[this.elem.length-1];
  39. }
  40. return this.elem[this.rear-1];
  41. }
  42. public boolean isEmpty() {
  43. if (this.front == this.rear) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. public boolean isFull() {
  49. if((this.rear+1) % this.elem.length == this.front) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. }

4. LeetCode习题

4.1 括号匹配问题

20. 有效的括号 - 力扣(LeetCode) (leetcode-cn.com)

思路:先判断是否为空、s.length()是否为0或奇数。

然后变量字符串,如果为左括号,则push进栈,如果为右括号,则与pop栈中的对比。

  1. class Solution {
  2. public boolean isValid(String s) {
  3. if(s == null || s.length() == 0) return true;
  4. Stack<Character> stack = new Stack<>();
  5. for (int i = 0; i < s.length(); i++) {
  6. char ch = s.charAt(i);
  7. if(ch == '{' ||ch == '(' ||ch == '[' ) {
  8. stack.push(ch);
  9. } else {
  10. if (stack.empty()) return false;
  11. //栈不为空
  12. char tmp = stack.peek();
  13. if(ch == '}' && tmp == '{' ||ch == ')' && tmp == '(' ||ch == ']' && tmp == '[') {
  14. stack.pop();
  15. } else {
  16. return false;
  17. }
  18. }
  19. }
  20. if (!stack.empty()) return false;
  21. return true;
  22. }
  23. }

4.2 用队列实现栈

225. 用队列实现栈 - 力扣(LeetCode) (leetcode-cn.com)

  1. class MyStack {
  2. private Queue<Integer> qu1 = new LinkedList<>();
  3. private Queue<Integer> qu2 = new LinkedList<>();
  4. public MyStack() {
  5. }
  6. //入栈
  7. public void push(int x) {
  8. if(!qu1.isEmpty()) {
  9. qu1.offer(x);
  10. } else if (!qu2.isEmpty()){
  11. qu2.offer(x);
  12. } else {
  13. qu1.offer(x);
  14. }
  15. }
  16. //出栈
  17. public int pop() {
  18. if(empty()) return -1;
  19. int e = -1;
  20. if (!qu1.isEmpty()) {
  21. int size = qu1.size();
  22. for (int i = 0; i < size -1; i++) {
  23. e = qu1.poll();
  24. qu2.offer(e);
  25. }
  26. e = qu1.poll();
  27. } else {
  28. int size = qu2.size();
  29. for (int i = 0; i < size -1; i++) {
  30. e = qu2.poll();
  31. qu1.offer(e);
  32. }
  33. e = qu2.poll();
  34. }
  35. return e;
  36. }
  37. //得到栈顶元素,不删除
  38. public int top() {
  39. if(empty()) return -1;
  40. int e = -1;
  41. if (!qu1.isEmpty()) {
  42. int size = qu1.size();
  43. for (int i = 0; i < size; i++) {
  44. e = qu1.poll();
  45. qu2.offer(e);
  46. }
  47. } else {
  48. int size = qu2.size();
  49. for (int i = 0; i < size; i++) {
  50. e = qu2.poll();
  51. qu1.offer(e);
  52. }
  53. }
  54. return e;
  55. }
  56. //是否为空
  57. public boolean empty() {
  58. return qu1.isEmpty() && qu2.isEmpty();
  59. }
  60. }

4.3 用栈实现队列

232. 用栈实现队列 - 力扣(LeetCode) (leetcode-cn.com)

一个栈用来入,另一个栈用来出

  1. class MyQueue {
  2. private Stack<Integer> s1 = new Stack<>();
  3. private Stack<Integer> s2 = new Stack<>();
  4. public MyQueue() {
  5. }
  6. public void push(int x) {
  7. s1.push(x); //指定push到第一个栈
  8. }
  9. public int pop() {
  10. if (empty()) return -1;
  11. if (s2.empty()) {
  12. while (!s1.empty()) {
  13. s2.push(s1.pop());
  14. }
  15. }
  16. return s2.pop();
  17. }
  18. public int peek() {
  19. if (empty()) return -1;
  20. if (s2.empty()) {
  21. while (!s1.empty()) {
  22. s2.push(s1.pop());
  23. }
  24. }
  25. return s2.peek();
  26. }
  27. public boolean empty() {
  28. return s1.empty() && s2.empty();
  29. }
  30. }

4.4 实现一个最小栈

155. 最小栈 - 力扣(LeetCode) (leetcode-cn.com)

两个栈,一个正常栈,另一个栈存储最小值

  1. class MinStack {
  2. private Stack<Integer> s1 = new Stack<>();
  3. private Stack<Integer> s2 = new Stack<>();
  4. public MinStack() {
  5. }
  6. public void push(int val) {
  7. s1.push(val);
  8. if (s2.empty() || val <= s2.peek()) {
  9. s2.push(val);
  10. }
  11. }
  12. public void pop() {
  13. int val = s1.pop();
  14. if(!s2.empty() && s2.peek()==val) {
  15. s2.pop();
  16. }
  17. }
  18. public int top() {
  19. if(!s1.empty()) {
  20. return s1.peek();
  21. }
  22. return 0;
  23. }
  24. public int getMin() {
  25. if(!s2.empty()) {
  26. return s2.peek();
  27. }
  28. return 0;
  29. }
  30. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/668305
推荐阅读
相关标签
  

闽ICP备14008679号