复制可以直接用!!!
链表增删查改方法 :
class OperationalListSolution{
public twoDireNode list = null;
public OperationalListSolution(twoDireNode list) {
this.list = list;
}
public int get(int index) {
twoDireNode p = list;
for(int i=1;i<index;i++){
p=p.next;
}
return p.val;
}
public void addAtHead(int val) {
twoDireNode head = new twoDireNode(val);
head.next = list;
//返回新头节点
this.list = head;
}
public void addAtTail(int val) {
twoDireNode head = list;//存储头节点
twoDireNode tail = new twoDireNode(val);//新建尾节点
twoDireNode p = list;//指针
while(p.next!=null){
p=p.next;
}
//出循环时p时指向tail
//当前的最后一个后面添加一个尾节点
p.next = tail;
tail.next = null;
}
public void addAtIndex(int index, int val) {
if(index==1){//如果index=1直接插入头部
addAtHead(val);
return;
}
twoDireNode p = list;
for(int i=1;i<index-1;i++){
p=p.next;
}
//出循环时p指向第index-1个节点
twoDireNode newNode = new twoDireNode(val);
newNode.next = p.next;
p.next = newNode;
}
public void deleteAtIndex(int index) {
if(index==1){
twoDireNode newHead = list;
list = list.next;
newHead.next = null;
}
twoDireNode p = list;
for(int i=1;i<index-1;i++){
p=p.next;
}//循环出来p指向第index-1个节点
twoDireNode temp = p.next;//temp存放要删除的节点
p.next = temp.next;
temp.next = null;
}
}
class twoDireNode {
int val;
twoDireNode next;
twoDireNode pre;
twoDireNode(int x) { val = x; }
}
测试:
package com.example.Algorithm;
/**
* @ About:
* @ Author: Garson
* @ Time: 2022/9/23
**/
@SuppressWarnings("all")
public class OperationalList {
public static void main(String[] args) {
System.out.println("初始化链表:1 2 3");
twoDireNode list = new twoDireNode(1);
list.next=new twoDireNode(2);
list.next.next=new twoDireNode(3);
OperationalListSolution operationalListSolution = new OperationalListSolution(list);
//输出第二个节点
System.out.println("输出第二个节点: "+operationalListSolution.get(2));
//加入新头节点9并输出
operationalListSolution.addAtHead(9);
System.out.print("加入新头节点9: ");
int i=1;
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//加一个尾部
operationalListSolution.addAtTail(4);
System.out.print("加入新尾节点4: ");
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//随机插
System.out.print("随机增(10加到2index)");
operationalListSolution.addAtIndex(2,10);
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//随机插
System.out.print("随机增(100加到6index)");
operationalListSolution.addAtIndex(6,100);
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//随机插
System.out.print("随机增(1000加到1index)");
operationalListSolution.addAtIndex(1,1000);
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//随机删
System.out.print("随机删除第2个元素: ");
operationalListSolution.deleteAtIndex(2);
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
i=1;
System.out.println();
//随机删
System.out.print("随机删除第1个元素: ");
operationalListSolution.deleteAtIndex(1);
while(true){
try {
System.out.print(operationalListSolution.get(i)+" ");
i++;
} catch (Exception e) {
break;
}
}
}
}