search
HomeJavajavaTutorialHow to implement basic operations (add, delete, check, modify) in Java linked lists

How to implement basic operations (add, delete, check, modify) in Java linked lists

The linked list is also a linear data structure. Unlike the array, the linked list is stored randomly in memory.

The following is a complete example covering the four operations of the linked list. There are a few points to note:

(1) Before adding, deleting, modifying, and checking, you need to check the following mark for boundary judgment;

(2) Adding a node named last can facilitate operations at the end of the linked list, eliminating the time complexity of finding the last node;

(3) When inserting an element into the linked list, we first find the prevNode of the previous node to be inserted, and then record the next of prevNode. When inserting, first point the next of prevNode to the node to be inserted, and then point the next of the node to be inserted. next points to the current next. This is slightly different from the operation in C;

(4) When deleting a node, use removedNode to record the return value of the deleted node, and don't forget to reduce the size by 1.

Recommended related free video tutorials: java free video tutorials

Operation examples are as follows:

public class MyLinkedList {
    //定义一个静态的内部类
    private static class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
        }
    }
 
    private Node head;
    private Node last;//为了方便尾部插入元素的操作
    private int size;//size表示链表的实际长度
 
    public void insert(int data, int index)throws Exception{
        if(index < 0 || index > size)
            throw new IndexOutOfBoundsException("超出链表节点范围!");
        Node insertedNode = new Node(data);
        if(size == 0){//插入第一个元素时元素个数为0
            head = insertedNode;
            last = insertedNode;
        }else if(size == index){//在链表的末尾插入
            last.next = insertedNode;
            last = insertedNode;
        }else{
            Node prevNode = get(index - 1);
            Node nextNode = prevNode.next;
            prevNode.next = insertedNode;
            insertedNode.next = nextNode;
        }
        size++;
    }
 
    public void update(int data, int index) throws Exception{
        if(index < 0 || index >= size)
            throw new IndexOutOfBoundsException("超出链表节点范围!");
        if(index == 0)
            head.data = data;
        else if(index == size - 1)
            last.data = data;
        else{
            Node temp = get(index);
            temp.data = data;
        }
    }
 
    public Node remove(int index) throws Exception {
        if(index < 0 || index >= size){
            throw new IndexOutOfBoundsException("超出链表节点范围!");
        }
        Node removedNode = null;//不给removedNode分配堆内存
        if(index == 0){
            removedNode = head;
            head = head.next;
        }
        else if(index == size - 1){
            //删除尾结点
            Node prevNode = get(index - 1);
            removedNode = prevNode.next;
            prevNode.next = null;
            last = prevNode;
        }
        else{
            Node prevNode = get(index - 1);
            Node nextNode = prevNode.next.next;
            removedNode = prevNode.next;
            prevNode.next = nextNode;
        }
        size--;
        return removedNode;
    }
 
 
 
    //查找链表元素
    public Node get(int index) throws Exception{
        if(index < 0 || index >= size){
            throw new IndexOutOfBoundsException("超出链表节点范围!");
        }
        Node temp = head;
        for(int i = 0; i < index; i++){
            temp = temp.next;
        }
//        size--;
        return temp;
    }
 
    //输出链表
    public void output(){
        Node temp = head;
        while(temp != null){
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
 
    public static void main(String[] args) throws Exception{
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.insert(3,0);
        myLinkedList.insert(7,1);
        myLinkedList.insert(9,2);
        myLinkedList.insert(5,3);
        myLinkedList.insert(6,1);
        myLinkedList.remove(0);
        myLinkedList.update(2,1);
        myLinkedList.output();
        System.out.println(myLinkedList.size);
    }
}

If you want to know more related tutorials, you can visit: javaDevelopmentIntroduction

The above is the detailed content of How to implement basic operations (add, delete, check, modify) in Java linked lists. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools