栈_Java
顺序栈:
import java.util.Arrays; public class SequenceStack<T> { private int DEFAULT_SIZE = 10; private int capacity; private int capacityIncrement = 0; private Object[] elementData; private int size = 0; public SequenceStack(){ capacity = DEFAULT_SIZE; elementData = new Object[capacity]; } public SequenceStack(T element){ this(); elementData[0] = element; size++; } public SequenceStack(T element , int initSize){ this.capacity = initSize; elementData = new Object[capacity]; elementData[0] = element; size++; } public SequenceStack(T element , int initSize , int capacityIncrement){ this.capacity = initSize; this.capacityIncrement = capacityIncrement; elementData = new Object[capacity]; elementData[0] = element; size++; } public int length(){ return size; } public void push(T element){ ensureCapacity(size + 1); elementData[size++] = element; } private void ensureCapacity(int minCapacity) { if(minCapacity > capacity){ if(capacityIncrement > 0){ while(capacity < minCapacity){ capacity += capacityIncrement; } } else { while(capacity < minCapacity){ capacity <<= 1; } } elementData = Arrays.copyOf(elementData, capacity); } } @SuppressWarnings("unchecked") public T pop(){ T oldValue = (T)elementData[size-1]; elementData[--size] = null; return oldValue; } @SuppressWarnings("unchecked") public T peek(){ return (T)elementData[size-1]; } public boolean empty(){ return size == 0; } public void clear(){ Arrays.fill(elementData, null); size = 0; } public String toString(){ if(size == 0){ return "[]"; } else { StringBuilder sb = new StringBuilder("["); for(int i = size - 1 ; i > -1 ; i--){ sb.append(elementData[i].toString() + ", "); } int len = sb.length(); return sb.delete(len-2, len).append("]").toString(); } } }
测试代码:
public class SequenceStackTest { public static void main(String[] args) { SequenceStack<String> stack = new SequenceStack<String>(); stack.push("aaaa"); stack.push("bbbb"); stack.push("cccc"); stack.push("dddd"); System.out.println(stack); System.out.println("访问栈顶元素: " + stack.peek()); System.out.println("第一次弹出栈顶元素: " + stack.pop()); System.out.println("第二次弹出栈顶元素: " + stack.pop()); System.out.println("两次pop之后的栈: " + stack); } }
测试结果:
[dddd, cccc, bbbb, aaaa]
访问栈顶元素: dddd
第一次弹出栈顶元素: dddd
第二次弹出栈顶元素: cccc
两次pop之后的栈: [bbbb, aaaa]
链表栈:
public class LinkStack<T> { private class Node{ private T data; private Node next; @SuppressWarnings("unused") public Node(){ } public Node(T data , Node next){ this.data = data; this.next = next; } } private Node top; private int size; public LinkStack(){ top = null; } public LinkStack(T element){ top = new Node(element , null); size++; } public int length(){ return size; } public void push(T element){ top = new Node(element , top); size++; } public T pop(){ Node oldTop = top; top = top.next; oldTop.next = null; size--; return oldTop.data; } public T peek(){ return top.data; } public boolean empty(){ return size == 0; } public void clear(){ top = null; size = 0; } public String toString(){ if(empty()){ return "[]"; } else { StringBuilder sb = new StringBuilder("["); for(Node current = top ; current != null ; current = current.next){ sb.append(current.data.toString() + ", "); } int len = sb.length(); return sb.delete(len-2, len).append("]").toString(); } } }
测试代码:
public class LinkStackTest { public static void main(String[] args) { LinkStack<String> stack = new LinkStack<String>(); stack.push("aaaa"); stack.push("bbbb"); stack.push("cccc"); stack.push("dddd"); System.out.println(stack); System.out.println("访问栈顶元素: " + stack.peek()); System.out.println("第一次弹出栈顶的元素: " + stack.pop()); System.out.println("第二次弹出栈顶的元素: " + stack.pop()); System.out.println("两次pop之后的栈: " + stack); } }
测试结果:
[dddd, cccc, bbbb, aaaa]
访问栈顶元素: dddd
第一次弹出栈顶的元素: dddd
第二次弹出栈顶的元素: cccc
两次pop之后的栈: [bbbb, aaaa]