Home >Java >javaTutorial >How 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

王林
王林forward
2019-11-28 14:24:272924browse

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.net. If there is any infringement, please contact admin@php.cn delete