本文主要是介绍算法通关村第一关挑战——帮鱼皮打印算法学员名单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", language='" + language + '\'' +", next=" + next +'}';}
}
这篇关于算法通关村第一关挑战——帮鱼皮打印算法学员名单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!