package com.hmx.datastructure;
/**
* @program: datastructureandalgorithm
* @description:
* @author: hmx
* @create: 2021-06-29 16:42
**/
public class MyStack<E> {
private Node<E> top;
private int size;
public boolean empty(){
return size == 0;
}
public int size(){
return size;
}
public MyStack(){
top = null;
}
public boolean push(E e){
top = new Node(e,top);
size++;
return true;
}
public E pop(){
if(empty()){
throw new RuntimeException("空栈异常!");
} else {
Node<E> node = top;
top = top.next;
node.next = null;
size--;
return node.value;
}
}
private class Node<E> {
E value;
Node next;
public Node(){}
public Node(E value,Node next){
this.value = value;
this.next = next;
}
}
}
栈(java实现)
最新推荐文章于 2024-10-16 10:13:45 发布