Single linked table 1 (data structure)

Keywords: Java data structure Singly Linked List

Single linked list

1. Definition of linked list

Chain storage structure of linear table: the location of data elements (nodes) in linear table in memory is arbitrary, that is, logically adjacent data elements are not necessarily adjacent in physical location.

Related terms:

Nodes: storage images of data elements. It consists of data field and pointer field.
Data field: the field that stores data element information is called data field.
Pointer field: the field that stores the direct and subsequent storage locations is called pointer field.

Head node: the first node of the linked list.
Tail node: the last node in the linked list. The pointer field of the tail node is empty.
Header pointer: pointer to the header node to save the address of the header node.

As follows:

2. Linked list operation

2.1 define a node

Because the Node consists of two parts, data field and pointer field. Therefore, a Node class is defined, which contains two parts of instance variables. value represents the data field and next represents the pointer field.
Node class:

public class Node {
    //Instance variable
    private int value;
    private Node next;

    //Construction method
    public Node(int value, Node next) {
        this.value = value;
        this.next = next; //Address of the next node
    }

    public int getValue() {
        return value;
    }

    public Node getNext() {
        return next;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

2.2 define a linked list

Because you need a header pointer to mark the starting position of the linked list, add an instance variable head in the SingleLink class;
singleLink class:

public class SingleLink {
       private Node head;//Used to mark the starting position of the linked list
}

2.3 adding linked list header

Add header addHead
1) You need to create a new node, and the next field of the new node points to the location of the original first node.
2) The header pointer should point to the new node.
3) The same applies when the linked list is empty.

The code is as follows:

public void addHead(int value) {
        //next of new node = original head position
       Node newNode = new Node(value,null);
       newNode.setNext(head);
       
       head = newnNode;//Update new header node location
    }

2.4 adding at the end of the linked list

Add tail operation addTail
1) If the linked list is empty, the header pointer directly points to the new node.
2) If the linked list is not empty, traverse the linked list (create a node p to traverse the linked list), find the tail node, and make the next field of the tail node point to the new node to be inserted.

The code is as follows:

 public void addTail(int value) {
        Node newNode = new Node(value, null);
        //The linked list is empty
        if (head == null) {
            head = newNode;
            return;
        }
		//The linked list is not empty. Traverse the linked list and find the tail node
        Node p = head;
        while (p.getNext() != null) {
            p = p.getNext();
        }

        p.setNext(newNode);
    }

2.5 deletion of linked list header

Linked list header deletion removeHead
1) If the linked list is empty, return directly without operation
2) The linked list is not empty. You only need to point the head pointer to the next node.

The code is as follows:

 public void removeHead() {
   		//The linked list is empty
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }
        //Linked list is not empty
        head = head.getNext();
    }

2.6 deleting the tail of the linked list

Remove tail of linked list
1) If the linked list is empty, do not operate and return directly.
2) The linked list has only one node. Directly set the head to null.
3) The linked list has multiple nodes. Traverse the linked list. If the next node of the current node is empty (i.e. the tail node), set the next field of the current node to null and delete the tail node successfully.

    public void removeTail() {
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }


        //Only one node
        if (head.getNext() == null) {
            head = null;
            return;
        }

        //Greater than or equal to two nodes
        Node p = head;
        while (p.getNext() != null) {
            p = p.getNext();
            if (p.getNext().getNext() == null) {
                p.setNext(null);
            }
        }
    }

2.7 deleting a specific node

Delete a specific node operation removeValue
1) If the linked list is empty, it will be returned directly.
2) If the first node is the node to be deleted, call the header deletion method removeHead.
3) If there is only one node and it is not the node to be deleted, it means that there is no such node in the linked list. Return.
4) There are two or more nodes in the linked list. Traverse the linked list. If the value value of the next node of the current node is the node to be deleted, set the next field of the current node as the next node of the next node.

public void removeValue(int value) {
        //The linked list is empty
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }
        //Consider that the first node is the node to be deleted
        if (head.getValue() == value) {
            removeHead();
            return;
        }

        //Only one node
        if (head.getNext() == null && head.getValue() != value) {
            System.out.println("There is no such node");
            return;
        }

        //Greater than or equal to two nodes
        Node p = head;
        while (p.getNext().getValue() != value) {
            p = p.getNext();
        }
        if (p.getNext() == null) {
            System.out.println("There is no such node");
        } else {
            p.setNext(p.getNext().getNext());
        }
    }

2.8 find specific nodes

public boolean comtains(int value) {
        if (head == null) {
            System.out.println("The linked list is empty");
            return false;
        }

        for (Node p = head; p != null; p = p.getNext()) {
            if (p.getValue() == value) {
                return true;
            }
        }
        return false;
    }

2.9 print value of linked list node

public void show() {
        for (Node p = head;p != null;p = p.getNext()) {
            System.out.print(p.getValue() + " ");
        }
        System.out.println();
    }

3. Source code

List interface:

public interface List {
    void addHead(int value);
    void addTail(int value);
    void removeHead();
    void removeTail();
    void removeValue(int value);
    boolean comtains(int value);
}

Node class:

public class Node {
    private int value;
    private Node next;

    public Node(int value, Node next) {
        this.value = value;
        this.next = next; //Address of the next node
    }

    public int getValue() {
        return value;
    }

    public Node getNext() {
        return next;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

SingleLink class:

public class SingleLink implements List {//Unidirectional linked list
    private Node head;//Used to mark the starting position of the linked list


    //Header addition
    @Override
    public void addHead(int value) {
        //next of new node = original head position
//        Node newnode = new Node(value,null);
//        newnode.setNext(head);
        Node newnNode = new Node(value, head);

        head = newnNode;//Update new header node location
    }

    //Tail addition
    @Override
    public void addTail(int value) {
        Node newNode = new Node(value, null);
        if (head == null) {
            head = newNode;
            return;
        }

        Node p = head;
        while (p.getNext() != null) {
            p = p.getNext();
        }

        p.setNext(newNode);
    }

    //Delete header
    @Override
    public void removeHead() {
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }
        //>=1
        head = head.getNext();
    }

    //Delete tail
    @Override
    public void removeTail() {
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }


        //Only one node
        if (head.getNext() == null) {
            head = null;
            return;
        }

        //Greater than or equal to two nodes
        Node p = head;
        while (p.getNext() != null) {
            p = p.getNext();
            if (p.getNext().getNext() == null) {
                p.setNext(null);
            }
        }
    }

    //Delete a specific node
    @Override
    public void removeValue(int value) {
        //The linked list is empty
        if (head == null) {
            System.out.println("The linked list is empty");
            return;
        }
        //Consider that the first node is the node to be deleted
        if (head.getValue() == value) {
            removeHead();
            return;
        }

        //Only one node
        if (head.getNext() == null && head.getValue() != value) {
            System.out.println("There is no such node");
            return;
        }

        //Greater than or equal to two nodes
        Node p = head;
        while (p.getNext().getValue() != value) {
            p = p.getNext();
        }
        if (p.getNext() == null) {
            System.out.println("There is no such node");
        } else {
            p.setNext(p.getNext().getNext());
        }
    }

    //Find a specific node
    @Override
    public boolean comtains(int value) {
        if (head == null) {
            System.out.println("The linked list is empty");
            return false;
        }

        for (Node p = head; p != null; p = p.getNext()) {
            if (p.getValue() == value) {
                return true;
            }
        }
        return false;
    }

    //Print
    public void show() {
        for (Node p = head;p != null;p = p.getNext()) {
            System.out.print(p.getValue() + " ");
        }
        System.out.println();
    }
}

TestDemo test class:

public class TestDemo {
    public static void main(String[] args) {
        SingleLink singleLink = new SingleLink();
        singleLink.addHead(5);
        singleLink.addHead(4);
        singleLink.addHead(3);
        singleLink.addHead(2);
        singleLink.addHead(1);
        singleLink.show();
        singleLink.addTail(6);
        singleLink.show();
        singleLink.removeHead();
        singleLink.show();
        singleLink.removeTail();
        singleLink.show();
        singleLink.removeValue(3);
        singleLink.show();
        if (singleLink.comtains(5)) {
            System.out.println("There is this node");
        }else {
            System.out.println("There is no such node");
        }
    }
}

4. Demonstration of operation results

Posted by kansaschuck on Fri, 26 Nov 2021 18:40:14 -0800