我想从字符串创建通用列表

时间:2015-12-06 15:29:47

标签: algorithm generics

我想从字符串

创建通用列表

输入字符串是(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)

我的班级是

public class Node
{
    public string Name; // method name
  //  public decimal Time; // time spent in method
    public List<Node> Children;
}

子节点表示在()。

示例:a是父节点,b,c u是子节点;将以与其父级相同的方式保存在List<Node>中,其中j为子级,k,l和m为子级j。

它与树类似

<.>
 |---<a>
 |    |--- b
 |    |--- c
 |    |--- u
 |--- d
 |---<e>
 |    |--- f
 |---<g>
 |--- h
 |---<i>
 |    |---<j>
 |    |    |--- k
 |    |    |--- l
 |    |    |---<m>
 |    |    |    |--- n
 |--- r

1 个答案:

答案 0 :(得分:2)

The end result for you Node data structure will end up looking similar to a tree. To achieve what you want you could use a recursive function.

Here is an example of such a function (with comments):

    //Recursive Function that creates a list of Node objects and their    
    //children from an input string
    //Returns: List of Nodes
    //Param string input: The input string used to generate the List
    //Param int index: The index to start looping through the string
    public List<Node> CreateNodeList(string input, int index)
    {
        List<Node> nodeList = new List<Node>();
        Node currentNode = new Node();
        StringBuilder nameBuilder = new StringBuilder();

        //Start looping through the characters in the string at the 
        //specified index
        for(int i = index; i < array.size; i++)
        {
            switch(input[i])
            {
                //If we see an open bracket we need to use the 
                //proceeding list to create the current nodes children
                //We do this by recursively calling this function 
                //(passing the input string and the next index as 
                //parameters) and setting the children property to b 
                //the return value
                case ‘(‘:
                    currentNode.Children = CreateNodeList(input, i+1);
                    i = input.IndexOf(‘)’, i);
                    break;
                //If we see a closed bracket we create a new Node based 
                //of the existing string, add it to the list, and then 
                //return the list
                case ‘)’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    return nodeList;
                //if we see a comma we create a new node object based 
                //off the current string and add it to the list
                case ‘,’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    currentNode = new Node();
                    break;
                //Any other character we see must be for the name   
                //of a node, so we will append it to our string 
                //builder and continue looping
                default:
                    nameBuilder.Append(input[i]);
            }
        }

        //We will probably never reach here since your input string 
        //usually ends in  ‘)’ but just in case we finish by returning 
        //the list
        return nodeList;
    }

    //An example of how to use this recursive function
    public static void main()
    {
        //Your input string
        string input = “(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)”;
        //Call our function with the input string and 1 as arguments
        //We use 1 to skip the first ‘(‘ which is a index 0
        List<Node> nodeList = CreateNodeList(input, 1);
        //Do stuff with list here 
    }

This function keeps track of characters for the names of nodes, creating new ones and adding them to the list every time it sees a ',' or ')' (returning the List when seeing a ')')) and it also populates a Nodes children when it sees a '(' character by recursively calling the function and using its return value. The one major downside being you have keep track off the index you're on.

This function was written free hand but it's meant to be very similiar to C# (you didn't specify a language so I hope this helps.)

I hope this helps and is what your'e looking for :)