C # Data structure and algorithm secrets four-way linked list _c# tutorial

Source: Internet
Author: User
Tags prev

First, understand what a two-way list is. The so-called doubly linked list is O (1) If the time complexity of looking for direct precursor nodes and direct subsequent nodes is desired. Then, you need to set up two reference domains in the node, one to save the address of the direct predecessor node, called Prev, a direct successor node address, called Next, such a linked list is a two-way list (doubly Linked List). The schematic diagram of the node structure of the bidirectional linked list is shown in the figure.

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

The implementation of the bidirectional linked list node class is shown below

A chain of the class

public class Dbnode<t>
{

//The current data is located in
Private T data,  
Private dbnode<t> prev;//predecessor reference domain precursor reference location  
Private Dbnode<t> Next; Subsequent reference fields the position of the chain later

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

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

//constructor   Bar This chain is passed to him by the corresponding value
public Dbnode (T val)
{
data = val;
next = null;
}

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

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

//Precursor Reference field Properties
Public dbnode<t> Prev
{
Get
{
return Prev;
}
Set
{
Prev = value;
}
}

//Successor Reference field Properties
Public dbnode<t> Next
{
Get
{
return next;
}
Set
{
Next = value;
}
}
}

Having said so much about the properties of the class of bidirectional linked list contacts, we need to look at his related operations. Here's a description of just the finishing touches.

Insert operation: Set P is to point to a two-way linked list of a node, that is, p stores the address of the node, now you want to insert a node s after 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 is shown in the figure (as in the case of a direct successor node of P).

Note: The order of the operation referencing the field value is not unique, but it is not arbitrary, the operation ➂ must be placed in the front of the operation ➃ to complete, otherwise p direct successor node will not be found. This requires the reader to figure out the meaning of each operation. The time operation of this algorithm consumes the lookup, and its time complexity is O (n).

Below, look at his deletion to remove the node after the knot as an example to illustrate the deletion of a node in a doubly linked list. Set p to point to a node in a two-way list, where p stores the address of the node, and now inserts a node s behind the node p. Pseudocode is as follows: operations are as follows:

➀p.next = P.next.next;
➁p.next.prev = P.prev;

The deletion process is shown in the figure (as in the case of a direct subsequent node of P).

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

The lookup operation is extremely similar to a single linked list and is also traversed from the beginning. The corresponding pseudo code is shown in the figure:

Current.next=p.next.next

Current.prev=p.next.prev;

The corresponding pseudo code is shown in the following illustration:

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

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

First of all, it's still the same, look at the definition of the ring list. Some applications do not require an obvious end-and-end node in the linked 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 the address of the first node that is saved (if the chain is node, the address of the header node is saved), which is the value of the header reference. We call such a linked list structure a circular list. He is like a child holding hands and playing games. As shown in the figure.

Use the list as shown in the diagram:

Here basically add, delete, operation operation and single linked list is almost the same, there is no need to write these things. We mainly look at them for some simple applications.

Using an example of a known single linked list H, write an algorithm to invert it, that is, to implement the operation shown in the figure, where (a) is inverted before, (b) is inverted.

Algorithm thinking: Because the single linked list of storage space is not continuous, so it can not be inverted as the table, the first I node and the first N-i node Exchange (I of the range is 1 to n/2,n for the length of a single chain). The solution is to insert each node in a single linked list into the new list in turn. In order to save memory resources, the head node of the original linked list is used as the head node of the new linked list. The inverted algorithm of the single linked list storing integers 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 sequence of nodes in the linked list to complete the inversion, so the time complexity is O (n), but it takes more time than the same length order table, because the sequential table only needs to scan half of the data elements. Is that what you have in mind? If the paste, please see the explanation of my legend.

For example 2, Joseph Ring question, the following topics:

Known n individuals (numbered 1,2,3...N, respectively) sit around a round table. From the number of people numbered K, count to M the man out of the column, his next person from 1 start off, Count to M of the person again out, according to this rule repeat, until the round table around the people all out. Ask for the number of the last person to come out.

void JOSEPHUS (int n,int k,int m)//n for total number, K for the first person to start off, M for the number of people shouting

{

/* p is the current node r is an auxiliary node, and the precursor node named P is a head joint.

Linklist p,r,list; /* Set up a cycle chain 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 linked list loop up * *

P=list/* Make P point to head node * *

/* Move the current pointer to the person who first numbered it.

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

{

R=p;

P=p.link;

}

/* Loop to delete 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 deleted element is: {0}", P.data);

The specific algorithm, as shown in the figure:

The complexity of the algorithm's time is O (n2)

}

Also share with you an example of is that I do a similar with NetEase mailbox products, tens of millions of or even the number of billions of millions of times to log in when the user login is really fucking slow, you guess I started to do is to directly check the database, this is certainly not. This How to do, finally, I am under the advice of an expert, found that the speed of the login, how to engage. I read all the data in the database into memory, and then strung them up with lists of data, and when I queried a user, I compared the number of bytes to the user.

This is the list structure in my eyes.

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.