fork download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4.  
  5. /// </summary>Single linked list.</summary>
  6. public class List<T> {
  7. /// <summary>Each node.</summary>
  8. private class Node {
  9. public T? Value { get; set; }
  10. public Node? Next { get; set; }
  11. }
  12.  
  13. /// </summary>To run over the list.</summary>
  14. public class ListCursor {
  15. public ListCursor(List<T> owner)
  16. {
  17. this.List = owner;
  18. }
  19.  
  20. public List<T> List { get; }
  21. public T? Value {
  22. get {
  23. if ( this.Node is null ) {
  24. return default(T);
  25. }
  26. return this.Node.Value;
  27. }
  28. }
  29. private Node? Node { get; set; }
  30. public void Reset() => this.Node = this.List._beg;
  31. public void Next() => this.Node = this.Node?.Next;
  32. public bool IsEnd() => this.Node is null;
  33. }
  34.  
  35. public List()
  36. {
  37. this._beg = this._end = null;
  38. this.Cursor = new ListCursor( this );
  39. this.Count = 0;
  40. }
  41.  
  42. public void Add(T x)
  43. {
  44. Debug.Assert( x is not null, "List.Add(): x es null");
  45. Trace.WriteLine( $"List.Add(): {x}" );
  46. Trace.Indent();
  47.  
  48. var node = new Node { Value = x };
  49. var oldEnd = this._end;
  50.  
  51. if ( this._beg is null ) {
  52. Trace.WriteLine( "List.Add(): _beg = _end = node" );
  53. this._beg = this._end = node;
  54. } else {
  55. Trace.WriteLine( "List.Add(): _end.Next = node" );
  56. this._end.Next = node;
  57. this._end = node;
  58. }
  59.  
  60. this.Count++;
  61. this.ChkInvariant();
  62.  
  63. Debug.Assert( oldEnd != this._end, "List.Add(): there's no new node" );
  64.  
  65. Trace.Unindent();
  66. }
  67.  
  68. public int Count { get; private set; }
  69.  
  70. /// <summary>
  71. /// Class invariant.
  72. /// The list should be traversable at all times.
  73. /// While traversing the list, we should count Count nodes.
  74. /// The last node should be _end.
  75. /// ** This is solely compiled in the Debug profile. **
  76. /// </summary>
  77. [Conditional("DEBUG")]
  78. public void ChkInvariant()
  79. {
  80. Trace.WriteLine( "List.ChkInvariant()" );
  81.  
  82. var node = this._beg;
  83. Node? prev = null;
  84. int num = 0;
  85.  
  86. while( node != null ) {
  87. ++num;
  88. prev = node;
  89. node = node.Next;
  90. }
  91.  
  92. Debug.Assert( prev == this._end, "List.ChkInvariant: end is not the end of the list" );
  93. Debug.Assert( num == this.Count, $"Expected {this.Count}, found {num}." );
  94. }
  95.  
  96. public ListCursor Cursor { get; }
  97. private Node? _beg;
  98. private Node? _end;
  99. }
  100.  
  101. static class Ppal {
  102. static void Main()
  103. {
  104. Trace.Listeners.Add( new ConsoleTraceListener() );
  105. var l = new List<int>();
  106.  
  107. for(int i = 0; i < 10; ++i) {
  108. l.Add( i + 1 * 100 );
  109. }
  110.  
  111. l.Cursor.Reset();
  112. while ( !l.Cursor.IsEnd() ) {
  113. Console.WriteLine( l.Cursor.Value );
  114.  
  115. l.Cursor.Next();
  116. }
  117.  
  118. Trace.Close();
  119. }
  120. }
  121.  
Success #stdin #stdout 0.05s 5028KB
stdin
Standard input is empty
stdout
List.Add(): 100
    List.Add(): _beg = _end = node
    List.ChkInvariant()
List.Add(): 101
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 102
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 103
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 104
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 105
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 106
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 107
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 108
    List.Add(): _end.Next = node
    List.ChkInvariant()
List.Add(): 109
    List.Add(): _end.Next = node
    List.ChkInvariant()
100
101
102
103
104
105
106
107
108
109