java单链表节点翻转_单链表Java实现

​ 链表中的结点是以结点来表示,单链表每一个结点有一个指针域和data域,物理位置不是连续的,逻辑上是连续的。

b3110d4fff577a38b67e47156381dafe.png

代码实现

class LinkedList<E> {
    private Node<E> head;
    private Integer size;

    public LinkedList(){
        head = new Node<>(null,null);
        size = 0;
    }

    //往链表末尾添加元素
    public void add(E e){
        Node<E> temp = head;
        Node<E> newNode = new Node<>(e,null);
        while (true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = newNode;
        size++;
    }

    //指定位置插入
    public boolean insert(E e,int index){
        if (index > size || index < 0){
            throw new IndexOutOfBoundsException();
        }
        Node<E> temp = head;
        Node<E> newNode = new Node<>(e,null);
        for (int i = 0; i < index; i++){
            temp = temp.next;
        }

        if (index != size){
            newNode.next = temp.next;
        }
        temp.next = newNode;
        size++;
        return true;
    }

    //获取链表长度
    public int size(){
        return size;
    }

    //移除元素
    public boolean remove(int index){
        if (index > size){
            throw new IndexOutOfBoundsException();
        }
        Node<E> temp = head;
        for (int i = 0; i < index; i++){
            temp = temp.next;
        }
        if (index == size){
            temp.next = null;
            return true;
        }
        temp.next = temp.next.next;
        size--;
        return true;
    }

    //展示所有元素
    public boolean show(){
        Node<E> temp = head;
        while (temp.next != null){
            System.out.println(temp.next.item);
            temp = temp.next;
        }
        return true;
    }
    public boolean isEmpty(){
        return size == 0;
    }

    //获取指定索引处的元素
    public E get(int index){
        if (isEmpty()){
            throw new IndexOutOfBoundsException("链表为空");
        }
        if (index > size){
            throw new IndexOutOfBoundsException();
        }
        Node<E> temp = head;
        for (int i = 0; i < index - 1; i++){
            temp = temp.next;
        }
        return (E) temp.next.item;
    }
    //节点
    private static class Node<E>{
        E item;
        Node<E> next;
        Node(E element,Node<E> next){
            this.item = element;
            this.next = next;
        }
    }
}

简单测试

public class LinkedListTest{
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("你好");
        linkedList.add("世界");
        linkedList.add("哈哈");
        linkedList.insert("!",2);
        linkedList.show();
        System.out.println("----------------");
        linkedList.remove(3);
        linkedList.show();
    }
}

运行结果

你好
世界
!
哈哈
----------------
你好
世界
!

欢迎关注我的微信公众号(秃头之路)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值