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

Source: Internet
Author: User

A doubly linked list of data structures

For 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

Example code:/** * twowaylinked Class  *  demo bidirectional linked list implementation  *  @author   Genius Federation  -  Yukun  */public class TwoWayLinked {//  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 that the element is added first = node;//sets the first and last nodes to their own first.pre = node; 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 the null//newly created node, the previous node should be the last node of the previous node.pre = last;//the newly created node the next node stores itself node.next =  node;//the last node that was saved at this point in the previous time when the element was added//so its next node should be set to the currently newly created node Last.next = node;//then sets last to the currently new node Last = node;} Add elements, length plus 1size++;} /** *  Gets the element  */public object get (Int index) based on the subscript  {//first determines whether 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 previous node node pre;//  its own type, for storing the latter node node next;//  Variable of type object, used to store element object element;}} /** * twowaylinkedtest class  *  for testing doubly linked lists  *  @author   Genius Federation  -  Yukun  */ Public class twowaylinkedtest {public static void main (STRING[]&NBsp;args)  {//Create the Object twowaylinked twl = new twowaylinked () of the double-line list,//Add the value to the 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 36: Bi-directional 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.