完整代码:
package LinkList;
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
LinkedListTest linkedListTest = new LinkedListTest();
LinkedList linkedList = linkedListTest.new LinkedList();
Scanner scan = new Scanner(System.in);
boolean isLoop = true;
while (isLoop) {
System.out.println("-------------------------------------------------------------------------------------");
System.out.println("SingleLinkedList Test");
System.out.println("Enter 'l' to print all node");
System.out.println("Enter 'au' to add or update node ");
System.out.println("Enter 'r' to remove node");
System.out.println("Enter 'q' to query node");
System.out.println("Enter 'e' to exit program");
System.out.println("-------------------------------------------------------------------------------------");
String str = scan.next();
switch (str) {
case "l" -> {
try {
linkedList.list();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
case "au"->{
int no;
String name;
String nickName;
System.out.println("Please enter 'no','name','nickName'[Update node when the 'no' is exist]:");
System.out.println("Enter the 'no':");
no = scan.nextInt();
System.out.println("Enter the 'name':");
name = scan.next();
System.out.println("Enter the 'nickName':");
nickName=scan.next();
linkedList.addOrUpdate(no,name,nickName);
}
case "r"->{
System.out.println("Enter the 'no' you'll remove:");
int no = scan.nextInt();
try {
HeroNode temp = linkedList.remove(no);
System.out.println("Remove no:[" + temp.no+"] success.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
case "q"->{
System.out.println("Enter the 'no' you'll query:");
int no = scan.nextInt();
try {
HeroNode result = linkedList.query(no);
System.out.println("The node is:");
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
case "e"-> isLoop = false;
}
}
System.out.println("Program exit.");
}
/**
* 节点类
*/
public class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no,String name,String nickname){
this.no = no;
this.name = name;
this.nickname = nickname;
}
public HeroNode() {
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
", next=" + next +
'}';
}
}
/**
* 单链表结构
*/
public class LinkedList{
HeroNode head = new HeroNode();
/**
* 添加或修改节点
* @param no
* @param name
* @param nickName
*/
public void addOrUpdate(int no,String name,String nickName){
HeroNode temp = head;
boolean isAdd = true;
while (temp.next != null) {
if (no == temp.no){
temp.name = name;
temp.nickname = nickName;
isAdd = false;
System.out.println("修改节点成功!");
break;
}
temp = temp.next;
}
if (isAdd){
temp.next = new HeroNode(no,name,nickName);
System.out.println("添加节点成功!");
}
}
/**
* 打印所有节点
*/
public void list(){
HeroNode temp = head.next;
boolean isNull = true;
while (temp != null) {
isNull = false;
System.out.println(temp);
temp = temp.next;
}
if (isNull){
throw new RuntimeException("链表为空,无法展示!");
}
}
/**
* 删除节点
* @return
*/
public HeroNode remove(int no){
HeroNode temp = head;
while (temp.next != null){
if (temp.next.no == no){
HeroNode p = temp.next;
temp.next = p.next;
p.next = null;
System.out.println("节点删除成功!");
return p;
}
}
throw new RuntimeException("找不到该节点,删除失败!");
}
/**
* 查询指定节点
* @param no
* @return
*/
public HeroNode query(int no) {
HeroNode temp = head.next;
while (temp != null){
if (no == temp.no){
System.out.println("查询成功!");
return temp;
}
temp = temp.next;
}
throw new RuntimeException("找不到该节点!");
}
}
}