Esempio n. 1
0
        //Step1:
        // 1---2---3---4---5---6--NULL
        //         |
        //         7---8---9---10--NULL
        //             |
        //             11--12--NULL

        //Step2:
        //1---2---3---7---8---9---10---4---5---6--NULL
        //             |
        //             11--12--NULL

        //Step3:
        //1---2---3---7---8---11--12--9---10--4---5---6--NULL
        public NodeWithChildren Flatten2(NodeWithChildren head)
        {
            if (head == null)
            {
                return(null);
            }

            NodeWithChildren tmp = head;

            while (tmp != null)
            {
                if (tmp.child != null)
                {
                    NodeWithChildren child = tmp.child;
                    tmp.child = null;

                    NodeWithChildren next = tmp.next;
                    tmp.next   = child;
                    child.prev = tmp;
                    while (child.next != null)
                    {
                        child = child.next;
                    }

                    if (next != null)
                    {
                        child.next = next;
                        next.prev  = child;
                    }
                }
                tmp = tmp.next;
            }
            return(head);
        }
Esempio n. 2
0
        // Step1:
        // 1---2---3---4---5---6--NULL
        //         |
        //         7---8---9---10--NULL
        //             |
        //             11--12--NULL
        // Step2:
        // 1---2---3---4---5---6--NULL
        //         |
        //         7---8---11--12--9---10--NULL
        // Step3:
        // 1---2---3---7---8---11--12--9---10--4---5---6--NULL
        public static NodeWithChildren Flatten(NodeWithChildren head)
        {
            if (head == null)
            {
                return(head);
            }
            NodeWithChildren tmp = head;

            while (tmp != null)
            {
                if (tmp.child != null)
                {
                    NodeWithChildren child = Flatten(tmp.child);
                    tmp.child = null;
                    NodeWithChildren next = tmp.next;
                    tmp.next   = child;
                    child.prev = tmp;
                    while (child.next != null)
                    {
                        child = child.next;
                    }
                    child.next = next;
                    if (next != null)
                    {
                        next.prev = child;
                    }
                    tmp = next;
                }
                else
                {
                    tmp = tmp.next;
                }
            }
            return(head);
        }
Esempio n. 3
0
        public NodeWithChildren FlattenAndReturnTail(NodeWithChildren head)
        {
            if (head == null)
            {
                return(null);
            }
            if (head.child == null)
            {
                if (head.next == null)
                {
                    return(head);
                }
                return(FlattenAndReturnTail(head.next));
            }
            else
            {
                NodeWithChildren child = head.child;
                head.child = null;

                NodeWithChildren next      = head.next;
                NodeWithChildren childTail = Flatten(child);
                head.next  = child;
                child.prev = head;
                if (next != null)
                {
                    childTail.next = next;
                    next.prev      = childTail;
                    return(FlattenAndReturnTail(next));
                }
                return(childTail);
            }
        }
Esempio n. 4
0
        public static void PrintFlatList(NodeWithChildren head)
        {
            if (head == null)
            {
                Console.Write("Null head");
            }
            NodeWithChildren tmp = head;

            while (tmp.next != null)
            {
                Console.Write(tmp.key + "---");
                tmp = tmp.next;
            }
            Console.Write("null");
            return;
        }
Esempio n. 5
0
 public NodeWithChildren(int key)
 {
     this.key = key;
     prev     = next = null;
     child    = null;
 }
Esempio n. 6
0
 public NodeWithChildren Flatten3(NodeWithChildren head)
 {
     FlattenAndReturnTail(head);
     return(head);
 }