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());
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值