数组实现栈
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class ArrayStack<E> {
private int capacity;
private int top;
private ArrayList<E> arr = new ArrayList<>(capacity);
public ArrayStack(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be a positive number");
}
this.capacity = capacity;
this.top = -1;
}
public void push(E element) {
if (top == capacity - 1) {
throw new IllegalStateException("Stack is full");
}
top++;
arr.add(element);
}
public E pop() {
if (isEmpty()) {
throw new NoSuchElementException("Stack is empty");
}
E oldval = arr.remove(top);
top--;
return oldval;
}
public E peek() {
if (isEmpty()) {
throw new NoSuchElementException("Stack is empty");
}
return arr.get(top);
}
public boolean isEmpty() {
return top == -1;
}
public E search(E element) {
int point = top;
while (point >= 0) {
if (arr.get(point).equals(element)) {
return arr.get(point);
}
point--;
}
return null;
}
}
链表实现栈
import java.util.NoSuchElementException;
public class LinkedStack<E> {
private Node<E> top;
private int size;
public LinkedStack() {
this.top = null;
this.size = 0;
}
public void push(E element) {
Node<E> node = new Node<>(element);
node.next = top;
top = node;
size++;
}
public E pop() {
if (isEmpty()) {
throw new NoSuchElementException("Stack is Empty");
}
E oldval = top.data;
top = top.next;
size--;
return oldval;
}
public boolean isEmpty() {
return size == 0;
}
public E peek() {
if (isEmpty()) {
throw new NoSuchElementException("Stack is Empty");
}
return top.data;
}
public int size() {
return size;
}
private static class Node<E> {
private E data;
private Node<E> next;
public Node(E data) {
this.data = data;
this.next = null;
}
}
}