Overview of single linked list
Single linked list is a kind of data structure with chain access. It uses a set of storage units with arbitrary addresses to store data elements in a linear table. The data in the linked list is represented by nodes. Each node is composed of elements (mapping of data elements) and pointers (indicating the storage location of successor elements). Elements are storage units for storing data, and pointers are address data connecting each node.
The Realization Principle of Single Chain List
In a single linked list, we define a node Node that consists of two parts: metadata and the address of the next node. Nodes and nodes are connected by next (the address of the next node), thus forming a chain structure. We can easily traverse from the head node to the last node. This structure is somewhat like an iron lock.
Single linked list operation (java implementation)
Node classes (need to be defined in two ways)
class Node{ Object data;//Data in linked list Node next;//Index of Next Node public Node(Object data) { this.data = data;//Extracted elements } } Node head;//Head Node of Link List
class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }
Function of adding node objects to linked list objects
/* * Adding Nodes */ public void add(Object data) { // Building Node Objects Node node = new Node(data); // Determine whether the list is empty, and if it is empty, the header node is empty. if (head == null) { head = node; // If it is not empty, the function is executed to determine the next node. } else { Node arr = listNode(); arr.next = node; } } private Node listNode() { // Traveling from scratch // Define a temporary variable that equals the head Node temp = head; // Continue traversing if it is not empty while (temp != null) { // If the next node is empty, the traversal is interrupted if (temp.next == null) { break; } // Go through the next temp = temp.next; } return temp; }
Delete specified elements from linked list objects
/* * delete */ public void delete(int index) { if (index < 0) { throw new IllegalStateException("invalid index"); } Node p = head; int i = 0; while (i < index-1 && p != null) { p = p.next; i++; } if(p == null) { throw new IllegalStateException("Invalid linked list"); } p.next = p.next.next; }
Insert the specified element into the list
/* * Insert Node */ public void insert(int value, int index) { Node p = head; int i = 0; while (i < index) { i++; } Node temp = new Node(value); temp.next = p.next; p.next = temp; }
Modify the specified element to the list
/* * Modifying Nodes */ public void update(int data, int index) { if (index<0) { throw new IllegalStateException("Invalid linked list"); } int i = 0; Node p = head; while (i < index) { p = p.next; i++; } p.data = data; }
Query the specified elements in the linked list
/* * Find Elements */ public Object find(int index) { int i = 0; Node p = head; while (i < index) { i++; p = p.next; } System.out.println(p.data); return p.data; }
Increase the printer system (this is mainly for the convenience of looking at the linked list structure)
/* * Define print function */ public void print() { Node p = head; while (p != null) { System.out.print(p.data + "->"); p = p.next; } }
Full code to implement the function:
//Define node objects class Node { Object data;// Data in linked list Node next;// Index of Next Node public Node(Object data) { this.data = data;// Extracted elements } } public class SingleList { Node head;// Head Node of Link List /* * Define print function */ public void print() { Node p = head; while (p != null) { System.out.print(p.data + "->"); p = p.next; } } /* * Adding Nodes */ public void add(Object data) { // Building Node Objects Node node = new Node(data); // Determine whether the list is empty, and if it is empty, the header node is empty. if (head == null) { head = node; // If it is not empty, the function is executed to determine the next node. } else { Node arr = listNode(); arr.next = node; } } private Node listNode() { // Traveling from scratch // Define a temporary variable that equals the head Node temp = head; // Continue traversing if it is not empty while (temp != null) { // If the next node is empty, the traversal is interrupted if (temp.next == null) { break; } // Go through the next temp = temp.next; } return temp; } /* * delete */ public void delete(int index) { if (index < 0) { throw new IllegalStateException("invalid index"); } Node p = head; int i = 0; while (i < index - 1 && p != null) { p = p.next; i++; } if (p == null) { throw new IllegalStateException("Invalid linked list"); } p.next = p.next.next; } /* * Insert Node */ public void insert(int value, int index) { Node p = head; int i = 0; while (i < index) { i++; } Node temp = new Node(value); temp.next = p.next; p.next = temp; } /* * Modifying Nodes */ public void update(int data, int index) { if (index<0) { throw new IllegalStateException("Invalid linked list"); } int i = 0; Node p = head; while (i < index) { p = p.next; i++; } p.data = data; } /* * Find Elements */ public Object find(int index) { int i = 0; Node p = head; while (i < index) { i++; p = p.next; } System.out.println(p.data); return p.data; } }
Examples of test objects
public class TestSingleList { public static void main(String[] args) { SingleList list = new SingleList(); list.add(10000); list.add(10010); list.add(10086); list.add(12580); list.add(12306); list.delete(2); list.insert(12586, 1); list.update(12345, 4); list.find(4); list.print(); } }