java链表基本操作

主要通过编码实现单链表的基础操作:

1 从头插入节点

2 插入链表中指定位置的节点

3 根据指定的value来查找节点

4 根据指定的节点索引来查询节点

5 删除第一个节点

6 删除节点指定位置的值

7 删除指定索引的节点

8 打印列表信息

public class Node {
    public Node next;//指针域
    public long data;//数据域

    public Node(long data){
        this.data = data;
    }

    /**
     * 显示数据
     */
    public void show(){
        System.out.print(data + " ");
    }
}
class NodeApi {

    private Node firstNode;

    public NodeApi() {
        firstNode = null;
    }
 
/**
 * 从头开始插入
 * @param value
 */
public void insertFromBeginValue(long value){
   Node currentNode =  new Node(value);
   currentNode.next = firstNode;
   firstNode = currentNode;

}
/**
 * 基于循环遍历插入
 * 实现思路 1 从头开始交换 2 交互指针域 3 交互值域
 * @param value 指定节点位置的值
 * @param index 要插入的位置的索引
 */
private void insertNodeByPostiiton01(int index,long value){
    if (index<0){
        System.out.println("插入索引节点位置不合法!");
    }
    else {
       Node currentNode = firstNode;
       Node insertNode = new Node(value);
       while (currentNode!=null){
           currentNode = currentNode.next;
           index--;
       }
       insertNode.next = currentNode.next;//指针域
       currentNode.next = insertNode;//值域
    }

}
/**
 * 基于查询思路
 * 1 根据索引查找出当前索引对应的节点
 * 2 改变节点指针域
 * 3 改变节点值域
 * @param index
 * @param value
 */
private void insertNodeByPostiiton02(int index,long value){
    if (index<0){
        System.out.println("插入索引节点位置不合法!");
    }
    else {
        Node insertNode = new Node(value);
        Node indexNode = findNodeByIndex(index);
        insertNode.next = indexNode.next;//指针域
        indexNode.next = insertNode;//值域
    }
}
/**
 * 根据指定的值域查找节点
 * @param value
 * @return
 */
public Node findNodeByValue(long value){
    Node currentNode = new Node(value);
    //判断当前节点值和要查找的值是否一致
    while(currentNode.data!=value){
        if (currentNode.next == null) {
            System.out.println("对不起已经查询到最后,请确认你输入的是否正确");
            return null;
        }
        currentNode = currentNode.next;
    }
    return currentNode;
}
/**
 * 根据指定的节点索引查找节点
 * @param index
 * @return
 */
public Node findNodeByIndex(int index){
    Node currentNode = firstNode;
    int currentIndex=1;
    if (index<0){
        System.out.println("对不起你输入的查找索引参数有误!");
        return null;
    }
    else {
        while (currentNode != null && index > currentIndex) {
            currentIndex++;
            currentNode = currentNode.next;
        }
    }
    return currentNode;
}
/**
 * 查找第一个节点
 * @return
 */
public Node findFirstNode(){
    Node currentNode = firstNode;
    return currentNode;
}
/**
 * 删除第一个节点
 * @return
 */
public Node deleteFirstNode(){
    Node currentNode = firstNode;
    firstNode=currentNode.next;
    return currentNode;
}
/**
 * 根据指定的值 删除节点
 * @param value
 * @return
 */
public void deleteNodeByParam(long value){
    Node previousNode = firstNode;
    Node currentNode = firstNode;
    while (currentNode.data!=value){
        if (currentNode.next==null){
        }
        previousNode = currentNode;
        currentNode = currentNode.next;
    }
    //删除第一个节点
    if (currentNode==firstNode){
        firstNode = firstNode.next;
    }
    else {
        previousNode.next = currentNode.next;
    }
}
/**
 * 根据指定的索引 删除节点
 * @param index
 * @return
 */
public void deleteNodeByIndex(int index){
    // index =3;
    Node currentNode = firstNode;
    Node indexNode = findNodeByIndex(index);
    if (index<0){
        System.out.println("对不起删除的节点索引不合理..");
    }
    //删除的是第一个节点
    else if (index==1){
        firstNode = firstNode.next;
    }
    else{
        int len=1;
        while (currentNode.next!=null){
             if (index == ++len){
                 currentNode.next = currentNode.next.next;
                 return;
             }
             currentNode = currentNode.next;
        }


    }
}
/**
 * 显示列表数据
 */
public void showLinkList(Node node){
    Node currentNode = null;
    if (node==null){
       currentNode = firstNode;
    }
    else{
        currentNode = node;
    }
    while (currentNode!=null){
        currentNode.show();
        //修改指针
        currentNode = currentNode.next;
    }
    //换行
    System.out.println();

}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值