[Java data structure] 2.2 simple implementation of two-way linked list

Source: Internet
Author: User

The linked list is organized into two types: ArrayList and LinkList. ArrayList is actually a linked list using arrays. It is more suitable for determining the linked list size or less performing addition or deletion operations on the linked list. At the same time, the access time to each linked list node is constant; the LinkList uses a List to implement the linked List, which is more suitable for situations where you need to perform operations on the linked List frequently. The access time to the linked List node is related to the length of the linked List O (N ).
In addition, the implementation form can be divided into two methods: direct expression and Iterator iteration mode. The direct implementation method is similar to the writing method in C/C ++. When using Iterator, java must be implemented. the Iterable interface in the lan, or you can define your own Iterator method in the linked list. This will be introduced in the following blog:

Direct mode:

Package com. ds. link; public class DoubleLink <T> {/*** Node <AnyType> class defines the structure of nodes in a two-way linked list. It is a private class, * its attributes and constructor are both public. In this way, its parent class can directly access its attributes. * The external class does not know the existence of the Node class. * @ Author ZHB ** @ param <T> type * @ param Data is the Data in the Node * @ param pre refers to the previous Node * @ param next points to the next Node */ private class Node <T> {public Node <T> pre; public Node <T> next; public T data; public Node (T data, Node <T> pre, Node <T> next) {this. data = data; this. pre = pre; this. next = next;} public Node () {this. data = null; this. pre = null; this. next = null ;}/// The following are data members and methods of the DoubleLinkedList class private I Nt theSize; private Node <T> Header; private Node <T> Tail; /** constructor * we construct a two-way linked list with header and tail nodes. * Next Of the header node points to the tail node. * This is the pre-pointing node of the node. * The length of the chain table starts from 0.. */Public DoubleLink () {theSize = 0; Header = new Node <T> (null, null, null); Tail = new Node <T> (null, Header, null); Header. next = Tail;} public void add (T item) {Node <T> aNode = new Node <T> (item, null, null); Tail. pre. next = aNode; aNode. pre = Tail. pre; aNode. next = Tail; Tail. pre = aNode; theSize ++;} public boolean isEmpty () {return (this. theSize = 0);} public int size () {return this. theSize;} pu Blic T getInt (int index) {if (index> this. theSize-1 | index <0) throw new IndexOutOfBoundsException (); Node <T> current = Header. next; for (int I = 0; I <index; I ++) {current = current. next;} return current. data;} public void print () {Node <T> current = Header. next; while (current. next! = Null) {System. out. println (current. data. toString (); current = current. next ;}} public static void main (String [] args) {DoubleLink <String> dLink = new DoubleLink <String> (); dLink. add ("zhb"); dLink. add ("zzb"); dLink. add ("zmy"); dLink. add ("zzj"); System. out. println ("size:" + dLink. size (); System. out. println ("isEmpty?: "+ DLink. isEmpty (); System. out. println (" 3: "+ dLink. getInt (2); dLink. print ();}}

Running result:


size : 4isEmpty? : false3 : zmyzhbzzbzmyzzj



If you have any questions, please feel free to contact us!


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1288669

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.