I genius official free Tutorial 36: Bi-directional list structure of Java data structure

Source: Internet
Author: User

A doubly linked list of data structuresFor example: Existing two-way linked list twowaylinked 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 A and node C, Node C stores element 3 and Node B and node D, and Node D stores element 4 and node C. 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. Assign the previous node in E to Node B
3. Modify the next node in B to node E
4. Assign the next node in E to node C
5. Modify the previous node in c to node E
From the above process, there is no movement of the node position when inserting, so the efficiency is high; the removal process is reversed from the insertion process
Instance code:/** * Twowaylinked class * Demonstrates the implementation of the data structure of a doubly linked list * @author Genius Federation-Yukun */public class Twowaylinked {//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 first and last nodes of a node to their own first.pre = Node;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 null//a newly created node the previous node should be the last node of the previous node.pre = last;//Newly created node the next node stores itself Node.next = node;// At this point, the last node that was saved in the previous add element///so that its next node should be set to the currently newly created node Last.next = node;//and then set final to the current newly created 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 in the found nodeVegetarian 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 {//variable of its own type, used to store the variables of node pre;//itself of the previous nodes, used to store the latter node of the nodes NEX A variable of type t;//object that is used to store elements of the object element;}} /** * Twowaylinkedtest class * For testing doubly linked list * @author Genius Federation-Yukun */public class Twowaylinkedtest {public static void main (string[] A RGS) {//Create a double-line linked list of objects twowaylinked TWL = new twowaylinked ();//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 36: Bi-directional list structure of Java data structure

Related Article

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.