I genius official Free Tutorial 37: One-way list structure of Java data structure

Source: Internet
Author: User

one-way linked list of data structuresFor 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
Instance code:/** * Onewaylinked class * Demonstrates the implementation of the data structure of a doubly linked list * @author Genius Federation-Yukun */public class Onewaylinked {//For storing the first node of the linked list private node fi RST = null;//is used to store the last node of the linked list private node end = null;//used to store the collection length private int size = 0;//method to add elements public void Add (Object obj) {/ /Create Node object nodes node = new node ();//node stores the added element node.element = obj;//Determines whether the first node is nullif (primary = = NULL) {//First node is the first time the element is added. = node;//Sets the latter node of the first node to its own first.next = node;} Determines whether the last node is nullif (final = null) {/* * If the last node is also null * the address of the same node object is stored by first and first */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) {//by the subscript) {//first determine if the incoming subscript exceeds the length if (Index < size) {/* * * Declare a variable of node type Tagnode, and set the * Indicates looking for the time from the first node to find */node Tagnode = first;for (int i = 0; i < index; i++) {//Gets the next node, equivalent to I+1tagnode = Tagnode.next;} Gets the element found in the 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 {///self-type variable, used to store the latter node, a variable of type next;//object, used to store the element object E Lement;}} /** * Onewaylinkedtest class * For testing doubly linked list * @author Genius Federation-Yukun */public class Onewaylinkedtest {public static void main (string[] A RGS) {//Create a double-line linked list of objects onewaylinked TWL = new onewaylinked ();//Add values to the list twl.add (1); Twl.add (2); Twl.add (3); Twl.add (4);// Gets the value of the subscript 2 object element = Twl.get (2);//output value System.out.println (element);}} Run Result: 3

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

I genius official Free Tutorial 37: One-way list structure of Java data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.