算法通关村第一关挑战——帮鱼皮打印算法学员名单

package com.yugutou.charpter1_linklist.hk;

/**
 * 第一关挑战
 */
public class StudentLinkList {


    public static void main(String[] args) {

        Student head = init();

        Student ccPython = new Student("cc", "Python");
        add(head, ccPython);

        Student bcCPP = new Student("bc", "CPP");
        add(head, bcCPP);

        Student acJava = new Student("ac", "Java");
        add(head, acJava);

        System.out.println("整个链表打印:" + head);

    }

    public static void add(Student head, Student addStudent) {

        Student tmp = new Student("-1", "-1");

        tmp.next = head;

        Student tmp2 = null;

        while (tmp != null) {

            if (addStudent.language.equals(tmp.language)) {

                while (tmp != null && addStudent.language.equals(tmp.language)) {

                    tmp2 = tmp;

                    tmp = tmp.next;

                }

//                System.out.println("找到的尾部为:" + tmp2);

                tmp2.next = addStudent;
                addStudent.next = tmp;

                break;

            }

            tmp = tmp.next;

        }

    }

    public static Student init() {

        Student aaJava = new Student("aa", "Java");
        Student abJava = new Student("ab", "Java");
        Student baCPP = new Student("ba", "CPP");
        Student bbCPP = new Student("bb", "CPP");
        Student caPython = new Student("ca", "Python");
        Student cbPython = new Student("cb", "Python");

        aaJava.next = abJava;
        abJava.next = baCPP;
        baCPP.next = bbCPP;
        bbCPP.next = caPython;
        caPython.next = cbPython;

        return aaJava;

    }

}

class Student {

    String name;
    String language;
    Student next;

    public Student(String name, String language) {
        this.name = name;
        this.language = language;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", language='" + language + '\'' +
                ", next=" + next +
                '}';
    }
}

你可能感兴趣的:(算法,java,算法,链表)