栈是一种数据结构,只能在某一端进行插入、删除操作的特殊线性表,通常是在尾端进行插入删除操作,允许插入、删除的一端称为栈顶(top),另一端称为栈底(bottom)
从栈顶插入一个元素,是“压栈”,从栈顶删除一个元素,是“出栈”,栈中的元素是按照 后进先出(LIFO) 的原则进行的。
顺序存储
import java.util.Arrays;
/**
* Created by Administrator on 2018/3/13.
*/
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);
}
}
public T pop() {
T oldValue = (T) elementData[size - 1];
elementData[--size] = null;
return oldValue;
}
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--) {
if (i < size - 1) {
sb.append(", ");
}
sb.append(elementData[i].toString());
}
sb.append("]");
return sb.toString();
}
}
public static void main(String[] args) {
SequenceStack<String> stack = new SequenceStack<>();
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);
}
}
链式存储:
/**
* Created by Administrator on 2018/3/13.
*/
public class LinkStack<T> {
private class Node {
private T data;
private Node next;
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("[");
Node current = top;
for (int i = size - 1; i > -1 && current != null; i--, current = current.next) {
if (i < size - 1) {
sb.append(", ");
}
sb.append(current.data.toString());
}
sb.append("]");
return sb.toString();
}
}
public static void main(String[] args) {
LinkStack<String> stack = new LinkStack<>();
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);
}
}