Implementation of generic linked list in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. collections;
  6. Namespace generic
  7. {
  8. // Implementation of simple linked list, one-way (generic)
  9. Class generic class
  10. {
  11. Public static void main ()
  12. {
  13. Shortlist <string> List = new shortlist <string> ();
  14. List. addlast ("500 ");
  15. List. addlast ("400 ");
  16. List. addlast ("300 ");
  17. List. addlast ("200 ");
  18. // When the generic version of ienumerable is used, foreach is also of type security. In some cases, it is impossible to write types other than string.
  19. Foreach (string I in List)
  20. {
  21. Console. writeline (I );
  22. }
  23. Console. Readline ();
  24. }
  25. }
  26. //// The definition of a generic class is similar to that of a general class. If you use a generic type declaration, this type can be used as a field member or a parameter type.
  27. Class initialize listnode <t>
  28. {
  29. // Project value
  30. Private T value;
  31. Private synchronized listnode <t> next;
  32. Private synchronized listnode <t> Prev;
  33. Public writable listnode (T value)
  34. {
  35. This. value = value;
  36. }
  37. Public T value
  38. {
  39. // Read-only attribute
  40. Get
  41. {
  42. Return this. value;
  43. }
  44. }
  45. // Next
  46. Public writable listnode <t> next
  47. {
  48. Get {return next ;}
  49. Set {next = value ;}
  50. }
  51. // Previous
  52. Public writable listnode <t> Prev
  53. {
  54. Get {return Prev ;}
  55. Set {Prev = value ;}
  56. }
  57. }
  58. Class Sequence List <t>: ienumerable <t>
  59. {
  60. // The first
  61. Private synchronized listnode <t> first;
  62. Internal internal listnode <t> first
  63. {
  64. Get {return first ;}
  65. Set {First = value ;}
  66. }
  67. // Last
  68. Private synchronized listnode <t> last;
  69. Internal internal listnode <t> last
  70. {
  71. Get {return last ;}
  72. Set {last = value ;}
  73. }
  74. // Insert from the back
  75. Public writable listnode <t> addlast (T node)
  76. {
  77. Inclulistnode <t> newnode = new inclulistnode <t> (node );
  78. If (first = NULL)
  79. {
  80. // The last reference is always updated.
  81. First = newnode;
  82. Last = first;
  83. }
  84. Else
  85. {
  86. // Point the reference of the next node of the current last node to a new object.
  87. Last. Next = newnode;
  88. // Update the last Node
  89. Last = newnode;
  90. }
  91. Return newnode;
  92. }
  93. Public ienumerator <t> getenumerator ()
  94. {
  95. Repeated listnode <t> current = first;
  96. While (current! = NULL)
  97. {
  98. Yield Return Current. value;
  99. // The address of the new reference pointing to the next node
  100. Current = current. Next;
  101. }
  102. }
  103. Ienumerator ienumerable. getenumerator ()
  104. {
  105. Return getenumerator ();
  106. }
  107. }
  108. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.