|
數組
|
鏈表
|
內存浪費
|
無浪費
|
有浪費:需存如額外引用信息 |
動態
|
非動態:大小沒法運行時隨意變更
|
動態的:能夠隨意增長或縮小
|
push操做
|
當數組大小超過期,須要擴容O(n)。
數組大小足夠時,直接push完成 O(1)
|
直接鏈表首部插入O(1). 但需新建節點
|
public class StackTest<E> { public static void main(String[] args) { StackTest<Integer> stackTest = new StackTest<>(); for (int i = 4; i > 0; i--) { System.out.println("push:" + stackTest.push(Integer.valueOf(i)).intValue()); } System.out.println("peek:" + stackTest.peek()); System.out.println("pop:" + stackTest.pop()); System.out.println("isEmpty:" + stackTest.isEmpty()); for (int i = 4; i > 0; i--) { System.out.println("search " + i + ":" + stackTest.search(Integer.valueOf(i))); } } //棧頂定義 StackNode<E> top; //節點定義: static class StackNode<E> { E data; StackNode<E> next; StackNode(E data, StackNode<E> next) { this.data = data; this.next = next; } } //向棧頂push一個元素,即向鏈表首部添加元素 public E push(E data) { top = new StackNode<E>(data, top); return top.data; } //返回棧頂的值。即鏈表首部節點的值。 public E peek() { if (isEmpty()) throw new RuntimeException("fail,stack is null!"); return top.data; } //從棧頂pop一個元素,即返回棧頂的值 並刪除鏈表第一個節點。 public E pop() { E preTopData = peek(); top = top.next; return preTopData; } //判空 public boolean isEmpty() { return top == null; } //查找數據爲data的節點位置,棧頂爲1.沒找到返回-1. public int search(E data) { int position = 1; StackNode<E> currNode = top; while (currNode != null && !currNode.data.equals(data)) { position++; currNode = currNode.next; } if (currNode == null) position=-1; return position; } }
push:4 push:3 push:2 push:1 peek:1 pop:1 isEmpty:false search 4:3 search 3:2 search 2:1 search 1:-1
public E push(E item) { addElement(item); return item; }
//數組變量定義 protected Object[] elementData; //有效元素個數,在棧中即表示棧的個數 protected int elementCount; //當數組溢出時,擴容 增長的大小。 protected int capacityIncrement; //3種構造方式,默認構造方式的 數組大小初始化爲10. public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } public Vector(int initialCapacity) { this(initialCapacity, 0); } public Vector() { this(10); } //增長元素 public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; }
private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); }
Vector類:
html
public synchronized int size() { return elementCount; } public synchronized E elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } return elementData(index); } @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; }
public synchronized void removeElementAt(int index) { modCount++; if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } int j = elementCount - index - 1; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; elementData[elementCount] = null; /* to let gc do its work */ }
public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; }
public synchronized int lastIndexOf(Object o) { return lastIndexOf(o, elementCount-1); } public synchronized int lastIndexOf(Object o, int index) { if (index >= elementCount) throw new IndexOutOfBoundsException(index + " >= "+ elementCount); if (o == null) { for (int i = index; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = index; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
public boolean empty() { return size() == 0; }
public class StackTest<E> { public static void main(String[] args) { System.out.println(symbolMatch("{for(int i=0;i<10;i++)}")); System.out.println(symbolMatch("[5(3*2)+(2+2)]*(2+0)")); System.out.println(symbolMatch("([5(3*2)+(2+2))]*(2+0)")); } public static boolean symbolMatch(String expression) { final char CHAR_NULL = ' '; if (expression == null || expression.equals("")) throw new RuntimeException("expression is nothing or null"); //StackTest<Character> stack = new StackTest<Character>(); Stack<Character> stack = new Stack<Character>(); char[] exps = expression.toCharArray(); for (int i = 0; i < exps.length; i++) { char matchRight = CHAR_NULL; switch (exps[i]) { case '(': case '[': case '{': stack.push(Character.valueOf(exps[i])); break; case ')': matchRight = '('; break; case ']': matchRight = '['; break; case '}': matchRight = '{'; break; } if(matchRight == CHAR_NULL) continue; if (stack.isEmpty()) return false; if (stack.peek().charValue() == matchRight) stack.pop(); } if (stack.isEmpty()) return true; return false; } }
true true false