链表的创建、增删改查、注意点 和 全部代码
链表的JAVA实现
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e36dffcc0cef7150425ce4696ad3b887.png)
全部代码
package Linkedlist.Single;
import java.awt.*;
public class SingleLinkedList {
private int size;
private Node head;
private int currentIndex;
public SingleLinkedList() {
size = 0;
head = null;
currentIndex = 0;
}
public int getSize() {
return size;
}
public void addEnd(Object data){
if (head == null){
head = new Node(data);
}else {
head.addNodeEnd(data);
}
size++;
}
public void addStart(Object data){
Node newNode = new Node(data);
if (head == null){
newNode = head;
}else {
newNode.next = head;
head = newNode;
}
size++;
}
public void del(Object data){
if (head == null) return;
if (head.getData() == data){
head = head.next;
}else {
head.delNode(data);
}
size--;
}
public void print(){
if (head != null){
System.out.print(head.getData() + "->");
head.printNode();
System.out.println();
}
}
public boolean find(Object data){
if (head == null) return false;
if (head.getData() == data) {
return true;
} else {
return head.findNode(data);
}
}
public boolean update(Object oldData,Object newData){
if (head == null) return false;
if (head.getData() == oldData){
head.setData(newData);
return true;
}else {
return head.updateNode(oldData,newData);
}
}
public void insert(int index,Object data){
if (index < 0 || index > size) {
System.out.println("插入失败,下标越界或小于0");
return;
}
currentIndex = 0;
if (index == currentIndex){
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}else {
head.insertNode(index,data);
}
}
public void insertBack(int index, Object data){
if (index < 0 || index > size) {
System.out.println("插入失败,下标越界或小于0");
return;
}
currentIndex = 0;
if (index == currentIndex){
Node newNode = new Node(data);
newNode.next = head.next;
head.next = newNode;
}else {
head.insertNodeBack(index,data);
}
}
private class Node{
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public void addNodeEnd(Object data){
if (this.next == null){
this.next = new Node(data);
}else{
this.next.addNodeEnd(data);
}
}
public void delNode(Object data){
if (this.next != null){
if (this.next.data == data){
this.next = this.next.next;
}else {
this.next.delNode(data);
}
}
}
public void printNode(){
if (this.next != null){
System.out.print(this.next.data + "->");
this.next.printNode();
}
}
public boolean findNode(Object data){
if (this.next != null){
if (this.next.data == data){
return true;
}else{
return this.next.findNode(data);
}
}
return false;
}
public boolean updateNode(Object oldData,Object newData){
if (this.next != null){
if (this.next.data == oldData){
this.next.data = newData;
return true;
}else {
return this.next.updateNode(oldData,newData);
}
}
return false;
}
public void insertNode(int index, Object data){
currentIndex++;
if (index == currentIndex){
Node newNode = new Node(data);
newNode.next = this.next;
this.next = newNode;
}else {
this.next.insertNode(index,data);
}
}
public void insertNodeBack(int index, Object data){
currentIndex++;
if (index == currentIndex){
Node newNode = new Node(data);
newNode.next = this.next.next;
this.next.next = newNode;
}else {
this.next.insertNodeBack(index,data);
}
}
}
}