关于栈
栈是一种后进先出的数据结构,栈限制只能在一端进行插入和删除。
栈顶:进行插入和删除操作的一端。
栈底: 不变的一端。
栈的数组实现
使用数组来存储栈中的元素,push 的时候,直接添加一个元素到 数组 arr[n] 中 , pop 的时候直接返回 数组最后一个元素 s[n] 。
/**
* 基于数组实现的栈
*/
public class ArrayStack<E> {
private Object[] elements;
private int top = -1;
public Stack(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Illegal capacity :" + capacity);
}
elements = new Object[capacity];
}
public void push(E e) {
if (top == elements.length - 1) {
resize(2 * elements.length);
}
elements[++top] = e;
}
private void resize(int newSize) {
elements = Arrays.copyOf(elements, newSize);
System.out.println("resize =====" + elements.length);
}
public E pop() {
if (top == -1) {
throw new IllegalArgumentException("Stack is empty");
}
E e = (E) elements[top];
elements[top] = null; // 重置元素空值
top--;
if (top > 0 && top <= elements.length / 4) {
resize(elements.length / 2);
}
return e;
}
public int length() {
return elements.length;
}
public boolean isEmpty() {
return top == -1;
}
public int size() {
return top + 1;
}
public void display() {
System.out.print("stack length = " + elements.length + " , bottom -> top : ");
for (int i = 0; i <= top; i++) {
if (i == top) {
System.out.print(elements[i]);
} else {
System.out.print(elements[i] + " , ");
}
}
System.out.println();
}
public static void main(String[] args) {
ArrayStack<String> stack = new ArrayStack<>(10);
stack.push("tomcat");
stack.push("linux1");
stack.push("linux2");
stack.push("linux3");
stack.push("linux4");
stack.push("linux5");
stack.push("linux6");
stack.push("linux7");
stack.push("linux8");
stack.push("linux9");
stack.push("linux10");
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.display();
stack.pop();
stack.pop();
stack.display();
stack.push("apple");
stack.push("google");
stack.push("baidu");
stack.display();
}
}
resize =====20
resize =====10
resize =====5
stack length = 5 , bottom -> top : tomcat , linux1 , linux2
resize =====2
stack length = 2 , bottom -> top : tomcat
resize =====4
stack length = 4 , bottom -> top : tomcat , apple , google , baidu
Push的时候,当元素的个数达到数组的Capacity的时候,我们开辟2倍于当前元素的新数组,然后将原数组中的元素拷贝到新数组中。Pop的时候,当元素的个数小于当前容量的1/4的时候,我们将原数组的大小容量减少1/2。
栈的链表实现
/**
* 链表节点
*/
public class Node<T> {
public T value; // 节点值域
public Node next = null; // 表示下一个节点的地址域
// 参数为节点的值,因为一个节点在创建时不知道指向下一个节点的地址
public Node(T val) {
this.value = val;
}
@Override
public String toString() {
return value + "";
}
}
/**
* 栈
*/
public class LinkListStack<T> {
private Node<T> first;
public LinkListStack() {
this.first = new Node(null);
}
public void push(T t) {
Node node = new Node(t);
node.next = first.next;
first.next = node;
}
public T pop() {
Node<T> node = (Node<T>) first.next;
T t = node.value;
first.next = first.next.next;
return t;
}
public int length() {
int length = 0;
Node temp = this.first;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
public void display() {
System.out.print("stack = ");
Node temp = first.next;
while (temp != null) {
System.out.print(temp + ",");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkListStack<String> stack = new LinkListStack();
stack.push("apple");
stack.display();
stack.push("google");
stack.display();
stack.push("facebook");
stack.display();
String top = stack.pop();
System.out.println("pop : " + top);
stack.display();
stack.push("mi");
stack.display();
stack.push("xiaomi");
stack.display();
}
}
stack = apple,
stack = google,apple,
stack = facebook,google,apple,
pop : facebook
stack = google,apple,
stack = mi,google,apple,
stack = xiaomi,mi,google,apple,