C # Data structures and algorithms--doubly linked list

Source: Internet
Author: User

First, understand what a doubly linked list is. The so-called doubly linked list is if you want to find the direct precursor node and the immediate successor node of the time complexity is O (1), then, you need to set two reference fields in the node, an address to save the direct predecessor node, called Prev, a direct successor node address, called Next, such a list is a doubly linked list (doubly Linked List). The node structure of a doubly linked list.


650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120510423.png "/>

The definition of a doubly linked list node is similar to that of a single linked list, except that the doubly linked list has more than one field prev. In fact, the doubly linked list is more like a chain, you even I, I even you, not clear, please look at the picture.

650) this.width=650; "Width=" 620 "height=" "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510424.png "/>

The implementation of the doubly linked list node class is as follows

A chain of classes

public class Dbnode<t>
{

//Current data is
Private T data,//data field record &NBSP,
private dbnode<t> prev;//precursor reference domain precursor reference location  
Private Dbnode<t> Next; Subsequent reference fields later the position of the chain

//constructor This is not initialized  
Public Dbnode (T Val, dbnode<t> p)
{
data = val;
Next = p;
}

//constructor This is not initialized
public dbnode (dbnode<t> p)
{
next = p;
}

//constructor   bar The corresponding value of this chain is passed to him
public Dbnode (T val)
{
data = val;
next = null;
}

//constructor   construct an empty chain
public Dbnode ()
{
data = default (T);
next = null;
}

//Data field Properties
Public T data
{
Get
{
return data;
}
Set
{
data = value;
}
}

//Precursor reference domain properties
Public dbnode<t> Prev
{
Get
{
return Prev;
}
Set
{
Prev = value;
}
}

//Subsequent reference domain properties
Public dbnode<t> Next
{
Get
{
return Next;
}
Set
{
Next = value;
}
}
}

Having said so much about the properties of the class of the doubly linked table contact, we want to take a look at his related operations. Here's just a description of the place where the finishing touches

Insert operation: Set P is a node in a doubly linked list, that is, p stores the address of the node, and now you want to insert a node s behind the node p, insert the source code as follows: The operation is as follows:

P.next.prev = s;
S.prev = p;
S.next = P.next;
P.next = s;

The insertion process (in the case of a direct successor node of P).

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120510425.png "/>

Note: The order of the operations that reference the domain values is not unique, but is not arbitrary, the operation must be placed in front of the operation to complete, otherwise p direct successor node can not be found. This requires the reader to clarify the meaning of each operation . This algorithm time operation is consumed on the lookup, the complexity of its time is O (n).

Below, look at his delete operation to remove the node after the node as an example to illustrate the deletion of nodes in a doubly linked list. Set P is a node that points to a doubly linked list, that is, p stores the address of the node, and now inserts a node s into the back of the node p. The pseudo-code is as follows:

P.next = P.next.next;
P.next.prev = P.prev;

Delete process (take the direct successor node of P as an example)

650) this.width=650; "Width=" 620 "height=" 175 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510426.png "/>

The time complexity of the corresponding algorithm is also consumed to find the node, its complexity should be O (n)

The find operation is very similar to a single-linked list and is also traversed from the beginning. The corresponding pseudo-code:

Current.next=p.next.next

Current.prev=p.next.prev;

The corresponding pseudo-code is as follows:

650) this.width=650; "Width=" 620 "height=" 348 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510427.png "/>

The time complexity of the algorithm is one of the traversal process, gu time complexity is O (n)

Get current doubly linked list length and look similar, do not do too much to repeat, here, we put the basic concept and operation of the two-way list is finished, the following is an important link list-ring linked list.

First of all, look at the definition of the ring list as usual. Some applications do not require significant tail-to-node nodes in the list. In this case, it may be convenient to access the first node from the last node. At this point, the reference field of the last node is not a null reference, but rather the address of the first node that is saved (if the link is a node, the address of the head node is saved), which is the value of the header reference. We call such a linked list structure as a ring-linked list. He's like a kid holding hands and playing games.

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120510428.png "/>

650) this.width=650; "Width=" 620 "height=" 257 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510429.png "/>

Here the basic addition, deletion, operation of the operation and the single-linked list is almost identical, there is no need to write these things. We mainly look at some of their simple applications.

An example of a known single-linked list H is used to write an algorithm that is inverted, that is, the operation implemented, where (a) is inverted before (b) is inverted.

650) this.width=650; "Width=" 620 "height=" "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510430.png "/>


Algorithm idea: Because the single-linked table storage space is not continuous, so, its inversion can not be like the following table, the first node and the N-i node Exchange (I value range is 1 to n/2,n for the length of the single-linked list). The solution is to take each node in a single linked list into a new linked list in turn. Also, in order to save memory resources, the head node of the original list is used as the head node of the new linked list. An inverted algorithm for storing integers in a single-linked list is implemented as follows:

public void Reverslinklist (linklist<int> H)
{
node<int> p = h.next;
node<int> q = new node<int> ();
H.next = null;

while (P! = null)
{
Q = p;
p = p.next;
Q.next = H.next;
H.next = q;
}
}
The algorithm has to scan the list of nodes sequentially to complete the inversion, so the time complexity is O (n), but the same length of the order table more than one time, because the order table only need to scan half of the data elements. Is this something you've got in your head? If the paste is put, please see my explanation of the legend.

650) this.width=650; "Width=" 620 "height=" 473 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510431.png "/>

For example 2, Joseph Ring question, the topic is as follows:

Known n individuals (denoted by number 1,2,3...N) sit around a table. From the person numbered K began to count, the number to m of the person out of the man, and his next person from 1 began to count, the number to m of the person again, according to this rule repeat until the round table all the people out. Ask for the number of the last person to dequeue.

void JOSEPHUS (int n,int k,int m)//n is the total number of people, and K is the first person to start a count, and M is the number called by the Dequeue

{

/* p for the current node r is the auxiliary node, the predecessor node pointing to P is the head nodes */

Linklist p,r,list; /* Create a circular link list */

for (int i=0;i<n;i++)

{

p= (linklist) Lnode;

P.data=i;

if (list==null)

list=p;

Else

R.link=p;

R=p;

}

P.link=list; /* Make the list loop */

p=list;/* point P to the head node */

/* Move the current pointer to the first count of people */

for (i=0;i<k;i++)

{

R=p;

P=p.link;

}

/* Loop to delete the queue node */

while (P.LINK!=P)

{

for (i=0;i<m-1;i++)

{

R=p;

P=p.link;

}

R.link=p.link;

Console.WriteLine ("deleted element: {0}", P.data);

Free (p);

P=r.node.;

}

Console.WriteLine ("\ n the last element removed is: {0}", P.data);

The specific algorithm:

650) this.width=650; "Width=" 620 "height=" 420 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120510432.png "/>

The complexity of the time of this algorithm is O (n2)

}


Reference: http://www.jb51.net/article/31698.htm

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1663509

C # Data structures and algorithms--doubly linked list

Related Article

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.