axeefectushka

Untitled

Feb 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1.  public static Node Parse(string nodes)
  2.         {
  3.             Node head = null;
  4.             List<string> StringNumbers = nodes.Split(' ').ToList();
  5.            
  6.             for(int i=0;i<StringNumbers.Count();i++)
  7.             {
  8.                 if(int.TryParse(StringNumbers[i], out int s)==true)
  9.                 {
  10.                     head = new Node(s, head.Next);
  11.                 }
  12.             }
  13.             return head;
  14.         }
  15.         //  Assert.AreEqual(new Node(1, new Node(2, new Node(3))), Kata.Parse("1 -> 2 -> 3 -> null"));
  16.         //Assert.AreEqual(new Node(0, new Node(1, new Node(4, new Node(9, new Node(16))))), Kata.Parse("0 -> 1 -> 4 -> 9 -> 16 -> null"));
  17.         //Assert.AreEqual(null, Kata.Parse("null"));
  18.         public class Node : Object
  19.         {
  20.             public int Data;
  21.             public Node Next;
  22.  
  23.             public Node(int data, Node next = null)
  24.             {
  25.                 this.Data = data;
  26.                 this.Next = next;
  27.             }
  28.  
  29.             public override bool Equals(Object obj)
  30.             {
  31.                 // Check for null values and compare run-time types.
  32.                 if (obj == null || GetType() != obj.GetType()) { return false; }
  33.  
  34.                 return this.ToString() == obj.ToString();
  35.             }
  36.  
  37.             public override string ToString()
  38.             {
  39.                 List<int> result = new List<int>();
  40.                 Node curr = this;
  41.  
  42.                 while (curr != null)
  43.                 {
  44.                     result.Add(curr.Data);
  45.                     curr = curr.Next;
  46.                 }
  47.  
  48.                 return String.Join(" -> ", result) + " -> null";
  49.             }
  50.         }
Add Comment
Please, Sign In to add comment