Exemple #1
0
        // Q4 BFS
        public static List<LinkedList> Tree2LinkedList(BinarySearchTree.Node Root)
        {
            List<LinkedList> Result;
            if (Root == null)
                return null;
            else
            {
                Result = new List<LinkedList>();
                Queue<BinarySearchTree.Node> Queue = new Queue<BinarySearchTree.Node>();
                Queue<int> QLevels = new Queue<int>();
                Queue.Enqueue(Root);
                QLevels.Enqueue(0);
                Result.Add(new LinkedList());
                Result[0].Add(Root);
                while (Queue.Count != 0)
                {
                    BinarySearchTree.Node TempNode = Queue.Dequeue();
                    LinkedList TempLinkedList;
                    int Level = QLevels.Dequeue();
                    if (TempNode != null)
                    {
                        // Check if(TempNode) is CurrentLevel
                        if (TempNode.Left != null)
                        {
                            Queue.Enqueue(TempNode.Left);
                            QLevels.Enqueue(Level + 1);
                        }
                        if (TempNode.Right != null)
                        {
                            Queue.Enqueue(TempNode.Right);
                            QLevels.Enqueue(Level + 1);
                        }
                        if (Level + 1 != Result.Count)
                        {
                            if (TempNode.Left != null) Result.Last().Add(TempNode.Left);
                            if (TempNode.Right != null) Result.Last().Add(TempNode.Right);

                        }
                        else
                        {
                            TempLinkedList = new LinkedList();
                            if (TempNode.Left != null) TempLinkedList.Add(TempNode.Left);
                            if (TempNode.Right != null) TempLinkedList.Add(TempNode.Right);
                            if (TempNode.Right != null || TempNode.Left != null)
                            Result.Add(TempLinkedList);
                        }
                    }
                }

                return Result;
            }
        }
 // Q5
 public static Node Circular(LinkedList List)
 {
     Node Result = List.Head;
     Hashtable hashtable = new Hashtable();
     while (Result != null)
     {
         if (hashtable.Contains(Result))
             return Result;
         else
         {
             hashtable.Add(Result, Result.Value);
             Result = Result.Next;
         }
     }
     return Result;
 }
        public LinkedList Reverse(LinkedList List, int K)
        {
            Node Cursor = this.Head;
            for (int i = 1; i < (K-1); i++)
            {
                while (Cursor.Next != null)
                {
                    Cursor.Next = Cursor.Next.Next;
                    Cursor.Next = Cursor;

                    Cursor = Cursor.Next;
                }
            }
            return List;
        }
 // Q1
 public LinkedList RemoveDuplicates(LinkedList List)
 {
     Node TempHead1 = List.Head;
     while (TempHead1 != null)
     {
         Node TempHead2 = TempHead1;
         while (TempHead2.Next != null)
         {
             if (TempHead1.Value.Equals(TempHead2.Next.Value))
             {
                 //List.Delete(TempHead2.Value);
                 TempHead2.Next = TempHead2.Next.Next;
             }
             else
                 TempHead2 = TempHead2.Next;
         }
         TempHead1 = TempHead1.Next;
     }
     return List;
 }