• 栈、队列——双向链表、数组表示以及相互表示


    一、双向链表表示栈、队列

    1. import java.util.*;
    2. /**
    3. * @author zhangchaoliang
    4. * create 2022
    5. */
    6. public class DoubleEndsQueueToStackAndQueue {
    7. public static class Node{
    8. public T value;
    9. public Node last;
    10. public Node next;
    11. public Node(T data){
    12. value=data;
    13. }
    14. }
    15. public static class DoubleEndsQueue{
    16. public Node head;
    17. public Node tail;
    18. public void addFromHead(T value){
    19. Node cur = new Node<>(value);
    20. if (head==null){
    21. head=cur;
    22. tail=cur;
    23. }else {
    24. cur.next=head;
    25. head.last=cur;
    26. head=cur;
    27. }
    28. }
    29. public void addFromBottom(T value){
    30. Node cur = new Node<>(value);
    31. if (head==null){
    32. head=cur;
    33. tail=cur;
    34. }else {
    35. cur.last=tail;
    36. tail.next=cur;
    37. tail=cur;
    38. }
    39. }
    40. public T popFromHead(){
    41. if (head==null)
    42. return null;
    43. Node cur = head;
    44. if (head==tail){
    45. head=null;
    46. tail=null;
    47. }else {
    48. head=head.next;
    49. cur.next=null;
    50. head.last=null;
    51. }
    52. return cur.value;
    53. }
    54. public T popFromBottom(){
    55. if (head==null){
    56. return null;
    57. }
    58. Node cur = tail;
    59. if (head==tail){
    60. head=null;
    61. tail=null;
    62. }else {
    63. tail=tail.last;
    64. tail.next=null;
    65. cur.last=null;
    66. }
    67. return cur.value;
    68. }
    69. public boolean isEmpty(){
    70. return head==null;
    71. }
    72. }
    73. public static class MyStack{
    74. private DoubleEndsQueue queue;
    75. public MyStack(){
    76. queue=new DoubleEndsQueue<>();
    77. }
    78. public void push(T value){
    79. queue.addFromHead(value);
    80. }
    81. public T pop(){
    82. return queue.popFromHead();
    83. }
    84. public boolean isEmpty(){
    85. return queue.isEmpty();
    86. }
    87. }
    88. public static class MyQueue{
    89. private DoubleEndsQueue queue;
    90. public MyQueue(){
    91. queue=new DoubleEndsQueue<>();
    92. }
    93. public void push(T value){
    94. queue.addFromHead(value);
    95. }
    96. public T poll(){
    97. return queue.popFromBottom();
    98. }
    99. public boolean isEmpty(){
    100. return queue.isEmpty();
    101. }
    102. }
    103. public static boolean isEqual(Integer o1,Integer o2){
    104. if (o1==null&&o2!=null)
    105. return false;
    106. if (o1!=null&&o2==null)
    107. return false;
    108. if (o1==null&&o2==null)
    109. return true;
    110. return o1.equals(o2);
    111. }
    112. public static void main(String[] args) {
    113. int oneTestDataNum=100;
    114. int value = 10000;
    115. int testTimes = 100000;
    116. for (int i=0;i
    117. MyStack myStack = new MyStack<>();
    118. MyQueue myQueue = new MyQueue<>();
    119. Stack stack = new Stack<>();
    120. Queue queue = new LinkedList<>();
    121. for (int j=0;j
    122. int nums = (int)(Math.random()*value);
    123. if (stack.isEmpty()){
    124. myStack.push(nums);
    125. stack.push(nums);
    126. }else {
    127. if (Math.random()<0.5){
    128. myStack.push(nums);
    129. stack.push(nums);
    130. }else {
    131. if (!isEqual(myStack.pop(),stack.pop()))
    132. System.out.println("Oops!");
    133. }
    134. }
    135. int numq = (int)(Math.random()*value);
    136. if (queue.isEmpty()){
    137. myQueue.push(numq);
    138. queue.offer(numq);
    139. }else {
    140. if (Math.random()<0.5){
    141. myQueue.push(numq);
    142. queue.offer(numq);
    143. }else {
    144. if (!isEqual(myQueue.poll(),queue.poll()))
    145. System.out.println("Oops!");
    146. }
    147. }
    148. }
    149. }
    150. System.out.println("finish!");
    151. }
    152. }

    二、数组表示队列

    1. public class RingArray {
    2. public static class MyQueue{
    3. private int[] arr;
    4. private int pushi;
    5. private int polli;
    6. private int size;
    7. private final int limit;
    8. public MyQueue(int limit){
    9. arr = new int[limit];
    10. pushi=0;
    11. polli=0;
    12. size=0;
    13. this.limit=limit;
    14. }
    15. public void push(int value){
    16. if (size==limit){
    17. throw new RuntimeException("队列满了,不能在加了!");
    18. }
    19. size++;
    20. arr[pushi]=value;
    21. pushi=nextIndex(pushi);
    22. }
    23. public int pop(){
    24. if (size==0){
    25. throw new RuntimeException("队列空了,不能再拿了!");
    26. }
    27. size--;
    28. int ans = arr[polli];
    29. polli=nextIndex(polli);
    30. return ans;
    31. }
    32. public boolean isEmpty(){
    33. return size==0;
    34. }
    35. private int nextIndex(int i){
    36. return i1?i+1:0;
    37. }
    38. }
    39. }

    三、使用栈模拟队列

    1. public class TwoStacksImplementQueue {
    2. public static class TwoStacksQueue{
    3. public Stack stackPush;
    4. public Stack stackPop;
    5. public TwoStacksQueue(){
    6. stackPush=new Stack();
    7. stackPop=new Stack();
    8. }
    9. private void pushToPop(){
    10. if (stackPop.isEmpty()){
    11. while (!stackPush.empty()){
    12. stackPop.push(stackPush.pop());
    13. }
    14. }
    15. }
    16. public void add(int pushInt){
    17. stackPush.push(pushInt);
    18. pushToPop();
    19. }
    20. public int poll(){
    21. if (stackPop.empty()&&stackPush.empty()){
    22. throw new RuntimeException("Queue is empty!");
    23. }
    24. pushToPop();
    25. return stackPop.pop();
    26. }
    27. public int peek(){
    28. if (stackPop.empty()&&stackPush.empty()){
    29. throw new RuntimeException("Queue is empty!");
    30. }
    31. pushToPop();
    32. return stackPop.peek();
    33. }
    34. }
    35. public static void main(String[] args) {
    36. TwoStacksQueue test = new TwoStacksQueue();
    37. test.add(1);
    38. test.add(2);
    39. test.add(3);
    40. System.out.println(test.peek());
    41. System.out.println(test.poll());
    42. System.out.println(test.peek());
    43. System.out.println(test.poll());
    44. System.out.println(test.peek());
    45. System.out.println(test.poll());
    46. }
    47. }

    四、使用队列模拟栈

    1. public class TwoQueueImplementStack {
    2. public static class TwoQueueStack {
    3. public Queue queue;
    4. public Queue 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 poll() {
    13. while (queue.size() > 1) {
    14. help.offer(queue.poll());
    15. }
    16. T ans = queue.poll();
    17. Queue tmp = queue;
    18. queue = help;
    19. help = tmp;
    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 tmp = queue;
    29. queue = help;
    30. help = tmp;
    31. return ans;
    32. }
    33. public boolean isEmpty() {
    34. return queue.isEmpty();
    35. }
    36. }
    37. public static void main(String[] args) {
    38. System.out.println("test begin");
    39. TwoQueueStack myStack = new TwoQueueStack<>();
    40. Stack test = new Stack<>();
    41. int testTime = 1000000;
    42. int max = 1000000;
    43. for (int i = 0; i < testTime; i++) {
    44. if (myStack.isEmpty()) {
    45. if (!test.isEmpty()) {
    46. System.out.println("Oops");
    47. }
    48. int num = (int) (Math.random() * max);
    49. myStack.push(num);
    50. test.push(num);
    51. } else {
    52. if (Math.random() < 0.25) {
    53. int num = (int) (Math.random() * max);
    54. myStack.push(num);
    55. test.push(num);
    56. } else if (Math.random() < 0.5) {
    57. if (!myStack.peek().equals(test.peek())) {
    58. System.out.println("Oops");
    59. }
    60. } else if (Math.random() < 0.75) {
    61. if (!myStack.poll().equals(test.pop())) {
    62. System.out.println("Oops");
    63. }
    64. } else {
    65. if (myStack.isEmpty() != test.isEmpty()) {
    66. System.out.println("Oops");
    67. }
    68. }
    69. }
    70. }
    71. System.out.println("test finish!");
    72. }
    73. }

    五、找出栈中的最小值

    1. public class GetMinStack {
    2. public static class MyStack1{
    3. private Stack stackData;
    4. private Stack stackMin;
    5. public MyStack1(){
    6. this.stackData=new Stack();
    7. this.stackMin=new Stack();
    8. }
    9. public void push(int newNum){
    10. if (this.stackMin.isEmpty()){
    11. this.stackMin.push(newNum);
    12. }else if (newNum<=this.getmin()){
    13. this.stackMin.push(newNum);
    14. }
    15. this.stackData.push(newNum);
    16. }
    17. public int pop(){
    18. if (this.stackData.isEmpty()){
    19. throw new RuntimeException("You stack is Empty.");
    20. }
    21. int value = this.stackData.pop();
    22. if (value==this.getmin()){
    23. this.stackMin.pop();
    24. }
    25. return value;
    26. }
    27. public int getmin(){
    28. if (this.stackMin.isEmpty()){
    29. throw new RuntimeException("You stack is empty.");
    30. }
    31. return this.stackMin.peek();
    32. }
    33. }
    34. public static class MyStack2{
    35. private Stack stackData;
    36. private Stack stackMin;
    37. public MyStack2(){
    38. this.stackData=new Stack();
    39. this.stackMin=new Stack();
    40. }
    41. public void push(int newNum){
    42. if (this.stackMin.isEmpty()){
    43. throw new RuntimeException("You stack is empty.");
    44. }else if (newNum<this.getMin()){
    45. this.stackMin.push(newNum);
    46. }else {
    47. newNum=this.stackMin.peek();
    48. this.stackMin.push(newNum);
    49. }
    50. this.stackData.push(newNum);
    51. }
    52. public int pop(){
    53. if (this.stackData.isEmpty()){
    54. throw new RuntimeException("You stack is empty.");
    55. }
    56. this.stackMin.pop();
    57. return this.stackData.pop();
    58. }
    59. public int getMin(){
    60. if (this.stackMin.isEmpty()){
    61. throw new RuntimeException("You stack is empty.");
    62. }
    63. return this.stackMin.peek();
    64. }
    65. }
    66. public static void main(String[] args) {
    67. MyStack1 stack1 = new MyStack1();
    68. stack1.push(3);
    69. System.out.println(stack1.getmin());
    70. stack1.push(4);
    71. System.out.println(stack1.getmin());
    72. stack1.push(1);
    73. System.out.println(stack1.getmin());
    74. System.out.println(stack1.pop());
    75. System.out.println(stack1.getmin());
    76. System.out.println("=============");
    77. MyStack1 stack2 = new MyStack1();
    78. stack2.push(3);
    79. System.out.println(stack2.getmin());
    80. stack2.push(4);
    81. System.out.println(stack2.getmin());
    82. stack2.push(1);
    83. System.out.println(stack2.getmin());
    84. System.out.println(stack2.pop());
    85. System.out.println(stack2.getmin());
    86. }
    87. }

  • 相关阅读:
    Android 应用框架层 SQLite 源码分析
    DNS解析为什么不生效?DNS解析不生效原因分析
    数据结构-并查集刷题
    RestTemplate配置和使用
    MySQL—MySQL架构
    「从零单排canal 07」 parser模块源码解析
    非线性二分类——机器学习
    算法|最大堆、最小堆和堆排序的实现(JavaScript)
    “摸鱼”就能得出设计灵感?他的经验分享得看
    23种设计模式之 : 模板方法设计模式
  • 原文地址:https://blog.csdn.net/z1171127310/article/details/126841788