fork download
  1. using System;
  2.  
  3. public class Node
  4. {
  5. public object data;
  6. public Node next;
  7. public Node(object data)
  8. {
  9. this.data = data;
  10. }
  11. }
  12.  
  13. public class LinkedList
  14. {
  15. private Node head;
  16. private Node current;
  17. public void Add(Node n)
  18. {
  19. if (head == null)
  20. {
  21. head = n;
  22. current = head;
  23. }
  24. else
  25. {
  26. current.next = n;
  27. current = current.next;
  28. }
  29. }
  30.  
  31. public void Print()
  32. {
  33. Node curr = head;
  34. while(true)
  35. {
  36. if(curr == null)
  37. return;
  38. Console.WriteLine(curr.data.ToString());
  39. curr = curr.next;
  40. }
  41. }
  42. }
  43.  
  44.  
  45. class Program
  46. {
  47. static void Main(string[] args)
  48. {
  49. LinkedList list = new LinkedList();
  50. list.Add(new Node("first"));
  51. list.Add(new Node("second"));
  52. list.Add(new Node("third"));
  53. list.Add(new Node("fourth"));
  54. list.Print();
  55. }
  56. }
Success #stdin #stdout 0.02s 33856KB
stdin
Standard input is empty
stdout
first
second
third
fourth