Linked List of Data Structure

Source: Internet
Author: User

Linked List of Data Structure

A linked list is a data structure with relatively fast insertion and deletion. The disadvantage is that searching is slow. Unless you need to frequently use subscript to randomly access data, you can use linked lists in many places where arrays are used.

In the linked list, each data item is included inLink node"A chain node is the object of a class. Each link object contains a reference to the next link point. The object of the linked list itself has a field that points to the reference of the first link, as shown in:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1NrK/dfp1tCjrMO/examples/HSu8XFt7/X06OsxOO/examples + 3c/examples + examples/b2rxL + examples/examples + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141212/20141212092813122.png" alt = "\">

In this way, the elements that are skipped and deleted in the linked list pointer chain are deleted. However, in fact, the next pointer of the deleted element still points to the original next element, but it cannot be retrieved by a single linked list, JVM's garbage collection mechanism will recycle it after a certain amount of time

Adding a link node is a little more complex than deleting a link node. First, we need to point the next pointer of the link node before insertion to the target link node, next pointer of the target link node also points to the link node after the insertion position. In this example, the insert position is limited to the first position of the linked list, as shown below:

The implementation class of a single-chain table is given below

// The encapsulation class public class Link {public int age; public String name; public Link next; // point to the next Link of the Link node // constructor public Link (int age, String name) {this. age = age; this. name = name;} // print the information of the link node public void displayLink () {System. out. println ("name:" + name + ", age:" + age) ;}// encapsulation class of the linked list public class LinkList {private Link first; // point to the first chain node in the linked list public LinkList () {first = null;} // insert it to the front-end public void insertFirst (Link link) {link. next = first; first = link;} // Delete the first Link. The system returns the deleted link that references the public Link deleteFirst () throws Exception {if (isEmpty ()) {throw new Exception ("the linked list is empty! You cannot perform the delete operation ");} Link temp = first; first = first. next; return temp;} // print out all linked list elements public void displayList () {Link cur = first; while (cur! = Null) {// print each chain node in a loop. displayLink (); cur = cur. next ;}// delete the public Link delete (int key) {Link link = null; Link cur = first; Link next = first. next; Link previous = null; while (cur! = Null) {if (cur. age = key) {// the link of the link to be deleted is found. // If the precursor of the current link is null, it is proved that it is the first link of the linked list, after deleting the link node, You need to assign a new value to the first attribute if (previous = null) {this. first = next;} else {// delete operation, that is, the next pointer of the precursor points to the next of the current link node, the chain table will go to the current link node previous. next = next;} break;} else if (cur. next = null) {// The current link node is not the target and the next link node is null. It proves that there is no link break to be deleted ;} // if the current link node is not the target to be deleted, continue searching for next = next. next; cur = cur. next; previous = cur;} return link;} // The Search property refers Public Link find (int key) {Link link Link = null; Link cur = first; Link next = null; Link previous = null; while (cur! = Null) {if (cur. age = key) {link = cur; break;} else if (cur. next = null) {// if the current link node is not the target and the next link node is null, the target break is not found ;} // if the current link node is not the target and has the next link node, continue searching for next = next. next; cur = cur. next; previous = cur;} return link;} // empty public boolean isEmpty () {return (first = null );}}

Double-ended linked list

Note that,Double-ended linked list and two-way linked list are completely different concepts.

The difference between a double-ended linked list and a single-chain table is that it not only references the first link node, but also references the last link node, as shown in:


The reference of the last link node allows inserting a link node at the end of the table as the header. Using a double-ended linked list is more suitable for the scenarios where it is inconvenient to operate a single-linked table. Queue implementation is like this.

The following is an implementation class of the double-ended linked list. Different from a single-linked table, the DoubleEndList class has more references pointing to the end of the table and insert more operations at the end of the table.

// The encapsulation class public class DoubleEndList {private Link first; // point to the first Link node private Link last in the linked list; // point to the last chain node in the linked list public DoubleEndList () {first = null; last = null;} // insert it to the front-end public void insertFirst (Link link Link) of the linked list) {if (isEmpty () {// if it is an empty linked list, the first link node inserted is both the header and the last link of the table;} link. next = first; first = link;} // insert to the end of the linked list public void insertLast (Link link) {if (isEmpty () {// if it is an empty linked list, the first link node inserted is both the header and the end of the table. first = lin K;} else {last. next = link;} last = link;} // Delete the first Link. The deleted link node references public link deleteFirst () throwsException {if (isEmpty ()) {throw new Exception ("the linked list is empty! You cannot perform the delete operation ");} Link temp = first; if (first. next = null) {last = null; // if there is only one link node, the last pointer will be affected after deletion} first = first. next; return temp;} // print out all linked list elements public void displayList () {Link cur = first; while (cur! = Null) {// print each chain node in a loop. displayLink (); cur = cur. next ;}// empty public boolean isEmpty () {return (first = null );}}

The double-ended linked list can be inserted to the end of the table, but it is still not convenient to delete the end of the table, because we have no way to quickly obtain the last and second chain nodes, the double-ended linked list has no reverse pointer, this is solved by a two-way linked list.

Ordered linked list

For some applications, it is very useful to keep data in order in the linked list. A linked list with this feature is called an ordered linked list"

In general, the deletion of an ordered linked list is limited to the maximum or minimum values. However, it sometimes finds and deletes a specific point. However, this operation is not efficient for an ordered linked list.

An ordered linked list is better than an ordered array in that the insertion efficiency is higher (elements do not need to be moved like an array). In addition, the linked list can flexibly expand the size, while the array size is fixed. However, the improvement in efficiency and flexibility are at the cost of complicated algorithms.

The following is an implementation class for an ordered single-chain table.

// The encapsulation class public class SortedList {private Link first; // point to the first Link node public SortedList () {first = null ;} // insert public void insert (Link link) {Link previous = null; Link cur = first; while (cur! = Null & link. age> cur. age) {// previous = cur; cur = cur. next;} if (previous = null) {// if previous is null, it indicates that the current link node is the header this. first = link;} else {previous. next = link;} link. next = cur;} // Delete the first Link. The system returns the deleted Link that references the public Link deleteFirst () throwsException {if (isEmpty ()) {throw new Exception ("the linked list is empty! You cannot perform the delete operation ");} Link temp = first; first = first. next; return temp;} // print out all linked list elements public void displayList () {Link cur = first; while (cur! = Null) {// print each chain node in a loop. displayLink (); cur = cur. next ;}// empty public boolean isEmpty () {return (first = null );}}

The difference between this implementation class and a single-chain table lies in the insert operation. Because it is ordered, you must first locate the location to be inserted before insertion.

Ordered linked lists can be used in an efficient sorting mechanism. Suppose there is an unordered array. If data is taken from this array and inserted into the ordered linked list, they are automatically arranged in order and then re-placed into the array from the ordered linked list, this array becomes an ordered array. This sorting method is more efficient than the insert sorting method commonly used in arrays, because this method performs less replication times.

Two-way linked list

Next we will discuss a more complex linked list structure: two-way linked list.

The traditional linked list has a potential problem, that is, there is no reverse reference. It is easy to jump from the previous link node to the next link node, but it is difficult to jump from the next link node to the previous link node.

A two-way linked list provides the ability to traverse successively or backward. The key to implementing this feature is that each link node has both a reference of the next link node and a reference of the previous link node, as shown in:


It is true that a two-way linked list has its own advantages, but all its advantages must be paid at a certain cost. For example, the insertion and deletion of a two-way linked list will become more complex, because we need to handle double pointer changes at the same time. For example, we insert a table header as follows:


Insert at the end of the table is similar to insert at the beginning of the table.

Insert a new link node at a location other than the end of the table in the header as follows:


For the delete operation, it is relatively simple to delete the header or end of a table. If you want to delete a chain node with a specified value, you must first locate the chain node to be deleted, then the pointer of the front and back links is changed as follows:


The implementation class of the two-way linked list is given below. What is different from the previous linked list is that the encapsulation class of the link node needs to be changed. The previous link node class only contains the next reference, you also need to add the forward reference previous to the two-way linked list.

// The encapsulation class of the Link node: public class Link {public int age; public String name; public Link next; // point to the next Link node: public Link previous; // refers to the forward Link node. // constructor public Link (int age, String name) {this. age = age; this. name = name;} // print the information of the link node public void displayLink () {System. out. println ("name:" + name + ", age:" + age );}}

// The encapsulation class public class DoublelyLinkList {private Link first; // point to the first Link node private Link last in the linked list; // point to the last chain node in the linked list public DoublelyLinkList () {first = null; last = null;} // insert it to the front-end public void insertFirst (Link link Link) of the linked list) {if (isEmpty () {// if it is an empty linked list, the first link node inserted is both the header and the last link of the table ;} else {// if the chain table is not empty, the first pointer of the chain table is directed to the first node of the chain. previous = link;} link. next = first; first = link;} // insert to the end of the linked list public void insertLast (Link link) {if (isEmpty () {// if it is an empty linked list, the first link node inserted is both the header and the end of the table first = Link;} else {last. next = link; link. previous = last;} last = link;} // Delete the first Link. The deleted link References public Link deleteFirst () throws Exception {if (isEmpty ()) {throw new Exception ("the linked list is empty! You cannot perform the delete operation ");} Link temp = first; if (first. next = null) {last = null; // if there is only one link node, the deletion will affect the last pointer} else {// if there are at least two link nodes, set previous of the second link node to null first. next. previous = null;} first = first. next; return temp;} // Delete the last Link. return the deleted Link that references the public Link deleteLast () throws Exception {if (isEmpty ()) {throw new Exception ("the linked list is empty! You cannot perform the delete operation ");} Link temp = last; if (last. previous = null) {first = null; // if there is only one link node, the deletion will affect the first pointer} else {// if there are at least two link nodes, set the next of the last and second chain nodes to null last. previous. next = null;} last = last. previous; return temp;} // The search attribute is the public Link find (int key) {Link cur = first; while (cur! = Null & cur. age! = Key) {if (cur. next = null) {returnnull; // The current link node is not the target and has reached the end of the table} cur = cur. next;} return cur;} // Insert after the specified Link node. If the operation succeeds, true is returned. If the operation fails, false public boolean insertAfter (link Link link) {Link target = find (link. age); boolean flag = true; if (target = null) {// The inserted reference link flag is not found ;} else {// found the inserted reference link node if (target. next = null) {// The reference link is the insertLast (link) at the end of the table;} else {// The linked list has at least two link nodes, target. next. previous = link; link. Next = target. next; // the preceding two steps must be completed, the following two steps can be executed: // The Relationship Between the link and its next link node is processed in the preceding two steps. // the link and the target of the previous link node are processed in the following two steps. next = link; link. previous = target ;}} return flag;} // print out all linked list elements public void displayList () {Link cur = first; while (cur! = Null) {// print each chain node in a loop. displayLink (); cur = cur. next ;}// empty public boolean isEmpty () {return (first = null );}}


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.