因为是复习,所以直接贴代码
package com.leolee.dataStructure.linkedList;
/**
* @ClassName Node
* @Description: 链表节点对象
* @Author LeoLee
* @Date 2020/9/14
* @Version V1.0
**/
public class Node {
public int no;
public String name;
public String nikeName;
public Node next;
public Node (int no, String name, String nikeName) {
this.no = no;
this.name = name;
this.nikeName = nikeName;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", name='" + name + '\'' +
", nikeName='" + nikeName + '\'' +
// ", next=" + next +
'}';
}
}
package com.leolee.dataStructure.linkedList;
/**
* @ClassName SingleLinkedList
* @Description: TODO
* @Author LeoLee
* @Date 2020/9/14
* @Version V1.0
**/
public class SingleLinkedList {
//初始化头节点
private Node headNode = new Node(0, "", "");
private int size = 0;
//在链表尾部插入节点
public void add (Node newNode) {
Node tempNode = headNode;
//遍历寻找最后的节点
while (true) {
if (tempNode.next == null) {
break;
}
tempNode = tempNode.next;
}
tempNode.next = newNode;
size++;
}
//按照顺序插入
public void addByOrder (Node newNode) {
Node tempNode = headNode;
boolean flag = false;//相同编号的节点是否已经存在
while (true) {
if (tempNode.next == null) {//说明当前节点为链表最后的节点
break;
}
if (tempNode.next.no > newNode.no) {//找到插入位置,在tempNode后面插入
break;
} else if (tempNode.next.no == newNode.no) {//已经存在该节点
flag = true;
break;
}
//以上条件不成立,后移当前节点,下一次循环开始
tempNode = tempNode.next;
}
if (flag) {
System.out.printf("添加的节点编号已经存在%d,不能添加\n", newNode.no);
} else {
newNode.next = tempNode.next;
tempNode.next = newNode;
size++;
}
}
public void update (Node newNode) {
//判断链表是否为空
if (headNode.next == null) {
System.out.println("链表为空");
return;
}
Node tempNode = headNode.next;
boolean flag = false;//是否找到要修改的节点(是否找到no一样的节点)
while (true) {
if (tempNode == null) {//最后一个节点的next为空,遍历结束了
break;
}
if (tempNode.no == newNode.no) {
flag = true;
break;
}
tempNode = tempNode.next;
}
if (flag) {
tempNode.name = newNode.name;
tempNode.nikeName = newNode.nikeName;
} else {
System.out.printf("没有找到对应需要修改的节点[%d]", newNode.no);
}
}
public void delete (int targetNo) {
Node tempNode = headNode;
boolean flag = false;//是否找到需要删除的节点的上一个节点
while (true) {
if (tempNode.next == null) {
System.out.println("没有找到链表中需要删除的目标节点");
break;
}
if (tempNode.next.no == targetNo) {
flag = true;
break;
}
tempNode = tempNode.next;
}
if (flag) {
tempNode.next = tempNode.next.next;
size--;
}
}
public static int size (Node head) {
Node currentNode = head.next;
int size = 0;
if (head.next == null) {
return 0;
}
while (currentNode != null) {
size++;
currentNode = currentNode.next;
}
return size;
}
public int size () {
return size;
}
//显示链表
public void list () {
if (headNode.next == null) {
System.out.println("链表为空");
return;
}
Node tempNode = headNode.next;
while (true) {
if (tempNode == null) {
break;
}
//输出节点信息
System.out.println(tempNode.toString());
//节点后移
tempNode = tempNode.next;
}
}
//反转链表
public void reversal (Node headNode) {
if (headNode.next == null || size(headNode) == 1) {
return;
}
Node currentNode = headNode.next;
Node tempHeadNode = new Node(0, "", "");
Node tempNode = null;
while (currentNode != null) {
tempNode = currentNode.next;
currentNode.next = tempHeadNode.next;
tempHeadNode.next = currentNode;
currentNode = tempNode;
}
headNode.next = tempHeadNode.next;
}
public Node getHeadNode() {
return headNode;
}
}