1.用鏈表實現一個棧package com.sun;import java.util.EmptyStackException;public class DLinkedStackEx { PRivate Node head; private Node tail; // 表示棧的大小 private int count; public Object push(String element) { if (null == head) { head = new Node(element, null, null); tail = head; count += 1; } else { Node tmp = new Node(element, null, null); // 尾節點的下一個節點是新創建的節點 tail.next = tmp; // 新節點的前一個節點是尾節點 tmp.prev = tail; // 把為節點放在最后的節點上(就是新創建的節點) tail = tmp; count += 1; } return element; } public Object pop() { if (isEmpty()) { throw new EmptyStackException(); } else { Object pop = tail.element; // 創建一個臨時節點引用指向前一個 Node tmp = tail.prev; // 斷開最后一個與前一個 tail.prev = null; // 斷開前一個與最后一個 tmp.next = null; // 把最后一個指向前一個 tail = tmp; count -= 1; return pop; } } public boolean isEmpty() { if (null == head || 0 == count || null == tail) { return true; } return false; } // 獲得棧頂元素 public Object peek() { if (isEmpty()) { return null; } else { return tail.getElement(); } } public int size() { return count; } public int search(String elment) { if (isEmpty()) { throw new EmptyStackException(); } else { Node tmphead = head; Node tmptail = tail; for (int i = 0; i <= count / 2; i++) { if (elment.equals(tmptail.element)) { return i; } if (elment.equals(tmphead.element)) { return count - 1 - i; } tmptail = tmptail.prev; tmphead = tmphead.next; } return -1; } } public DLinkedStackEx() { } private class Node { // 要保存的數據 private Object element; // 當前節點的下一個節點 private Node next; public Object getElement() { return element; } @SuppressWarnings("unused") public void setElement(Object element) { this.element = element; } @SuppressWarnings("unused") public Node getNext() { return next; } @SuppressWarnings("unused") public void setNext(Node next) { this.next = next; } @SuppressWarnings("unused") public Node getPrev() { return prev; } @SuppressWarnings("unused") public void setPrev(Node prev) { this.prev = prev; } // 當前節點的前一個節點 private Node prev; public Node(String element, Node pnext, Node pprev) { this.element = element; this.next = pnext; this.prev = pprev; } }}2.用數組實現一個棧
package com.sun;public class Stack {/** 棧的規則:先進后出*/public class DStack {private static final int STACK_DEFAULT_SIZE = 16;private static final int STACK_EMPTY_SIZE = 0;// 用來保存元素private Object[] arrayElement;// 用來表示棧中元素的個數,一般解釋就是棧的大小private int count;public DStack() {arrayElement = new Object[STACK_DEFAULT_SIZE];count = STACK_EMPTY_SIZE;}// 壓棧@SuppressWarnings("unused")public Object push(String element) {// 1.棧是空的if (isEmpty()) {arrayElement[0] = element;count += 1;}// 2. 棧不空也不滿if (!isFull() && !isEmpty()) {arrayElement[count] = element;count += 1;}// 3. 棧滿了if (isFull()) {// 1. 分配一個臨時的原來大小+16的空間Object[] tmp = new Object[arrayElement.length+ STACK_DEFAULT_SIZE];if (null == tmp) {throw new RuntimeException("has no xxx space, please change LinkedStack.");}// 2. 把原來數組中的元素拷貝到新的數組中,再把新的元素增加進來。for (int j = 0; j < arrayElement.length; j++) {tmp[j] = arrayElement[j];}tmp[arrayElement.length] = element;count += 1;// 4. 釋放原來的空間for (int j = 0; j < arrayElement.length; j++) {arrayElement[j] = null;}arrayElement = null;// 5. 把臨時的數組引用指向原來的數組(這步一定不能省略)arrayElement = tmp;}return element;}// 從棧中彈出元素public Object pop() {Object popObject = arrayElement[count - 1];arrayElement[count - 1] = null;count -= 1;return popObject;}// 獲得棧頂的元素的拷貝public Object peek() {return arrayElement[count - 1];}// 判斷棧是否為空// true 表示棧為空的。public boolean isEmpty() {return count == STACK_EMPTY_SIZE ? true : false;}// 獲得元素在棧中的位置public int search(String element) {for (int i = 0; i < count; i++) {if (arrayElement[i].equals(element)) {return count - 1 - i;}}return -1;}// 判斷棧是否為滿// true 表示棧為滿的。private boolean isFull() {return count == arrayElement.length ? true : false;}}}
新聞熱點
疑難解答