基于java语言的单链表

来源:互联网 发布:淘宝账号在哪看到 编辑:程序博客网 时间:2024/06/04 04:16

链表的基本概念   

  先来看看百度百科对链表的描述

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。

简单链表的逻辑表示如下图所示,其中Head是链表中第一个元素的引用

      

java代码描述

//学生信息类,存放节点的内容
class Student{    private String name;    private int age;    private int num;    public String getName() {        return name;    }    public int getNum() {        return num;    }    public int getAge() {        return age;    }    public Student(String name, int age, int num) {        this.name = name;        this.age = age;        this.num = num;    }}

//节点类,包括学生对象和指向节点的引用
class Node{    Student stu;    Node reference;    public Node(Student stu) {        this.stu = stu;    }    public Student getStu() {        return stu;    }    public Node getReference() {        return reference;    }}
//链表,主要执行了插入、删除和查找三种操作
class LinkList{    //声明头结点    Node head = null;    //插入节点    public void insert(Student stu){        Node node = new Node(stu);        node.reference = head;        head = node;    }    //遍历链表    public void Display(){        if(head == null){            System.out.println("链表为空");        }        else{            Node currentRef;            currentRef = head;            while (currentRef != null){                System.out.println(currentRef.getStu().getName()+"  "+                                   currentRef.getStu().getNum()+"  "+                                   currentRef.getStu().getAge());                currentRef = currentRef.reference;            }        }    }    //按照学号查找    public Node FindByNum(int num){        if(head == null){            System.out.println("链表为空");            return null;        }        else{            Node currentRef;            currentRef = head;            while (currentRef != null){                if(currentRef.getStu().getNum() == num) {                    return currentRef;                }                currentRef = currentRef.reference;            }        return null;        }    }    //根据学号删除    public boolean DelteStu(int num){        if(head == null){            System.out.println("链表为空");            return false;        }        else if(head.getStu().getNum() == num){            head = head.reference;            System.out.println("删除成功");            return true;        }        else {            Node before;            Node currentRef;            before = head;            currentRef = head.reference;            while (currentRef !=null){                if(currentRef.getStu().getNum() == num){                    before.reference = currentRef.reference;                    System.out.println("删除成功");                    return true;                }                before = currentRef;                currentRef = currentRef.reference;            }            System.out.println("不存在该学生");            return false;        }    }

主函数(测试类
public  class BasicLinkList {    public static void main(String[] arg){        LinkList list = new LinkList();        list.insert(new Student("lucy",12,1011));        list.insert(new Student("peter",12,1021));        list.insert(new Student("smith",12,1061));        System.out.println("姓名  学号  年龄");        list.Display();        Node node = list.FindByNum(1061);        if(node == null){            System.out.println("不存在这个学生");        }        else {            System.out.println(node.getStu());        }        list.DelteStu(1061);        list.Display();    }}

第一次贴这么多代码,请大家多多指教!


0 0