The realization and simple operation of the doubly linked list

Source: Internet
Author: User

1. One of the single-linked listsThe advantage is simple structure, but it also has a disadvantage, that is, in a single linked list can only access its subsequent nodes through a reference to a node, and cannot directly access its predecessor, to find a node in a single linked list of the predecessor node from the first node of the list, you must go backwards . look , but it takes 0. (n) time 。 For this we can expand the single chainthe node structure of the table makes it possible to access its predecessor nodes through the reference of a node, not only to its subsequent nodes. The way to extend a single-linked list node structure is to add a new field to the single-linked table node structure, which is used to refer to the direct precursor node of the node . The expanded node structure is the node structure that forms the doubly linked list. In the Finding in a doubly linked list is similar to that in a single-linked list, exceptin a doubly linked list, the find operation can start from the first node of the list, or from the tail nodes start , but the time required is the same as in a single-linked list.

The insert operation of a single-linked list must be preceded by a node in addition to the first node, and in a doubly linked list, before or after a known node.


1) S.prev = P.prev;

2) P.prev.next = s;3) S.next = p; 4) P.prev = s;
The deletion of a single-linked list, in addition to the first node must be aware of the node to be deleted on the basis of the predecessor node to enter the line, and in the doubly linked list in the case of a known node reference, you can complete the deletion of the node itself.

p.prev.next = P.next; P.next.prev = P.prev;   Inserts and deletes nodes in a doubly linked list with a Kinsoku node, regardless of the location of the inserted and deleted nodes, as In addition to the node, you do not have to consider the case that the linked list becomes non-empty or empty by non-empty   < Span style= "color: #ff0000; Background-color:orange ">HEAD&NBSP; TAIL&NBSP; point to ask The disadvantage of the   doubly linked list is that each time a node is inserted or deleted, the reference to four links is processed instead of two, and two links are connected before one node, Two connected after a link node. Of course, due to the two more references, the chain node occupies a bit more space.
<strong>public class Doublelink {public link first, public link last, public doublelink () {//constructor initialization this.last =  Null This.first = null;  The public boolean isEmpty () {///determines whether the empty return first = = null;} public void Insertfirst (int data) {//Inserts the element into the link header link  = new Link (data); if (IsEmpty ()) last = link;  If empty, last needs to change else First.prev = link;  Link.next = First; first = link;  } public void Insertlast (int data) {//Inserts the element at the end of the chain link = new link (data);  if (IsEmpty ()) last = link;  else Last.prev = link;  Link.prev = Last; last = link;  } public boolean InsertAfter (int key, int idata) {//After an element is inserted into Link current = first;  while (current.data! = key) {current = Current.next;  Link link = new link (idata);   if (current = = last) {link.next = null;  last = link;   } else {link.next = Current.next;  Current.next.prev = link;  } Link.prev = current;  Current.next = link; return true;  } public link DeleteKey (int key) {//delete an element Link current = first; WhiLe (current.data! = key) {current = Current.next;  } first = Current.next;  if (current = = last) last = Current.prev;  else Current.next.prev = Current.prev; return current;  } public link Deletefirst () {//delete the element at the beginning of the table link TEMP = first;  if (First.next = = null) last = NULL; else First.next.prev = null;  The next field of the first node refers to the Prev field of the link node first = First.next; return temp;  } public link Deletelast () {//delete the element at the end of the table link temp = last;  if (First.next = = null) first = NULL;  else Last.prev.next = null;  last = Last.prev; return temp;  } public void Showfirst () {//forward display Link current = last;   while (current = null) {current.show ();  current = Current.prev;  }} public void Showlast () {//rear display Link current = first;   while (current = null) {current.show ();  current = Current.next;  }} public static void Main (string[] args) {Doublelink dlink = new Doublelink ();  Dlink.insertfirst (1);  Dlink.insertfirst (3);  Dlink.insertfirst (2);  Dlink.insertafter (2, 4); Dlink.showfirSt ();  }}class link {public int data;//The public link prev;//a reference to the previous item, the direct predecessor public link next;//a reference to the latter, and the direct successor to public link (int Data) {this.data = data;} public void Show () {System.out.println (data + "")}} </strong>


The realization and simple operation of the doubly 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.