Day36_Stack&Queue

  1. 用順序映像實現棧。
package week7.day36;

import java.util.EmptyStackException;

/**
* 順序映像實現棧
* API:
* void push(E e)
* E pop()
* E peek()
* boolean isEmpty()
*/
public class MyStack<E> {
   private static final int DEFAULT_CAPACITY = 16;
   private static final int MAX_CAPACITY = Integer.MAX_VALUE - 8;
   int size;
   // 屬性
   Object[] elements;

   public MyStack() {
       elements = new Object[DEFAULT_CAPACITY];
   }

   public MyStack(int initialCapacity) {
       if (initialCapacity <= 0 || initialCapacity > MAX_CAPACITY) {
           throw new IllegalArgumentException("initialCapacity=" + initialCapacity);
       }
       elements = new Object[initialCapacity];
   }

   /**
    * 將元素入棧
    *
    * @param e 待入棧的元素
    */
   public void push(E e) {
       if (size == elements.length) {
           int newCapacity = calculateCapacity();
           grow(newCapacity);
       }
       elements[size++] = e;
   }

   private int calculateCapacity() {
       if (size == MAX_CAPACITY) {
           throw new StackOverflowException();
       }
       int newCapacity = (elements.length << 1);
       if (newCapacity < 0 || newCapacity > MAX_CAPACITY) {
           newCapacity = MAX_CAPACITY;
       }
       return newCapacity;
   }

   private void grow(int newCapacity) {
       Object[] newArr = new Object[newCapacity];
       // 複製元素
       System.arraycopy(newArr, 0, elements, 0, elements.length);
       // 把elements指向新數組
       elements = newArr;
   }

   /**
    * 將棧頂元素出棧
    *
    * @return 棧頂元素
    */
   @SuppressWarnings("unchecked")
   public E pop() {
       if (isEmpty()) {
           throw new EmptyStackException();
       }
       return (E) elements[--size];
   }

   /**
    * 訪問棧頂元素
    *
    * @return 棧頂元素
    */
   @SuppressWarnings("unchecked")
   public E peek() {
       if (isEmpty()) {
           throw new EmptyStackException();
       }
       return (E) elements[size-1];
   }

   /**
    * 判斷棧是否爲空棧
    *
    * @return 如果是空棧, 返回true, 否則返回false
    */
   public boolean isEmpty() {
       return size == 0;
   }
}

class Test2 {
   public static String reverse(String original) {
       // 遍歷字符串, 獲取每一個字符, 然後將字符壓棧
       MyStack<Character> stack = new MyStack<>();
       for (int i = 0; i < original.length(); i++) {
           char c = original.charAt(i);
           stack.push(c);
       }
       // 將字符出棧, 拼接每個字符
       StringBuilder sb = new StringBuilder();
       while (!stack.isEmpty()) {
           sb.append(stack.pop());
       }
       return sb.toString();
   }

   public static void main(String[] args) {
       String s = "abc";
       System.out.println(reverse(s));


   }
}

package week7.day36;

public class StackOverflowException extends RuntimeException {
   public StackOverflowException() {
   }

   public StackOverflowException(String message) {
       super(message);
   }
}
  1. 用非順序映像實現隊列。
package week7.day36;

import java.util.NoSuchElementException;

/*
循環隊列:
void enqueue(E e)
E dequeue()
E peek()
boolean isEmpty()
int size
*/
public class MyQueue<E> {

   private int size;
   private Node front;
   private Node rear;

   // 構造方法
   public MyQueue() {
       front = new Node();
       rear = new Node();
       front.next = rear;
   }

   public MyQueue(E value) {
       front = new Node();
       rear = new Node(value);
       front.next = rear;
       size++;
   }

   /**
    * 將元素e入隊列
    *
    * @param e 待插入的元素
    */
   public MyQueue<E> enqueue(E e) {
       // 插入元素
       rear=rear.next = new Node(e);
       size++;
       return this;
   }

   /**
    * 將隊頭元素出隊列
    *
    * @return 隊頭元素
    */
   @SuppressWarnings("unchecked")
   public E dequeue() {
       if (isEmpty()) {
           throw new NoSuchElementException();
       }
       E removeValue = (E) front.next.value;
       front.next = front.next.next;
       size--;
       return removeValue;
   }

   /**
    * 訪問隊頭元素
    *
    * @return 隊頭元素
    */
   @SuppressWarnings("unchecked")
   public E peek() {
       if (isEmpty()) {
           throw new NoSuchElementException();
       }
       return (E) front.next.value;
   }

   /**
    * 判斷隊列是否爲空隊列
    *
    * @return 如果是空隊列返回true, 否則返回false
    */
   public boolean isEmpty() {
       return size == 0;
   }

   /**
    * 獲取隊列中元素的個數
    *
    * @return 元素的個數
    */
   public int size() {
       return size;
   }

   @Override
   public String toString() {
       return "MyQueue{" + front.next + '}';
   }

   private static class Node {
       Object value;
       Node next;

       public Node() {
       }

       public Node(Object value) {
           this.value = value;
       }

       public Node(Object value, Node next) {
           this.value = value;
           this.next = next;
       }

       @Override
       public String toString() {
           if (this.next == null) {
               return "" + value;
           }
           return value + "-->" + this.next.toString();
       }
   }
}

class Test3 {
   public static void main(String[] args) {
       MyQueue<Integer> queue = new MyQueue<>(1);
       queue.enqueue(2).enqueue(3).enqueue(4).enqueue(5);
       System.out.println(queue);
       System.out.println(queue.dequeue());
       System.out.println(queue);
       System.out.println(queue.peek());
   }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章