Java 实现链表 单向链表

package sort;

/**
 * 学生实体类
 * @author Brook Lieu
 * @version v1.0 2014-10-2
 */
public class Student {
    private int id;
    private String name;
    private int age;
    
    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "id: " + id + ",name: " + name + ",age: " + age;
    }
    
}

package sort;

/**
 * 链接点
 * @author Brook Lieu
 * @version v1.0 2014-10-2
 */
public class Link {
    private Student student;
    private Link next;
    
    public Link(Student student, Link next) {
        this.student = student;
        this.next = next;
    }
    
    /**
     * @return the student
     */
    public Student getStudent() {
        return student;
    }
    /**
     * @param student the student to set
     */
    public void setStudent(Student student) {
        this.student = student;
    }
    /**
     * @return the next
     */
    public Link getNext() {
        return next;
    }
    /**
     * @param next the next to set
     */
    public void setNext(Link next) {
        this.next = next;
    }
    
    
}

package sort;

/**
 * 链接表
 * @author Brook Lieu
 * @version v1.0 2014-10-2
 */
public class LinkList {
    private Link first;

    /**
     * 在链接表头部加入链接点
     * @param link
     */
    public void addFirst(Link link) {
        if (isEmpty()) {
            first = link;
        } else {
            link.setNext(first);
            first = link;
        }
    }
    
    public void showList() {
        Link next = first;
        while(next != null) {
            System.out.println(next.getStudent().toString());
            next = next.getNext();
        }
    }
    
    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty() {
        return first == null;
    }

    public int findElementNum() {
        Link next = first;
        int count = 0;
        while (next != null) {
            count++;
            next = next.getNext();
        }
        
        return count;
    }
    
    /**
     * 删除链接表头部链接点
     * @return
     */
    public Student deleteFirst() {
        Link temp = first;
        first = first.getNext();
        return temp.getStudent();
    }
    
    /**
     * 根据学生id找到对应的学生
     * @param studentId
     * @return
     */
    public Student findLink(int studentId) {
        Link link = first;
        while (link != null) {
            if (link.getStudent().getId() == studentId) return link.getStudent();
            link = link.getNext();
        }
        return null;
    }
    
    /**
     * @return the first
     */
    public Link getFirst() {
        return first;
    }

    /**
     * @param first the first to set
     */
    public void setFirst(Link first) {
        this.first = first;
    }
}

package sort;

public class LinkListTest {
   public static void main(String[] args) {
       LinkList linkList = new LinkList();
       Student student = new Student(1, "brook", 24);
       Link link = new Link(student, null);
       
       linkList.addFirst(link);
       
       student = new Student(2, "amy", 24);
       link = new Link(student, null);
       linkList.addFirst(link);
       
       linkList.showList();
       System.out.println("============");
       student = new Student(3, "jackie", 21);
       link = new Link(student, null);
       linkList.addFirst(link);
       linkList.showList();
       
       System.out.println("LinkList count is " + linkList.findElementNum());
       
       System.out.println(linkList.findLink(2).toString());
       
       System.out.println("=======================");
       System.out.println(linkList.deleteFirst());
       linkList.showList();
   }
}

测试方法输出结果为:

id: 2,name: amy,age: 24
id: 1,name: brook,age: 24
============
id: 3,name: jackie,age: 21
id: 2,name: amy,age: 24
id: 1,name: brook,age: 24
LinkList count is 3
id: 2,name: amy,age: 24
=======================
id: 3,name: jackie,age: 21
id: 2,name: amy,age: 24
id: 1,name: brook,age: 24

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值