栈底层实际上就是个数组
栈方法
Stack stack = new Stack<>();
stack.push(1);//入栈
stack.push(20);
1.System.out.println(stack.peek());//查看拿到栈顶元素 不删除 结果为20
2.System.out.println(stack.pop());//出栈 删除栈顶元素 结果为20
3.System.out.println(stack.peek());//结果为1
4.System.out.println(stack.empty());//判断栈是否为空 结果为false
Stack stack = new Stack<>();
stack.push(1);//入栈
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println(stack.size());//栈中含有的元素 5
System.out.println(stack.search(2));//返回从栈顶往前数第size()- i(i为栈下标)个元素 4
import java.util.Stack;
public class MyStack {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);//入栈
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println(stack.size());//栈中含有的元素 5
System.out.println(stack.search(2));//返回从栈顶往前数第size()-i(i为下表)个元素 4
/*System.out.println(stack.peek());//查看拿到栈顶元素 不删除 结果为20
*//*System.out.println(stack.pop());//出栈 删除栈顶元素 20
System.out.println(stack.peek());//1*//*
System.out.println(stack.empty());//判断栈是否为空 false*/
}
}
top表示的是当前可以存放数据元素的下标
push元素val
public boolean full() {
return this.top == this.elem.length;//判断栈是否满了
}
public void push(int val) {
if (full()) {//如果是满的 扩容 拷贝原来数据 2
this.elem = Arrays.copyOf(this.elem,2this.elem.length);
}
this.elem[top++] = val;//如果没有满 val放到栈顶top指向的位置 然后top++
}
pop() 元素
public boolean empty() {//判断是否为空的
return this.top == 0;
}
public int pop() {
if (empty()) {
throw new RuntimeException(“栈空!”);//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
int data = this.elem[this.top-1];//拿到要pop的数据的数据 用data存放
this.top–;//删掉那个元素
return data;
//return this.elem[–this.top];//上面的简写
peek()元素
public int peek() {
if (empty()) {
throw new RuntimeException(“栈空!”);//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
return this.elem[this.top-1];//此时拿到top-1的元素 但是top依旧是top
}
size()
public int size() {
return this.top;
}
import java.util.Arrays;
public class MyStack {
public int[] elem;//数组 默认值为null
public int top;//表示可以存放数据元素的下标
public MyStack() {//构造方法
this.elem = new int[10];//数组放10个元素 都是0
}
public void push(int val) {
if (full()) {//如果是满的 扩容 拷贝原来数据 *2
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[top++] = val;//如果没有满 val放到top指向的位置 然后top++
}
/* public boolean empty() {//判断是否为空的
return this.top == 0;
}*/
public boolean full() {
return this.top == this.elem.length;//判断栈是否满了
}
public boolean empty() {//判断是否为空的
return this.top == 0;
}
public int pop() {
if (empty()) {
throw new RuntimeException("栈空!");//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
int data = this.elem[this.top-1];//拿到要pop的数据的数据 用data存放
this.top--;//删掉那个元素
return data;
//return this.elem[--this.top];//上面的简写
}
public int peek() {
if (empty()) {
throw new RuntimeException("栈空!");//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
return this.elem[this.top-1];//此时拿到top-1的元素 但是top依旧是top
}
public int size() {
return this.top;
}
}
改为泛型
import java.util.Arrays;
public class MyStack<T> {
public T[] elem;//数组 默认值为null
public int top;//表示可以存放数据元素的下标
public MyStack() {//构造方法
this.elem = (T[]) new Object[10];//数组放10个元素 都是0
}
public void push(T val) {
if (full()) {//如果是满的 扩容 拷贝原来数据 *2
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[top++] = val;//如果没有满 val放到top指向的位置 然后top++
}
/* public boolean empty() {//判断是否为空的
return this.top == 0;
}*/
public boolean full() {
return this.top == this.elem.length;//判断栈是否满了
}
public boolean empty() {//判断是否为空的
return this.top == 0;
}
public T pop() {
if (empty()) {
throw new RuntimeException("栈空!");//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
T data = this.elem[this.top-1];//拿到要pop的数据的数据 用data存放
this.top--;//删掉那个元素
return data;
//return this.elem[--this.top];//上面的简写
}
public T peek() {
if (empty()) {
throw new RuntimeException("栈空!");//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
return this.elem[this.top-1];//此时拿到top-1的元素 但是top依旧是top
}
public int size() {
return this.top;
}
}
import java.util.List;
import java.util.Stack;
public class TestDemo {
public static void main(String[] args) {
// List<Integer> list = new Stack<>();//可以这样写 但是一般情况下不这样写
Stack<Integer> list = new Stack<>();//直接用自己类写
MyStack<Integer> myStack = new MyStack<>();
myStack.push(1);
myStack.push(2);
myStack.push(3);
System.out.println(myStack.peek());//3
System.out.println(myStack.pop());//3
System.out.println(myStack.peek());//2
}
}