Creating Linear List with Advanced Programming Techniques


/ Published in: C#
Save to your folder(s)

This is a demo how to create a Linear List with APTs make using of Generics, Indexer and the IEnumerator Interface. With this APTs it is possible to create a high performace, type safety class which is available for different control loops techniques and so on.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. namespace MyOwnGeneric
  7. {
  8. class OwnObject<T>
  9. {
  10. private T content;
  11. private OwnObject<T> next;
  12.  
  13. public T Content
  14. {
  15. get { return content; }
  16. set { content = value; }
  17. }
  18.  
  19. public OwnObject<T> Next
  20. {
  21. get { return next; }
  22. set { next = value; }
  23. }
  24.  
  25. public override string ToString()
  26. {
  27. return content.ToString();
  28. }
  29. }
  30.  
  31. class LinList<T> : IEnumerable<T>
  32. {
  33. private OwnObject<T> first;
  34. private OwnObject<T> last;
  35.  
  36. public OwnObject<T> First
  37. {
  38. get { return first; }
  39. set { first = value; }
  40. }
  41.  
  42. public OwnObject<T> Last
  43. {
  44. get { return last; }
  45. set { last = value; }
  46. }
  47.  
  48. public void Add(OwnObject<T> item)
  49. {
  50. if (first == null)
  51. {
  52. first = item;
  53. last = item;
  54. }
  55. else
  56. {
  57. last.Next = item;
  58. last = item;
  59. }
  60. }
  61.  
  62. //function to count items in LinList
  63. public int Count()
  64. {
  65. int counter = 0;
  66. OwnObject<T> position = first;
  67. while (position != null)
  68. {
  69. counter++;
  70. position = position.Next;
  71. }
  72. return counter;
  73. }
  74.  
  75. //function to get the content at a fix position in LinList
  76. public T GetItem(int pos)
  77. {
  78. int counter = 0;
  79. OwnObject<T> position = first;
  80. //T result = null not possible because value type
  81. T result = default(T);
  82. while (position != null)
  83. {
  84. if (counter == pos)
  85. {
  86. result = position.Content;
  87. break;
  88. }
  89. counter++;
  90. position = position.Next;
  91. }
  92. return result;
  93. }
  94.  
  95. public override string ToString()
  96. {
  97. string result = "";
  98. OwnObject<T> position = first;
  99. while (position != null)
  100. {
  101. result += position.ToString();
  102. if (position.Next != null)
  103. result += " - ";
  104. position = position.Next;
  105. }
  106. return result;
  107. }
  108.  
  109. //indexer
  110. public T this[int index]
  111. {
  112. get
  113. {
  114. return this.GetItem(index);
  115. }
  116. }
  117.  
  118. //interface IEnumerable
  119. public IEnumerator<T> GetEnumerator()
  120. {
  121. OwnObject<T> position = first;
  122. while (position != null)
  123. {
  124. yield return position.Content;
  125. position = position.Next;
  126. }
  127. }
  128.  
  129. IEnumerator IEnumerable.GetEnumerator()
  130. {
  131. return GetEnumerator();
  132. }
  133. }
  134.  
  135. class Program
  136. {
  137. static void Main(string[] args)
  138. {
  139. LinList<string> test = new LinList<string>();
  140. OwnObject<string> testcont = new OwnObject<string>();
  141. testcont.Content = "test";
  142. OwnObject<string> testcont2 = new OwnObject<string>();
  143. testcont2.Content = "test2";
  144. OwnObject<string> testcont3 = new OwnObject<string>();
  145. testcont3.Content = "test3";
  146.  
  147. test.Add(testcont);
  148. test.Add(testcont2);
  149. test.Add(testcont3);
  150.  
  151. //using the interface of IEnumerable
  152. foreach (string item in test)
  153. {
  154. Console.WriteLine(item);
  155. }
  156.  
  157. //using the indexer and the item counter
  158. for (int i = 0; i < test.Count(); i++)
  159. {
  160. Console.WriteLine(test[i]);
  161. }
  162. }
  163. }
  164. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.