Java实现链表各种操作(超详细)

复制可以直接用!!! 

链表增删查改方法 :

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;
            }
        }


    }
}

测试结果 :

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值