fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class LinkedList {
  5.  
  6. public static void Main() {
  7.  
  8. // Create a new LinkedListNode of type String and displays its properties
  9. LinkedListNode<String> lln = new LinkedListNode<String>( "Teahupoo" );
  10. Console.WriteLine ( "After creating the node ...." );
  11. DisplayProperties ( lln );
  12.  
  13. // Create a new LinkedList
  14. LinkedList<String> ll = new LinkedList<String>();
  15.  
  16. // Add the "Teahupoo" node and display its properties
  17. ll.AddLast ( lln );
  18. Console.WriteLine ( "After adding the node to the empty LinkedList ...." );
  19. DisplayProperties ( lln );
  20.  
  21. // Add nodes before and after the "Avatar" node and display the "Avatar" node's properties
  22. ll.AddFirst ( "Mavericks" );
  23. ll.AddLast ( "Cocoa Beach" );
  24. Console.WriteLine ( "After adding Mavericks and Cocoa Beach ...." );
  25. DisplayProperties ( lln );
  26.  
  27. } // Main
  28.  
  29. public static void DisplayProperties ( LinkedListNode<String> lln ) {
  30.  
  31. if ( lln.List == null )
  32. Console.WriteLine ( " Node is not linked." );
  33. else
  34. Console.WriteLine ( " Node belongs to a linked list with {0} elements.", lln.List.Count );
  35.  
  36. if ( lln.Previous == null )
  37. Console.WriteLine ( " Previous node is null." );
  38. else
  39. Console.WriteLine ( " Value of previous node: {0}", lln.Previous.Value );
  40.  
  41. Console.WriteLine ( " Value of current node: {0}", lln.Value );
  42.  
  43. if ( lln.Next == null )
  44. Console.WriteLine ( " Next node is null." );
  45. else
  46. Console.WriteLine ( " Value of next node: {0}", lln.Next.Value );
  47.  
  48. Console.WriteLine ();
  49.  
  50. } // DisplayProperties
  51.  
  52. } // Class LinkedList
Success #stdin #stdout 0.04s 27720KB
stdin
Standard input is empty
stdout
After creating the node ....
   Node is not linked.
   Previous node is null.
   Value of current node:  Teahupoo
   Next node is null.

After adding the node to the empty LinkedList ....
   Node belongs to a linked list with 1 elements.
   Previous node is null.
   Value of current node:  Teahupoo
   Next node is null.

After adding Mavericks and Cocoa Beach ....
   Node belongs to a linked list with 3 elements.
   Value of previous node: Mavericks
   Value of current node:  Teahupoo
   Value of next node:     Cocoa Beach