Java数据结构——单链表

package p1;

public class Main {
    public static void main(String[] args) {
        studentNode st1 = new studentNode(1, "小红", 1101);
        studentNode st2 = new studentNode(2, "小蓝", 1102);
        studentNode st3 = new studentNode(3, "小白", 1101);
        studentNode st4 = new studentNode(4, "小黄", 1102);
        studentNode st5 = new studentNode(4, "mmm", 1100);
        linkedList list = new linkedList();
        list.add(st1);
        list.add(st2);
        list.addOrder(st4);
        list.addOrder(st3);
        System.out.println("链表中的内容为:");
        list.show();
        System.out.println("修改num=4的学生,修改后的内容为:");
        list.update(st5);
        list.show();
        System.out.println("删除编号为2的学生,删除后的内容为:");
        list.delete(2);
        list.show();
    }
}

class linkedList {
    studentNode head = new studentNode(0, "", 0);

    //添加节点
    public void add(studentNode node) {
        studentNode t = head; //创建变量来存放当前节点
        while (true) {
            if (t.next == null) //找到单链表的尾部
                break;
            else
                t = t.next;
        }
        t.next = node;
    }

    //按顺序添加节点
    //当找到某个节点的next中的num值大于该节点的num值,便把该节点插入
    //若找不到这样的节点,就在最后添加该节点
    public void addOrder(studentNode node) {
        studentNode t = head;
        boolean f = true;
        while (true) {
            if (t.next == null)
                break;
            if (t.next.num > node.num) {
                break;
            } else if (t.next.num == node.num) {
                System.out.println("该节点已存在");
                f = false;
                break;
            } else
                t = t.next;
        }
        if (f) {
            node.next = t.next;
            t.next = node;
        }
    }

    //修改节点
    public void update(studentNode node) {
        studentNode t = head.next;
        boolean f = false;
        while (true) {
            if (t == null) {
                System.out.println("未找到该节点,无法修改");
                break;
            }
            if (t.num == node.num) {
                f = true;
                break;
            } else
                t = t.next;
        }
        if (f) {
            t.num = node.num;
            t.name = node.name;
            t.Class = node.Class;
        }
    }

    //删除节点
    public void delete(int num) {
        studentNode t = head;
        boolean f = false;
        while (true) {
            if (t.next == null) {
                System.out.println("没找到要删除得到节点");
                break;
            }
            if (t.next.num == num) {
                f = true;
                break;
            } else
                t = t.next;
        }
        if (f) {
            t.next = t.next.next;
        }
    }

    //显示链表数据
    public void show() {
        studentNode t = head.next;
        if (head.next == null) {
            System.out.println("该链表为空");
            return;
        }
        while (t != null) {
            System.out.println(t);
            t = t.next;
        }
    }
}

//创建节点类
class studentNode {
    public int num;
    public String name;
    public int Class;
    studentNode next;

    public studentNode(int n, String na, int c) {
        this.num = n;
        this.name = na;
        this.Class = c;
    }

    //重写toString()方法
    @Override
    public String toString() {
        return "studentNode{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", Class=" + Class +
                '}';
    }
}

链表中的内容为:
studentNode{num=1, name='小红', Class=1101}
studentNode{num=2, name='小蓝', Class=1102}
studentNode{num=3, name='小白', Class=1101}
studentNode{num=4, name='小黄', Class=1102}
修改num=4的学生,修改后的内容为:
studentNode{num=1, name='小红', Class=1101}
studentNode{num=2, name='小蓝', Class=1102}
studentNode{num=3, name='小白', Class=1101}
studentNode{num=4, name='mmm', Class=1100}
删除编号为2的学生,删除后的内容为:
studentNode{num=1, name='小红', Class=1101}
studentNode{num=3, name='小白', Class=1101}
studentNode{num=4, name='mmm', Class=1100}

 

你可能感兴趣的:(Java数据结构)