Linked lists (singly linked lists, doubly linked lists)

1. Basic introduction of linked list

   First, the linked list stores data in the form of nodes. Each node contains a data field (data) and a node field (next). The storage location of each node of the linked list in the computer is discontinuous and random. The advantage is data. Insertion and deletion are better, and the efficiency of finding data is not too good (because the linked list cannot read data randomly like static data, you must find the corresponding data in order).

 

 2. One-way linked list

  2.1. What is a singly linked list

   A one-way linked list is like a train. All nodes are connected in a row and the direction of the pointer is the same. That is, in addition to storing the original data, each node in the linked list also stores the storage address of the next node. As shown below:

 

 

 

 Node class code implementation

class SingleNode {
    public Object object; // Node data
    public SingleNode next; // Point to the next node

    public SingleNode() {

    }

    public SingleNode(Object object, SingleNode next) {
        this.object = object;
        this.next = next;

    }
}

  

 2.2. One-way linked list implementation (add, delete, modify, view) data

  

/**
 * Implementation of singly linked list
 */
public class SingleLinkList {
    private SingleNode first; // Define the head node
    private SingleNode last; // Define the tail node
    private int size; // Define the number of nodes in the linked list

    /**
     * Determine whether the linked list is empty
     * @return
     */
    public boolean isEmpty() {
        return first == null;
    }

    /**
     * Add elements at the head and tail of the linked list 
* Idea analysis: when the linked list is empty, it is equivalent to adding data at the head of the linked list, so assign the newly added node to the head node (tail node),
* when the linked list When it is not empty, it is equivalent to adding data at the end of the linked list. At this time, we are pointing the tail pointer to the newly added node (that is, last.next = node), and then assign the newly added node to the
* last node ( Ie last = node) */ public void add(Object object) { SingleNode node = new SingleNode(object, null); if (isEmpty()) { first = node; } else { last.next = node; } last = node; size++; } /** * Add elements at specified nodes * Idea analysis: First of all, we need to judge whether the added position meets the condition (index> 0 && index <size), then we need to find the element at the specified index position and the element at its previous position, so we use
* two pointers to Record the element at the index position and the element at the previous position, and finally put (before.next = node, node.next = temp), which is equivalent to pointing before to node, node pointing to the current node (temp). * @param index * @param object */ public void addElement(int index, Object object) { SingleNode node = new SingleNode(object, null); if (index > 0 && index < size) { SingleNode temp = first; SingleNode before = null; for (int i = 0; i < index; i++) { before = temp; temp = temp.next; } before.next = node; node.next = temp; size++; } } /** * Display the node elements in the linked list */ public void show() { if (!isEmpty()) { SingleNode current = first; while (current != null) { System.out.println(current.object); current = current.next; } } } /** * Get the elements of the specified node * Thinking analysis: define a pointer, loop to the specified position, and take out the element * @param index * @return */ public Object get(int index) { if (index > 0 && index < size) { if (!isEmpty()) { SingleNode temp = first; for (int i = 0; i < index; i++) { temp = temp.next; } return temp.object; } } return null; } /** * Delete the element of the specified node * Idea analysis: You need to define two pointers to record the node to be deleted and the previous node of the node to be deleted, and then put before.next = temp.next. * @param index */ public void remove(int index) { if (index > 0 && index < size) { if (!isEmpty()) { SingleNode temp = first; SingleNode beforeTemp = null; for (int i = 0; i < index; i++) { beforeTemp = temp; temp = temp.next; } beforeTemp.next = temp.next; } } } /** * Modify the specified element of the specified node * * @param index * @param object */ public void update(int index, Object object) { if (index > 0 && index < size) { if (!isEmpty()) { SingleNode temp = first; for (int i = 0; i < index; i++) { temp = temp.next; } temp.object = object; } } } /** * Reverse printing of linked lists
* Analysis of ideas: To take advantage of the advanced features of the stack, first put the elements on the stack, and then remove them. */ public void rePrint() { Stack<SingleNode> stack = new Stack<SingleNode>(); if (!isEmpty()) { SingleNode current = first; while (current != null) { stack.push(current); current = current.next; } while (stack.size() > 0) { System.out.println(stack.pop().object); } } } /** * Reverse singly linked list */ public void reversetList() { SingleNode current = first; SingleNode before = null; while (current != null) { last = before; before = current; current = current.next; before.next = last; } current = before; while (current != null) { System.out.println(current.object); current = current.next; } }

  

 

  

 

 

 

 

 

 

 

  

 

 

   

Guess you like

Origin www.cnblogs.com/xiaofuzi123456/p/12676819.html