Guest User

Untitled

a guest
Aug 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. Sortable linked list of objects
  2. class ListOfMessages
  3. {
  4. private int m_nSize;
  5. public Message m_cListStart;
  6. //public Message m_cNextItem;
  7. public Message m_cLastItem;
  8.  
  9. public ListOfMessages()
  10. {
  11. m_nSize = 0;
  12. m_cListStart = null;
  13. //m_cNextItem = null;
  14. }
  15.  
  16. public int Count
  17. {
  18. get { return m_nSize; }
  19. }
  20.  
  21. public string Display(Message cMessage)
  22. {
  23. return "Message: " + cMessage.m_strMessage + "nPriority: " + cMessage.m_strPriority;
  24. }
  25.  
  26. //list additions
  27. public int Add(Message newMessage)
  28. {
  29. Message nextMessage = new Message();
  30. //inserts objects at the end of the list
  31. if (m_nSize == 0)
  32. {
  33. m_cListStart = newMessage;
  34. //m_cLastItem = newMessage;
  35. }
  36. else
  37. {
  38. Message CurrentMessage = m_cListStart;
  39.  
  40. if (newMessage.m_strPriority == "High")
  41. {
  42.  
  43. if (m_cListStart.m_strPriority != "High")
  44. {
  45. //Make the object at the start of the list point to itself
  46. CurrentMessage.m_cNext = m_cListStart;
  47. //Replace the object at the start of the list with the new message
  48. m_cListStart = newMessage;
  49.  
  50. }
  51. else
  52. {
  53. Message LastMessage = null;
  54.  
  55. for (int iii = 0; iii < m_nSize; iii++)//(newmessage.m_strpriority == iii.m_strpriority)
  56. //&& (iii.m_cnext == null);)
  57. {
  58. if (m_cListStart.m_strPriority != "High")
  59. {
  60. nextMessage = newMessage;
  61. nextMessage.m_cNext =
  62. CurrentMessage = nextMessage;
  63. //LastMessage.m_cNext = CurrentMessage;
  64. }
  65. LastMessage = CurrentMessage;
  66.  
  67. if (m_cListStart.m_cNext != null)
  68. m_cListStart = m_cListStart.m_cNext;
  69. }
  70. }
  71. //iii = iii.m_cnext;
  72. }
  73. // for (int iii = m_cListStart; ; iii = iii.m_cNext)//(newMessage.m_strPriority == iii.m_strPriority)
  74. // //&& (iii.m_cNext == null);)
  75. //{
  76. // //Message lastMessage = iii;
  77. // if (iii.m_strPriority != iii.m_strPriority)
  78. // {
  79. // //iii.m_cNext = newMessage;
  80. // newMessage.m_cNext = iii.m_cNext;
  81. // iii.m_cNext = newMessage;
  82. // }
  83.  
  84.  
  85. //m_cLastItem.m_cNext = newMessage;
  86. }
  87. //m_cLastItem = newMessage;
  88. return m_nSize++;
  89. }
  90.  
  91. public Message Pop()
  92. {
  93. //Message Current = m_cListStart;
  94. //if the object at the start of the list has another object after it, make that object the start of the list
  95. //and decrease the size by 1 after popping an object off if there is more than 1 object after the start of the list
  96. if (m_cListStart.m_cNext != null)
  97. {
  98. m_cListStart = m_cListStart.m_cNext;
  99. }
  100. if (m_nSize > 0)
  101. m_nSize--;
  102. else
  103. m_cListStart = null;
  104. return m_cListStart;
  105. //if (m_cListStart.m_cNext != null)
  106. // m_cListStart = m_cListStart.m_cNext;
  107. //if (m_nSize > 1)
  108. // m_nSize--;
  109. //return m_cListStart;
  110. }
  111.  
  112. class Node<T> : IComparable<T>
  113. {
  114. public int Priority {set;get;}
  115. public T Data {set;get;}
  116. public Node<T> Next {set;get;}
  117. public Node<T> Previous {set;get;}
  118.  
  119. // you need to implement IComparable here for sorting.
  120. }
  121.  
  122. class LinkedList<T> : IEnumerable<T> where T: IComparable
  123. {
  124. public Node<T> Head {set;get;}
  125. public Node<T> Tail {set;get;}
  126.  
  127. // set of constructors
  128. //.....
  129.  
  130. public void Insert(Node<T> node)
  131. {
  132. // you can do recursive or iterative impl. very easy.
  133. }
  134.  
  135. // other public methods such as remove, insertAfter, insert before, insert last etc.
  136.  
  137. public void Sort()
  138. {
  139. // easiest solution is to use insertion sort based on priority.
  140. }
  141.  
  142. }
Add Comment
Please, Sign In to add comment