简单java链表的实显
package Text;
public class Dome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Lick lick = new Lick();
lick.add(new Preson("张三" , -9));
lick.add(new Preson("李四" , 25));
lick.add(new Preson("王五" , 30));
lick.remove(new Preson("李四" , 25));
lick.infor();
System.out.println(lick.find(new Preson("王五" , 30)));
}
}
//定义preson类
class Preson extends Object{
private String name ;
private int age ;
public Preson(){
//无参
}
public Preson(String name , int age){
this.name = name ;
this.age = age ;
}
@Override
//对象对比
public boolean equals(Object arg) {
Preson other = (Preson) arg; // 将object类强转成Preson类
if(arg == null){ //判断传入的质是否为空
return false ;
}else if(this == other){ //判断地址是否相 等
return true ;
}else if(this.name.equals(other.name) && this.age == other.age){//将对象内容依次对比
return true ;
}else{
return false ;
}
}
@Override
public String toString() {
return "姓名:" + name + "\t" +"年龄:" + age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(this.age >= 0){
this.age = age;
}else {
this.age = 0;
}
}
}
//链表类
class Lick{
private Node root;
public Lick(){
//无参
}
public Lick(Node root){
this.root = root;
}
//添加内容
public void add(Preson date){
if(date == null){ //判断内容是否为空 如果为空就反回调用处
return ;
}else{ //如果不为空 进行打包数据
Node newNode = null ;
if(date.getAge() >= 0){ //判断输入的年龄是否大于0;
newNode = new Node(date);
}else { //如果不大于0年龄就默认为0
date.setAge(0);
newNode = new Node(date);
}
if(this.root == null ){ //判断根节点是否为空 如果不为空将第一个数据为根节点
this.root = newNode;
}else{
this.root.addNode(newNode);//如果不为空,调用Node
}
}
}
//查找数据
public boolean find(Preson date){
if(this.root == null){ //判断根节点是否为空,如果为空反回false
return false;
}else if(this.root.date.equals(date)){ //判断要查找的数据是否是根节点如果是返回true
return true;
}else { //不是就调用Node类处理
return this.root.findNode(date);
}
}
//删除数据
public void remove(Preson date){
if(date.equals(date)){ //判断要删除的数据是否存在
if(this.root.date.equals(date)){ // 判断要删除的数据是否是根节点。如果是根节点为下一节点
this.root = this.root.next;
}else{ //如果不是就调用Node类处理
this.root.removeNode(this.root ,date);
}
}else{ // 如果要删除的数据不存在近回调用处
return ;
}
}
//输出方法
public void infor(){
if(this.root != null){ //判断根节点是否为空,不为空调用Node类输出
this.root.inforNode();
}
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
class Node{
private Preson date;
private Node next;
public Node(){
//无参
}
public Node(Preson date , Node next){
this.date = date ;
this.next = next;
}
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode;
}else{
this.next.addNode(newNode);
}
}
public boolean findNode(Preson date){
if(this.date.equals(date)){
return true ;
}else if(this.next != null){
return this.next.findNode(date);
}else {
return false;
}
}
public void removeNode(Node jd ,Preson date){
if(this.date.equals(date)){
jd.next = this.next;
}else {
this.next.removeNode(this, date);
}
}
public void inforNode(){
System.out.println(date.toString());
if(this.next != null){
this.next.inforNode();
}
}
public Node(Preson date) {
this.date = date ;
}
public Node(Node next){
this.next = next ;
}
}
}