package utils;
public class MyStack<T> {
private Object[] elements;
private int top;
private int maxSize;
public MyStack(int size) {
elements = new Object[size];
top = -1;
maxSize = size;
}
public void push(T x) {
elements[++top] = x;
}
public T pop() {
return (T)elements[top--];
}
public T peek() {
return (T)elements[top];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public static void main(String[] args) {
MyStack<Integer> stack= new MyStack<Integer>(10);
for (int i = 0; i < 10; ++i) {
stack.push(i);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
}
}
为算法考试做准备--栈(Stack)实现
最新推荐文章于 2019-07-28 09:33:48 发布