簡體   English   中英

顛倒鏈表的順序

[英]reverse the order of the linked list

我有一個鏈表,它添加一個像樹的對象,以下是打印輸出LinkedList nodeList = new LinkedList();

(結果A)

1 : Tester Meeting
2 : Adminstrative & Operational 
3 : Functional Committees
4 : Aduit Committee
9 :    Supporting Services Development
8 :    Medical Services Development 
7 :    Information Technology Services 
6 :    Human Resources Committee
15 :          test2-2
14 :       test2
13 :          test1-1
12 :       test1
5 :    Finance Committee
10 : Regional Advisory Committees
11 : Board Workshop

(結果B)以下應該是正確的順序

Tester Meeting
Adminstrative & Operational 
Functional Committees
Aduit Committee
   Finance Committee
      test1
         test1-1
      test2
         test2-2
   Human Resources Committee
   Information Technology Services 
   Medical Services Development 
   Supporting Services Development
Regional Advisory Committees
Board Workshop

那么,我想顛倒(ResultA)審計委員會子節點的順序輸出與ResultB相同的結果,是否有任何方法對鏈表的特定節點進行排序?

否。鏈接列表沒有排序順序的概念,而不是創建項目的順序。 它意味着快速遍歷和添加許多項目。 我不確定這個數據結構是否適合您的需求。

我不確定你的要求是什么。 也許如果你能列出要求我們可以提出一些建議。

從您描述的樹結構看起來更合適。

它是關於樹遍歷的。 這是關於你是從左到右還是從右到左遍歷孩子:

class Program {static void Main(string [] args){Node rootNode = new Node(); rootNode.Name =“root”; 節點node1 = new Node(); node1.Name =“child 1”;

    Node node2 = new Node();
    node2.Name = "child 2";

    rootNode.Children.Add(node1);
    rootNode.Children.Add(node2);

    Node node3 = new Node();
    node3.Name = "child 3";



    node1.Children.Add(node3);


    Traverse(rootNode);

    Console.WriteLine("Reverse: ");

    TraverseReverse(rootNode);

}

private static void Traverse(Node node)
{
    Console.WriteLine(node.Name);
    for (int index = 0; index < node.Children.Count;index++ )
    {
        Traverse(node.Children[index]);
    }            
}

private static void TraverseReverse(Node node)
{
    Console.WriteLine(node.Name);
    for (int index = node.Children.Count-1; index >=0; index--)
    {
        TraverseReverse(node.Children[index]);
    }
}     

}

輸出:

root
child 1
child 3
child 2
Reverse:
root
child 2
child 1
child 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM