コード例 #1
0
ファイル: Tree.cs プロジェクト: dengyu301/Personal-Projects
        public LinkedList insertNode(LinkedList rootNode , LinkedList newNode)
        {
            LinkedList currentNode = rootNode;
            if (rootNode == null)
            {
                return newNode;
            }
            while (currentNode.NextNode != null)
            {
                    currentNode = currentNode.NextNode;
            }
            currentNode.NextNode = newNode;

            return rootNode;
        }
コード例 #2
0
ファイル: Tree.cs プロジェクト: dengyu301/Personal-Projects
 public Boolean isAdjacent(LinkedList sourceNode, LinkedList destinationNode)
 {
     LinkedList currentNode = sourceNode;
     while (currentNode != null)
     {
         if (currentNode.Data == destinationNode.Data)
         {
             return true;
         }
         else
         {
             currentNode = currentNode.NextNode;
         }
     }
     return false;
 }
コード例 #3
0
        static void Main(string[] args)
        {
            Tree treeOperations = new Tree();

            int noofNodes = 0;
            LinkedList newNode;
            LinkedList rootNode = null;
            Console.WriteLine("Enter the number of nodes");
            noofNodes = Convert.ToInt16(Console.ReadLine());
            treeOperations.Graph = new LinkedList[noofNodes];
            treeOperations.Visited = new Boolean[noofNodes];
            for (int i = 0; i < noofNodes; i++)
            {
                treeOperations.Visited[i] = false;
            }

            for (int i = 0; i < noofNodes; i++)
            {
                Console.WriteLine("Enter the "+ (i+1)+ " root value" );
                newNode = new LinkedList(Convert.ToInt16(Console.ReadLine()));
                rootNode = treeOperations.insertNode(rootNode, newNode);
                Console.WriteLine("You are entering the mode to" +
                "give inputs for the adjacent nodes for node " + (i+1));
                while (true)
                {
                    String choice = "";
                    Console.WriteLine("Is there an adjacent node ? Y : N");
                    choice = Console.ReadLine().ToString();
                    if (choice.Equals("n"))
                    {
                        break;
                    }
                    Console.WriteLine("Enter the adjacent value");
                    newNode = new LinkedList(Convert.ToInt16(Console.ReadLine()));
                    rootNode = treeOperations.insertNode(rootNode, newNode);
                }

                treeOperations.Graph[i] = rootNode;
                rootNode = null;

            }
            treeOperations.displayTree();
            Console.WriteLine("The DFS traversal is");
            treeOperations.callDFS();
            Console.ReadLine();
        }
コード例 #4
0
 public LinkedList(int data)
 {
     this.data = data;
     this.nextNode = null;
 }