Java Learning Sharing------linked list

Source: Internet
Author: User

A linked list is an ordered collection that stores each object in a separate node, and each node holds a reference to the next node. In Java, because a linked list is a two-way link, each node also holds a reference to the previous node.

(Image from Java Core Technology Volume 1 Basics)

To delete an element in the middle of a linked list, only the nodes near the deleted element need to be updated. Assuming that we have three nodes, after the second node is deleted, the first node will update the reference to the third node (which corresponds to the "reference to the next node in each node") that originally held the second one. The third node updates the reference to the first node that originally held the second node (this corresponds to the "reference to the previous node" that we mentioned earlier).

(Image from Java Core Technology Volume 1 Basics)

The normal call to the Add () method is to add elements to the end of the list, but there may be cases where elements are added to the middle of the list. We need to use Listiterator to do this, and from the name we know that this is also an iterator, but this iterator provides an add () method (note that it is understood separately from the Add () method of the list itself). The Add () method of the call iterator adds an element before the location of the iterator. In another set type, because the elements are unordered, there is no Add () method in the iterator interface. So we come to the conclusion that "it is only practical to use iterators to add elements to a naturally ordered set."

What about this implementation process? First we need to make sure that the new element is added to the existing element in the list, after the position has been determined, the next () method of the iterator is passed over to the existing element, and the new element can be added to the specified position of the list by invoking the Add () method of the iterator.

After clarifying the above steps, there are a few questions to consider below.

① need to add elements to the table header of the linked list

1) We can call the iterator's Add () method after we get the Listiterator object we just returned, and the following is example

list<string> list =NewLinkedlist<string>(); List.add ("Andy"); Listiterator<String> iterator =List.listiterator (); Iterator.add ("Amy");  while(Iterator.hasnext ()) {//The result of this output is Andy.System.out.println (Iterator.next ()); } iterator=List.listiterator ();  while(Iterator.hasnext ()) {//this will output two times, one for Amy and one for Andy.System.out.println (Iterator.next ()); }

Why is the println in the first while loop output only once, and is it the output or Andy?

According to the "Add () method of the call iterator we mentioned earlier" adds an element before the position of the iterator ", when we first get a reference to the iterator object, the position of the iterator precedes the Andy element, by invoking the Add () method of the iterator, Amy this element is added to the front of the iterator, so that when the Hasnext () method is executed, the iterator object also has an element that is accessible to Andy, which returns true. Another call to Next () returns the element Andy's reference.

And before executing the second while, the iterator object's reference is re-located, where the iterator is before Amy, because there are two accessible elements in the iterator object, so println prints out Amy and Andy, respectively.

② need to add elements to the footer of the list

1) Call the Add () method of the linked list itself directly

2) when the Hasnext () method of the iterator returns false, this means that the iterator has crossed the last element of the list and then called the iterator's Add () method

        New Linkedlist<string>();        List.add ("Andy");                Listiterator<String> iterator = list.listiterator ();                  while (Iterator.hasnext ()) {            iterator.next ();        }                Iterator.add ("Amy");                 = list.listiterator ();                  while (Iterator.hasnext ()) {            System.out.println (Iterator.next ());        }

③ how many locations in a linked list are elements that can be added

If there are n elements in the list, then there will be n+1 places to add elements. For example, the list has ab two elements, then the front of a, the middle of AB, and the back of B can be added elements.

The Add () method we mentioned earlier, which refers to the add () method of the list itself, is the addition of elements to the tail of the list. There is also a set () method in the list, what is it used for? The set () method replaces the incoming element with the one returned by the call to next () or the previous () method, at which point the linked list is modified. An additional explanation is that the add (), remove () method is a structural modification to the list, and the set () method is not considered a structural modification.

The list does not support fast random access, and if you want to see the nth element in a linked list, you must start over n-1 elements. This means that if you use Get (2) to access the third element in the linked list, you must cross the first two elements. What if I want to access the fourth element in the linked list? Get (3) This approach seems to work, although it does also access the fourth element, but this process pays the price of crossing the first three elements.

Think about using iterators? When we use iterators to access the third element in a linked list, is it so troublesome to access the fourth element?

No, because at this point the iterator only needs to move backwards, passing the fourth element, to return a reference to the element that was just crossed.

Given this, if you need to access the element through an integer index, you should not use a linked list.

So much so, what are the scenarios under which we choose a linked list, the only reason is to try to minimize the cost of inserting or deleting elements in the middle of the list.

Finally, a few written questions related to the linked list are attached:

First, please write out the results of println printing

        New Linkedlist<string>();        List.add ("one");        List.add ("both");        List.add ("three");        List.add ("four");        List.add ("five");
listiterator<String> iteratorone = list.listiterator (2); Listiterator<String> iteratortwo = list.listiterator (Iteratorone.nextindex ());
if (Iteratortwo.hasnext ()) { System.out.println (Iteratortwo.next ()); }

Second, please write the results of each println printing

List<string> A =NewLinkedlist<>(); A.add ("Amy"); A.add ("Carl"); A.add ("Erica"); List<String> B =NewLinkedlist<>(); B.add ("Bob"); B.add ("Doug."); B.add ("Frances"); B.add ("Gloria"); Listiterator<String> Aiter =A.listiterator (); Iterator<String> biter =B.iterator ();  while(Biter.hasnext ()) {if(Aiter.hasnext ()) Aiter.next ();        Aiter.add (Biter.next ());        } System.out.println (a); Biter=B.iterator ();  while(Biter.hasnext ()) {biter.next (); if(Biter.hasnext ()) {biter.next ();            Biter.remove ();                }} System.out.println (b);                A.removeall (b); System.out.println (a);

Java Learning Sharing------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.