C # data structure and algorithm secrets 4

Source: Internet
Author: User

As mentioned in the previous section, we will talk about two-way linked lists, circular linked lists, and application examples. Let's get started !!!!

First, understand what a two-way linked list is. The so-called two-way linked list is that if you want to find the direct precursor node and the time complexity of the direct successor node is O (1), you need to set two reference fields in the node, the address that stores the direct precursor node, Prev, And next. Such a linked list is a two-way linked list (doubly linked list ). The node structure of the two-way linked list.

The definition of a two-way linked list node is similar to that of a single-chain table node, but the two-way linked list has an additional field Prev. In fact, a two-way linked list is more like a chain. You have connected me and I have connected you. It is not clear. Please refer to the figure.

The implementation of the bidirectional linked list node class is as follows:

// A chain class

Public class dbnode <t>
{

// The location of the current data
Private t data; // The data recorded in the data field
Private dbnode <t> Prev; // The Prev reference position of the prev domain.
Private dbnode <t> next; // reference the location of the domain chain

// Constructor. Is this initialization?
Public dbnode (T Val, dbnode <t> P)
{
Data = val;
Next = P;
}

// Constructor. Is this initialization?
Public dbnode (dbnode <t> P)
{
Next = P;
}

// The constructor. Pass the corresponding value of this chain to him.
Public dbnode (T Val)
{
Data = val;
Next = NULL;
}

// Constructor constructs an empty chain
Public dbnode ()
{
Data = default (t );
Next = NULL;
}

// Data domain attributes
Public t data
{
Get
{
Return data;
}
Set
{
Data = value;
}
}

// Pre-reference domain attributes
Public dbnode <t> Prev
{
Get
{
Return Prev;
}
Set
{
Prev = value;
}
}

// Reference domain attributes subsequently
Public dbnode <t> next
{
Get
{
Return next;
}
Set
{
Next = value;
}
}
}

After talking about the attributes of so many double linked list contacts, let's take a look at their related operations. Here, we will only describe the highlights.

Insert operation: Set P to point to a node in the two-way linked list, that is, p Stores the address of the node. Now, insert a node s to the end of node p, the source code to be inserted is as follows:

Optional P. Next. Prev = s;
➁ S. Prev = P;
S. Next = P. Next;
Optional P. Next = s;

Insert Process (take the direct successor node of P as an example ).

Note: The operation sequence for referencing domain values is not unique, but not arbitrary. The operation sequence must be placed before the operation sequence. Otherwise, P cannot be found directly at the successor node. This requires readers to clarify the meaning of each operation. This algorithm consumes time operations on the search, and the time complexity is O (n ).

Next, let's take a look at the delete operation. Taking deleting after a node as an example to describe how to delete a node in a two-way linked list. Set P to point to a node in the two-way linked list, that is, p Stores the address of the node. Now, insert a node s to the end of node p. The pseudocode is as follows:

When P. Next = P. Next. Next;
When P. Next. Prev = P. Prev;

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

The time complexity of the corresponding algorithm is also consumed to the node search. The complexity should be O (N)

The search operation is very similar to that of a single-chain table. It also traverses from the beginning. Pseudo code:

Current. Next = P. Next. Next

Current. Prev = P. Next. Prev;

Shows the pseudo-code:

The time complexity of this algorithm is the process of traversing one by one. The time complexity is O (n)

Retrieving the length of the current two-way linked list is similar to searching. We will not repeat it too much. Here, we have finished introducing the basic concepts and operations of the two-way linked list. Next we will introduce an important linked list-the ring linked list.

First, let's look at the definition of the circular linked list. Some applications do not need to have obvious head and tail nodes in the linked list. In this case, you may need to easily access the first node from the last node. At this point, the reference domain of the last node is not a blank reference, but the address of the first node saved (if the chain strap node is saved as the address of the first node ), that is, the value of the header reference. We call this chain table structure a circular chain table. He is like a child playing games with hands in hand ..

Linked List:

Here, the basic addition and deletion operations are exactly the same as those of a single-chain table, so there is no need to write these items here. We mainly look at some of their simple applications.

An example of an example of a known single-chain table h is used. Write an algorithm to put it upside down, that is, the implemented operation. (a) is before the inversion, and (B) is after the inversion.

Algorithm idea: Because the storage space of a single-chain table is not continuous, its inversion cannot be like that of a forward table, exchange the I node with the N-I node (the value range of I is 1 to n/2, and N is the length of the single-chain table ). The solution is to insert each node in the single linked list to the new linked list in sequence. In addition, to save memory resources, the header node of the original linked list is used as the header node of the new linked list. The inverted Algorithm for a single-chain table storing integers is 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;
}
}
This algorithm completes the inversion only after scanning the nodes in the linked list. Therefore, the time complexity is O (n), but it takes twice as long as the sequential table of the same length, because the sequence table only needs to scan half of the data elements. Are you confused about this? If you paste it, see the explanation of my legend.

Example 2: Joseph ring:

We know that N people (represented by numbers 1, 2, 3... n) are sitting around a round table. The number of people numbered K starts to report, and the person counting m is listed; the next person reporting the number from 1, and the person counting m is listed again; repeat this rule, until all the people around the Round Table are listed. Find the number of the person in the last column.

Void Joseph PHUs (int n, int K, int m) // n indicates the total number of people. k indicates the first person who starts reporting the number, and M indicates the number of people who call the column.


{


/* P indicates the current node, r indicates the secondary node, and the list of precursor nodes pointing to P indicates the header node */


Linklist P, R, list;/* Create a circular linked 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;/* loop the linked list */


P = List;/* point P to the header node */


/* Move the current pointer to the first reporter */


For (I = 0; I <K; I ++)


{


R = P;


P = P. Link;


}


/* Delete queue nodes cyclically */


While (P. Link! = P)


{


For (I = 0; I <m-1; I ++)


{


R = P;


P = P. Link;


}


R. Link = P. Link;


Console. writeline ("deleted elements: {0}", p. data );


Free (P );


P = R. node .;


}


Console. writeline ("\ n: {0}", p. data );

Specific algorithms ,:

 

The time complexity of this algorithm is O (n2)


}

I also share with you the following example: When I create a product similar To Netease mailbox and log on to tens of millions or even hundreds of millions of users, I found the user login was really slow. You can guess how I started to do it, that is, directly query the database, which of course cannot be done. What should I do? Finally, with the help of an expert, I found that the login speed was fast. I read all the database data into the memory, and then concatenate the data with a linked list. When I query a user, I only compare the user's bytes.

This is the linked 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.