标签:
栈的最基本功能是保障后进先出,然后在此基础上可以对在栈中的对象进行弹入弹出,此外,在弹出时,如果栈为空,则会报错,所以还需要提供获取当前栈大小的方法。
/** * Created by lili on 15/11/14. */ public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; Student student = (Student) o; if (getAge() != student.getAge()) return false; return !(getName() != null ? !getName().equals(student.getName()) : student.getName() != null); } @Override public int hashCode() { int result = getName() != null ? getName().hashCode() : 0; result = 31 * result + getAge(); return result; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
这里重写了几个来自Object的方法,equals,hashCode和toString。
import java.util.LinkedList; /** * Created by lili on 15/11/14. */ public class MyStack { private LinkedList linkedList; public MyStack() { linkedList = new LinkedList(); } public void push(Object o) { linkedList.addFirst(o); } public Object pop() { //删除并返回 return linkedList.removeFirst(); } public boolean isEmpty() { return linkedList.isEmpty(); } }
标签:
原文地址:http://www.cnblogs.com/gslyyq/p/4967046.html