Java linked list

Source: Internet
Author: User

Write a data structure that everyone is familiar with: one-way linked list.

But first tell everyone a small secret, Java API has provided a one-way list of the class, you can directly to use, but the study of data structure course when you have already know, although the system will provide us with some commonly used data structure, but the custom can always bring a different sense of joy, and through their own writing is also more able to let us understand the process of implementation, and we can also write some more personalized methods as their own data structure. Here is mainly to introduce some of the common structure used in the method, as well as how the list is specific how to operate.

First, the advantage of a single-linked list relative to the queue is that the storage address is not contiguous, meaning that the elements in one of these locations do not have to be memory-operated on the other elements, greatly reducing the pressure on our computer. Go directly to the following:

First, you define a node class, as follows:

Java code

public class Node {          node next;//the next node refers to the          object obj;//node element public          node (object obj) {              this.obj=obj;          }  }

Then there is our LinkedList class, which first defines an empty list:

Node head=null;//creates an empty list, the head node.

Node last=head;//tail node

There are two ways to print a list, either recursively or by using a non-recursive method, as follows:

Java code

/** * Method of non-recursive printing elements */public void print (Node head) {while      (head!=null) {          System.out.println (head.obj);          head=head.next;//index back Shift      }  }  /**  * Recursive printing of linked list elements  */public void Printnode (Node head) {      if (head! =null) {          System.out.println (head.obj);          Node Node=head.next;          Printnode (node);//Recursive Call      }  }

The non-recursive method has a fatal flaw, and printing changes the position of the head node at the same time, so we should tend to use recursive methods.

Said so much, adding and deleting the official start of the change:

Adds an element to the linked list. Judge that a linked list has reached the end of the basis is that the next reference to the node is null, so to add a node to the end, the first to put the new node at the end, and then the end of the node to shift back, the specific operation process such as:

The code is as follows:

Java code

/**  * method to add elements to the specified list  * @param obj   inserted element */public void Add (Object obj) {      node node=new node (obj);//New node       if (head==null) {//If the list is empty          head = node;      } else{          last.next=node;//First put the new node in the last      }      last=node;//and then move the last node backwards.  


Inserts an element. To insert a new element first to create a new node to hold it, and in the implementation of the most annoying when the most frustrating is how to find the index of the specified location, the method described here is basically the following other operations are derived, first understand the insertion node specific implementation, according to the structure of the logical definition, If we want to insert a node after node A, then we need to modify the next reference to Node A, in fact, let the a node's next reference to the element field of the new node, and then let the next reference of the new node point to the original next node (B) of the Element field, the graph is more intuitive:

The code is as follows:

Java code

/**  * method to insert a new element into the list  */public void insert (int index,object obj) {      Node node=head;      int j=0;      while (node!=null&&j<index-2) {          //finds the index-1 element          Node=node.next;          j + +;      }      Node sert=new node (obj);//The node being inserted      Sert.next=node.next;      Node.next=sert;  }

Deletes an element. Know the insert element of the specific operation, the deletion of the element is relatively simple, for example, we want to delete a node B, is to make this node lost the reference, but do not directly write B=b.next, so that the reference to B is still there, and there will be another error, you can try it yourself, The correct way to delete a node is as follows:

The code is as follows:

Java code

/**  * Delete node at specified location  * @param index index  */public void Delete (int index) {      Node node=head;      int j=0;      while (node!=null&&j<index-2) {          //finds the i-1 element          Node=node.next;          j + +;      }      node.next=node.next.next;//Delete index element  }

View more Highlights: http://www.bianceng.cn/Programming/sjjg/

Finally, the element is modified. Believe that you read the previous two methods, the next method in the heart has already been thrown waves, it is directly below the code:

Java code

/** * Change the element at the specified position  * @param index index  * @param obj     */public void Modify (int index,object obj) {      Node node=h EAD;      int j=0;      while (node!=null&&j<index-1) {          //finds the Node=node.next of the index node          ;          j + +;      }      node.obj=obj;  }


Reference Source: http://www.bianceng.cn/Programming/sjjg/201407/42457.htm

Java linked list

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.