Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #ifndef DLINKEDLIST_H
  2. #define DLINKEDLIST_H
  3.  
  4. #include "Header.h"
  5. #include "DListNode.h"
  6. #include "DListIterator.h"
  7.  
  8. class DLinkedList
  9. {
  10. public:
  11. DListNode* m_head;
  12. DListNode* m_tail;
  13. int m_count;
  14.  
  15. DLinkedList()
  16. {
  17. m_head = 0;
  18. m_tail = 0;
  19. m_count = 0;
  20. }
  21.  
  22. ~DLinkedList()
  23. {
  24. // temporary node pointers.
  25. DListNode* itr = m_head;
  26. DListNode* next;
  27. while (itr != 0)
  28. {
  29. // save the pointer to the next node.
  30. next = itr->m_next;
  31. // delete the current node.
  32. delete itr;
  33. // make the next node the current node.
  34. itr = next;
  35. }
  36. }
  37.  
  38. void Append(Player p_data)
  39. {
  40. if (m_head == 0)
  41. {
  42. // create a new head node.
  43. m_head = m_tail = new DListNode;
  44. m_head->m_data = p_data;
  45. }
  46. else
  47. {
  48. // insert a new node after the tail and reset the tail.
  49. m_tail->InsertAfter(p_data);
  50. m_tail = m_tail->m_next;
  51. }
  52. m_count++;
  53. }
  54.  
  55. void Prepend(Player p_data)
  56. {
  57. // create the new node.
  58. DListNode* newnode = new DListNode;
  59. newnode->m_data = p_data;
  60. newnode->m_next = m_head;
  61. // set the head node and the tail node if needed.
  62. m_head = newnode;
  63. if (m_tail == 0)
  64. m_tail = m_head;
  65. m_count++;
  66. }
  67.  
  68. void RemoveHead()
  69. {
  70. DListNode* node = 0;
  71.  
  72. if (m_head != 0)
  73. {
  74. // make node point to the next node.
  75. node = m_head->m_next;
  76. // then delete the head and make the pointer point to node.
  77.  
  78. delete m_head;
  79. m_head = node;
  80.  
  81. // if the head is null, then you’ve just deleted the only node
  82. // in the list. set the tail to 0.
  83. if (m_head == 0)
  84. m_tail = 0;
  85. m_count--;
  86. }
  87. }
  88.  
  89. void RemoveTail()
  90. {
  91. DListNode* node = m_head;
  92. // if the list isn’t empty, then remove a node.
  93. if (m_head != 0)
  94. {
  95. // if the head is equal to the tail, then
  96. // the list has 1 node, and you are removing it.
  97. if (m_head == m_tail)
  98. {
  99. // delete the node and set both pointers
  100. // to 0.
  101. delete m_head;
  102. m_head = m_tail = 0;
  103. }
  104. else
  105. {
  106. // skip ahead until you find the node
  107. // right before the tail node
  108. while (node->m_next != m_tail)
  109. node = node->m_next;
  110. // make the tail point to the node before the
  111. // current tail and delete the old tail.
  112. m_tail = node;
  113. delete node->m_next;
  114. node->m_next = 0;
  115. }
  116. m_count--;
  117. }
  118. }
  119.  
  120. DListIterator GetIterator()
  121. {
  122. return DListIterator(this, m_head);
  123. }
  124. };
  125.  
  126. class DListIterator
  127. {
  128. public:
  129. DListNode* m_node;
  130. DLinkedList* m_list;
  131.  
  132. DListIterator(DLinkedList* p_list = 0, DListNode* p_node = 0)
  133. {
  134. m_list = p_list;
  135. m_node = p_node;
  136. }
  137.  
  138. void Start()
  139. {
  140. if (m_list != 0)
  141. m_node = m_list->m_head;
  142. }
  143.  
  144. void Forth()
  145. {
  146. if (m_node != 0)
  147. m_node = m_node->m_next;
  148. }
  149.  
  150. Player Item()
  151. {
  152. return m_node->m_data;
  153. }
  154.  
  155. bool Valid()
  156. {
  157. return (m_node != 0);
  158. }
  159. };
  160.  
  161. #endif
  162.  
  163. WARNINGS/ERRORS
  164. Error 1 error C2146: syntax error : missing ';' before identifier 'GetIterator'
  165. Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  166. Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  167. Warning 4 warning C4183: 'GetIterator': missing return type; assumed to be a member function returning 'int'
  168. Error 5 error C3861: 'DListIterator': identifier not found
  169. Error 6 error C2440: 'initializing' : cannot convert from 'int' to 'DListIterator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement