# ＜数据结构与算法＞Java代码实现单向链表-代码实现

### 数据结构-Java代码实现单向链表-代码实现:

public class SingleLinkedList {

public PersonNode head = new PersonNode();//头结点
public int SIZE = 0;//记录链表中目前的长度

public boolean isEmpty() {//判断链表是否为空
return SIZE == 0;
}

public void appendElement(PersonNode t) {//链表末端添加新元素t
for (int i = 0; i < SIZE; i++) {
currentPointer = currentPointer.next;
}
currentPointer.next = t;
SIZE++;
}

public void delElement(PersonNode t) {//删除链表上的t对象
for (int i = 0; i < SIZE; i++) {
if (currentPointer.next == t) {
currentPointer.next = t.next;
t.next = null;
SIZE--;
break;
}
currentPointer = currentPointer.next;
if (i == SIZE - 1) {
throw new RuntimeException("未找到该元素！");
}
}
}

public void replaceElement(PersonNode oldT, PersonNode newT) {//用newT替换链表上的oldT元素
for (int i = 0; i < SIZE; i++) {
if (currentPointer.next == oldT) {
if (i != SIZE - 2) {
newT.next = currentPointer.next.next;
}
currentPointer.next = newT;
break;
}
currentPointer = currentPointer.next;
if (i == SIZE - 1) {
throw new RuntimeException("未找到该元素！");
}
}
}

public void insertElement(PersonNode t, int place) {//将对象t插入在place位置上，即插在原place-1 和 place位置之间
for (int i = 0; i < place - 1; i++) {
currentPointer = currentPointer.next;
}
t.next = currentPointer.next;
currentPointer.next = t;
SIZE++;
}

public void SearchElement(PersonNode t) {//搜索链表上的t对象，若有则返回t对象的详细信息
for (int i = 0; i < SIZE; i++) {
if (currentPointer.equals(t)) {
System.out.println("已找到，其详细信息和位置为：");
System.out.println(i + ". " + currentPointer);
break;
}
if (currentPointer.next != null) {
currentPointer = currentPointer.next;
}
if (i == SIZE - 1) {
throw new RuntimeException("未找到该元素！");
}
}
}

System.out.println("\n链表当前成员有：");
for (int i = 0; i < SIZE; i++) {
if (currentPointer.next != null) {
currentPointer = currentPointer.next;
}
System.out.println((i + 1) + ". " + currentPointer);
}
}

}

class PersonNode {
public int age;
public String name;
public PersonNode next;

public PersonNode(int age, String name) {
this.age = age;
this.name = name;
}

public PersonNode() {
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PersonNode that = (PersonNode) o;
if (age != that.age) return false;
if (!name.equals(that.name)) return false;
return Objects.equals(next, that.next);
}

}



class Test {
public static void main(String[] args) {
PersonNode p1 = new PersonNode(11, "lzd");
PersonNode p2 = new PersonNode(19, "wgl");
PersonNode p3 = new PersonNode(26, "qrx");
PersonNode p4 = new PersonNode(56, "cy");
PersonNode p5 = new PersonNode(29, "yhf");
PersonNode p6 = new PersonNode(28, "zzj");

l.appendElement(p1);
l.appendElement(p2);
l.appendElement(p3);
l.appendElement(p4);

l.delElement(p2);

l.replaceElement(p4, p5);

l.appendElement(p6);

l.insertElement(p4, 3);

l.SearchElement(p1);
}
}


链表当前成员有：
1. PersonNode{age=11, name='lzd'}
2. PersonNode{age=19, name='wgl'}
3. PersonNode{age=26, name='qrx'}
4. PersonNode{age=56, name='cy'}

1. PersonNode{age=11, name='lzd'}
2. PersonNode{age=26, name='qrx'}
3. PersonNode{age=56, name='cy'}

1. PersonNode{age=11, name='lzd'}
2. PersonNode{age=26, name='qrx'}
3. PersonNode{age=29, name='yhf'}

1. PersonNode{age=11, name='lzd'}
2. PersonNode{age=26, name='qrx'}
3. PersonNode{age=29, name='yhf'}
4. PersonNode{age=28, name='zzj'}

1. PersonNode{age=11, name='lzd'}
2. PersonNode{age=26, name='qrx'}
3. PersonNode{age=56, name='cy'}
4. PersonNode{age=29, name='yhf'}
5. PersonNode{age=28, name='zzj'}

1. PersonNode{age=11, name='lzd'}

Process finished with exit code 0



01-19 373
08-26
02-17 279
08-26 145
06-21 3184
10-26 1868
11-06 1万+
08-09 2490
10-28 1521
01-09 850