Chain data and type data

Source: Internet
Author: User

Chain data and type data

I have been very busy over the past few days and have little time to study. But fortunately, I have encountered a small feature in my project recently, so I simply recorded it.

I. Chain Structure

The data in the chain structure is certainly not unfamiliar to children's shoes who have learned the C language. In the C class, there is a concept of a linked list, which then inserts, deletes, and concatenates various types of linked lists.

In C #, there is no such concept of linked list, but other methods can be used to implement that effect.

However, all of the above have nothing to do with this topic. The next step is the topic.

If there is a pile of unordered data, such:

2->3, 4->1, 1->2, 5->6, 3->4, 6->8, 7->11, 8->10, 9->35, 10->7, 11->9

There is no sequential order among them. What we need to do now is to splice the above directed data into several chain types, or call it a chain.

Note that the data on the left side cannot be repeated, and the data on the right side cannot be repeated. If there is a duplicate, a cross is enabled, not the data I want. Please make a good decision in advance.

 

II. Implementation

1. Check the effect first:

This is the final result.

2. Code:

/// <Summary> /// data to be exchanged /// </summary> public class Changed {public int Source {get; set;} public int Target {get; set ;}//< summary >/// chain //</summary> public class Line {public List <int> OneLine {get; set ;} public override string ToString () {return string. join ("->", OneLine) ;}} public static void Test () {List <Line> lines = new List <Line> (); list <Changed> changes = new List <Changed> () {new C Hanged () {Source = 2, Target = 3}, new Changed () {Source = 4, Target = 1}, new Changed () {Source = 1, target = 2}, new Changed () {Source = 5, Target = 6}, new Changed () {Source = 3, Target = 4}, new Changed () {Source = 6, Target = 8}, new Changed () {Source = 7, Target = 11}, new Changed () {Source = 8, Target = 10 }, new Changed () {Source = 9, Target = 35}, new Changed () {Source = 10, Target = 7}, new Changed () {Source = 11, target = 9 },}; L Ist <Changed> temp = changes. getRange (0, changes. count); foreach (var item in changes) {Line currentLine = lines. find (n => n. oneLine. contains (item. source); if (currentLine! = Null) {continue;} // the entire chain is searched every time and currentLine = new Line () is generated () {OneLine = new List <int> {item. source, item. target}; temp. remove (item); currentLine = GetALine (currentLine, temp); lines. add (currentLine);} lines. forEach (n => Console. writeLine (n. toString (); Console. readKey ();} public static Line GetALine (Line line, List <Changed> changes) {var flagLeft = false; var flagRight = false; if (changes. count = 0) {return line;} Changed left = null; Changed right = null; foreach (var item in changes) {if (item. target = line. oneLine [0]) {flagLeft = true; left = item;} if (item. source = line. oneLine. last () {if (item. target = line. oneLine [0]) {flagLeft = false;} flagRight = true; right = item;} if (flagLeft | flagRight) {if (flagLeft) {line. oneLine. insert (0, left. source); changes. remove (left);} if (flagRight) {line. oneLine. add (right. target); changes. remove (right);} return GetALine (line, changes);} return line ;}

A Recursion is used to generate a chain.

 

In fact, when I got the first data, I didn't know whether it was the initial data or the intermediate data. So, I adopted the method of extending to both sides, recursively search for other data.

In many cases, recursion can be converted cyclically.

public static Line GetALineByWhile(Line line, List<Changed> changes){    while (true)    {        var flagLeft = false;        var flagRight = false;        Changed left = null;        Changed right = null;        if (changes.Count == 0)        {            break;        }        foreach (var item in changes)        {            if (item.Target == line.OneLine[0])            {                flagLeft = true;                left = item;            }            if (item.Source == line.OneLine.Last())            {                if (item.Target == line.OneLine[0])                {                    flagLeft = false;                }                flagRight = true;                right = item;            }        }        if (flagLeft || flagRight)        {            if (flagLeft)            {                line.OneLine.Insert(0, left.Source);                changes.Remove(left);            }            if (flagRight)            {                line.OneLine.Add(right.Target);                changes.Remove(right);            }            continue;        }        break;    }    return line;}

When the data can be found on the left or right side of the loop, the next round of search will continue. Only after the source is traversed, no matching data will be found, to jump out of the loop and continue to judge the next group of data.

 

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.