设置节点类
public class SingleLinkedList {
/**
* 节点类
*/
private static class Move{
//当前节点的值
int value;
//下一个节点
Move next;
public Move(int value, Move next){
this.value=value;
this.next = next;
}
}
}
在第一个添加一个节点
/**
* java实现单链表
*/
public class SingleLinkedList {
// 头节点
private Move head = null;
/**
* 节点添加到第一个
* @param value 节点的值
*/
public void addFirst(int value){
//判断是否为空链表
/* if(head != null){
head = new Move(value, head);
}
else {
//头节点
head = new Move(value, null);
}*/
head = new Move(value, head); //new Move(value, head); 每次new都会分配一个地址,head不是覆盖关系
// System.out.println("添加节点的地址:"+head +" 插入值:"+head.value );
}
}
在链表后添加一个节点
/**
* java实现单链表
*/
public class SingleLinkedList {
// 头节点
private Move head = null;
/**
* 节点添加到最后第一个
* @param value 节点的值
*/
private void addLast(int value){
// 插入的节点
Move interposition = new Move(value,null);
// 为空链表
if(head == null){
head = interposition;
}
// 非空链表
else {
Move move = head; //移动指针
//有下一个节点
while (move.next != null){
move = move.next;
}
//插入节点
move.next = interposition;
}
}
}
根据索引寻找节点
/**
* 根据索引寻找节点
* @param index
* @return
*/
private Move indexNode(int index){
int i = 0; //索引
for(Move move = head; move != null; move = move.next,i++){
if(index == i){
return move;
}
}
return null;
}
全部代码
/**
* java实现单链表
*/
public class SingleLinkedList {
// 头节点
private Move head = null;
/**
* 节点类
*/
private static class Move{
//当前节点的值
int value;
//下一个节点
Move next;
public Move(int value, Move next){
this.value=value;
this.next = next;
}
}
/**
* 节点添加到第一个
* @param value 节点的值
*/
public void addFirst(int value){
//判断是否为空链表
/* if(head != null){
head = new Move(value, head);
}
else {
//头节点
head = new Move(value, null);
}*/
head = new Move(value, head); //new Move(value, head); 每次new都会分配一个地址,head不是覆盖关系
// System.out.println("添加节点的地址:"+head +" 插入值:"+head.value );
}
/**
* 节点添加到最后第一个
* @param value 节点的值
*/
private void addLast(int value){
// 插入的节点
Move interposition = new Move(value,null);
// 为空链表
if(head == null){
head = interposition;
}
// 非空链表
else {
Move move = head; //移动指针
//有下一个节点
while (move.next != null){
move = move.next;
}
//插入节点
move.next = interposition;
}
}
/**
* 根据索引寻找节点
* @param index
* @return
*/
private Move indexNode(int index){
int i = 0; //索引
for(Move move = head; move != null; move = move.next,i++){
if(index == i){
return move;
}
}
return null;
}
/**
* 输出索引后的节点
*/
private int getValue(int index){
Move move = indexNode(index);
if(move == null){
throw IllegalIndex(index);
}
System.out.println("索引"+index+"的节点值为:"+move.value);
return move.value;
}
/**
* 异常
* @param index
* @return
*/
private IllegalArgumentException IllegalIndex(int index) {
return new IllegalArgumentException(
String.format("此索引不合法:index = "+ index)
);
}
/**
* 插入节点
* @param index 位置
* @param value 插入值
*/
private void addNode(int index, int value){
if(index == 0){
head = new Move(value, head);
return;
}
//插入前index-1位置的节点
Move move = indexNode(index-1);
if(move == null){
throw IllegalIndex(index);
}
Move interposition = new Move(value, move.next);
move.next = interposition;
System.out.println("添加成功");
}
/**
* 删除节点
* @param index 位置
*/
private void remove(int index){
//链表为空
if(head == null){
throw IllegalIndex(index);
}
//删除节点前的一个节点
Move move = indexNode(index-1);
if(move == null){
throw IllegalIndex(index);
}
if(move.next == null){
throw IllegalIndex(index);
}
// 删除节点
Move removeNode = move.next;
move.next = removeNode.next;
// removeNode = null;
System.out.println("删除成功");
}
/**
* 遍历链表
*/
private void traversal(){
//指针
Move pointer = head;
while (pointer != null){
System.out.print(pointer.value+" ");
pointer = pointer.next;
}
/* for(Move move = head; move != null; move = move.next){
System.out.println(move.value);
}*/
}
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addFirst(1);
singleLinkedList.addFirst(2);
singleLinkedList.addFirst(3);
singleLinkedList.addFirst(4);
//遍历链表
singleLinkedList.traversal();
System.out.println("");
singleLinkedList.addLast(5);
singleLinkedList.addLast(6);
singleLinkedList.addLast(7);
singleLinkedList.addLast(8);
singleLinkedList.addLast(9);
//遍历链表
singleLinkedList.traversal();
System.out.println("");
//根据索引查找节点
singleLinkedList.getValue(5);
singleLinkedList.addNode(9,10);
singleLinkedList.remove(10);
}
}