one-way linked list of data structures
For example: Existing two-way linked list onewaylinked stored 1,2,3,4 Four elements, then the collection object will have 4 nodes A, B, C, D, by the above structure can be known, Node A is stored in element 1 and Node B, node B is stored in element 2 and node C, Element 3 and Node d are stored in node C, and element 4 and null are stored in node D. If you now want to insert an element 5 in the middle of elements 2 and 3;
The process is as follows:
1. Creating a node E,e storage element 5
2. Modify the next node in B to node E
3. Assign the next node in E to node C
From the above process, there is no node position movement operation, so the efficiency is high; the removal process is the opposite of the insertion process; the difference from a doubly linked list is that the previous node is no longer stored in a node
Example code:/** * onewaylinked Class * demo bidirectional linked list implementation * @author Genius Federation - Yukun */public class OneWayLinked {// the first node for storing a list private node first = null;// the last node used to store the linked list private node last = null;// is used to store the collection length Private int size = 0;//method of adding elements Public void add (object obj) {//Create node object node node = new node ();//node stores the added element node.element = obj;//determines whether the first node is nullif (first == NULL) {//The first node for the description is the first time the element is added first = node;//sets the last node of the first node to its own first.next = node;} Determines whether the last node is Nullif (last == null) {/* * if the last node is also null * Last and first store the address of the same node object */last = node;} else{//if the last node is not a newly created node for null//, the next node stores itself node.next = node;//the last node that was saved at the end of the previous add element// So its next node should be set to the currently newly created node last.next = node;//and then set last to the currently newly created node Last = node;} Add elements, length plus 1size++;} /** * Gets the element */public object get (Int index) According to the subscript {//first determine if the incoming subscript exceeds the length if (index < size) {/* * Declare a node-type variable Tagnode and set it to first * to find the time to start looking for the first node */Node tagnode = first;for (int i = 0; i < index; i++) {// Gets the next node, equivalent to I+1tagnode = tagnode.next;} Gets the element in the found node return tagnode.element;} Returns Nullreturn null if the incoming subscript is greater than or equal to the length;} /** * Node Class (Private member inner Class) * * @author Genius Federation - Yukun */private class node {// a variable of its own type, used to store a variable of the latter node Node next;// object type for storing the element object element;}} /** * onewaylinkedtest class * for testing doubly linked lists * @author Genius Federation - Yukun */ Public class onewaylinkedtest {public static void main (String[] args) {//Create object onewaylinked twl = new onewaylinked () for double-line linked list,//Add value to list Twl.add (1); Twl.add (2); twl.adD (3); Twl.add (4);//Gets the value of subscript 2 object element = twl.get (2);//output value System.out.println (element);}} Run Result: 3
This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!
I genius official Free Tutorial 37: One-way list structure of Java data structure