public class TestDemo {
public static void main(String[] args) {
StudentNode student1 = new StudentNode(1, "Alice");
StudentNode student2 = new StudentNode(2, "Tom");
StudentNode student3 = new StudentNode(3, "Jack");
StudentNode student4 = new StudentNode(4, "Mary");
StudentNode student5 = new StudentNode(3, "Amy");
SingleLinkedList singleLinkedList = new SingleLinkedList();
// singleLinkedList.showLinkedList();
//测试添加add方法
// singleLinkedList.add(student1);
// singleLinkedList.add(student2);
// singleLinkedList.add(student4);
// singleLinkedList.add(student3);
//测试addByOrder方法
singleLinkedList.addByOrder(student1);
singleLinkedList.addByOrder(student3);
singleLinkedList.addByOrder(student4);
singleLinkedList.addByOrder(student2);
singleLinkedList.showLinkedList();
//测试添加重复编号元素
// singleLinkedList.addByOrder(student5);
//测试删除功能
// singleLinkedList.delete(student3);
//测试修改功能
singleLinkedList.update(student5);
singleLinkedList.showLinkedList();
}
}
class SingleLinkedList {
StudentNode head = new StudentNode(0, "");
//1.将新添加的元素直接插入到链表的最后
// public void add(StudentNode newStudentNode){
// StudentNode temp = head;
// while(true){
// if(temp.next==null){
// break;
// }
// temp=temp.next;
// }
// temp.next=newStudentNode;
// }
//2.将新添加的元素按照编号插入链表
public void addByOrder(StudentNode newStudentNode) {
StudentNode temp = head;
while (true) {
if (temp.getNo() == newStudentNode.getNo()) {
System.out.printf("该编号%d已经存在,无法向链表添加该元素!\n", temp.getNo());
return;
}
if (temp.next == null) {
break;
}
if (temp.next.getNo() > newStudentNode.getNo()) {
break;
}
temp = temp.next;
}
newStudentNode.next = temp.next;
temp.next = newStudentNode;
}
//3.删除链表元素
public void delete(StudentNode studentNode) {
StudentNode temp = head;
while (true) {
if (temp.next == null) {
System.out.println("该链表不存在该元素!无法删除该元素!");
return;
}
if (temp.next.getNo() == studentNode.getNo()) {
break;
}
temp = temp.next;
}
temp.next = temp.next.next;
}
//4.修改链表元素
public void update(StudentNode studentNode) {
StudentNode temp = head;
while (true) {
if (temp.getNo() == studentNode.getNo()) {
break;
}
if (temp.next == null) {
System.out.println("该链表不存在该元素!无法修改该元素!");
return;
}
temp = temp.next;
}
temp.setName(studentNode.getName());
}
//5.遍历链表
public void showLinkedList() {
StudentNode temp = head;
if (head.next == null) {
System.out.println("链表为空!");
return;
}
while (true) {
if (temp.next == null) {
break;
} else {
System.out.println(temp.next);
}
temp = temp.next;
}
}
}
class StudentNode {
private int no;
private String name;
StudentNode next;
public StudentNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}
寻找到标志为flag版
class SingleLinkedList {
StudentNode head = new StudentNode(0, "");
//1.将新添加的元素直接插入到链表的最后
// public void add(StudentNode newStudentNode){
// StudentNode temp = head;
// while(true){
// if(temp.next==null){
// break;
// }
// temp=temp.next;
// }
// temp.next=newStudentNode;
// }
//2.将新添加的元素按照编号插入链表
public void addByOrder(StudentNode newStudentNode) {
StudentNode temp = head;
boolean flag = false;
while (true) {
if (temp.getNo() == newStudentNode.getNo()) {
break;
}
if (temp.next == null) {
flag = true;
break;
}
if (temp.next.getNo() > newStudentNode.getNo()) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
newStudentNode.next = temp.next;
temp.next = newStudentNode;
} else {
System.out.printf("该编号%d已经存在,无法向链表添加该元素!\n", temp.getNo());
}
}
//3.删除链表元素
public void delete(StudentNode studentNode) {
StudentNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.getNo() == studentNode.getNo()) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.println("该链表不存在该元素!无法删除该元素!");
}
}
//4.修改链表元素
public void update(StudentNode studentNode) {
StudentNode temp = head;
boolean flag = false;
while (true) {
if (temp.getNo() == studentNode.getNo()) {
flag = true;
break;
}
if (temp.next == null) {
break;
}
temp = temp.next;
}
if (flag) {
temp.setName(studentNode.getName());
} else {
System.out.println("该链表不存在该元素!无法修改该元素!");
}
}
//5.遍历链表
public void showLinkedList() {
StudentNode temp = head;
if (head.next == null) {
System.out.println("链表为空!");
return;
}
while (true) {
if (temp.next == null) {
break;
} else {
System.out.println(temp.next);
}
temp = temp.next;
}
}
}
class StudentNode {
private int no;
private String name;
StudentNode next;
public StudentNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}