fork download
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. using namespace std;
  5. #include "CardLinkedList.h"
  6.  
  7. CardLinkedList::CardLinkedList()
  8. {
  9. head = NULL;
  10. tail = NULL;
  11. }
  12.  
  13. CardLinkedList::~CardLinkedList()
  14. {
  15. CardNode* current = head;
  16. while (current != NULL)
  17. {
  18. CardNode* next = current->getNext();
  19. delete current;
  20. current = next;
  21. }
  22. head = NULL;
  23. tail = NULL;
  24. }
  25.  
  26. /* addHead
  27.  * add an item to the front of the linked list
  28.  */
  29.  
  30. void CardLinkedList::addHead(Card* c)
  31. {
  32. CardNode *l = new CardNode(c);
  33.  
  34. if (head == NULL)
  35. {
  36. head = l;
  37. tail = l;
  38. }
  39.  
  40. else
  41. {
  42. l->setNext(head);
  43. head = l;
  44. for (tail= l ; tail->getNext() != NULL ; tail->getNext())
  45. tail = tail->getNext();
  46. }
  47. }
  48.  
  49. /* addTail
  50.  * Add an item to the end (tail) of the list
  51.  */
  52.  
  53. void CardLinkedList::addTail(Card* c)
  54. {
  55. CardNode *l = new CardNode(c);
  56. if (head == NULL)
  57. {
  58. head = l;
  59. tail = l;
  60. }
  61. else
  62. {
  63. CardNode *l2;
  64. for(l2 = head;l2->getNext() != NULL; l2 = l2->getNext());
  65. l2->setNext(l);
  66. tail = l2->getNext();
  67. }
  68. }
  69.  
  70. /* removeHead
  71.  * Removes the item at the head of the list
  72.  */
  73.  
  74. bool CardLinkedList::removeHead()
  75. {
  76. // if the list is empty, return 0
  77. if (head == NULL)
  78. return false;
  79.  
  80. if (head->getNext() == NULL)
  81. {
  82. delete head;
  83. head = NULL;
  84. tail = NULL;
  85. return true;
  86. }
  87.  
  88. // set a temporary pointer to head
  89. CardNode *tmp = head;
  90. // advance head so it points to the next one
  91. head = head->getNext();
  92. // delete the IntNode
  93. delete tmp;
  94. // return the value
  95. return true;
  96. }
  97.  
  98. /* removeTail
  99.  * removes the last (tail) item from a linked list
  100.  * Returns true if it removed something, false if the list was empty
  101.  */
  102. bool CardLinkedList::removeTail()
  103. {
  104. CardNode* tmp = head;
  105.  
  106. if (head == NULL)
  107. return false;
  108.  
  109. if (head->getNext()==NULL)
  110. {
  111. delete head;
  112. head = NULL;
  113. tail = NULL;
  114. return true;
  115. }
  116.  
  117. while (tmp->getNext() != tail)
  118. tmp = tmp->getNext();
  119.  
  120. delete tail;
  121. tmp->setNext(NULL);
  122. tail = tmp;
  123. return true;
  124. }
  125.  
  126. /* peekHead
  127.  * return the item at the head of the list
  128.  * If the list is empty, return 0
  129.  */
  130. Card* CardLinkedList::peekHead()
  131. {
  132. CardNode* current = head;
  133. if (current != NULL)
  134. return current->getValue();
  135. else
  136. return 0;
  137. }
  138.  
  139. /* peekTail
  140.  * return the item at the end (tail) of the list
  141.  * If the list is empty, return 0
  142.  * TODO: Implement this function. Make sure you take advantage of the tail ptr
  143.  */
  144. Card* CardLinkedList::peekTail()
  145. {
  146. CardNode* current = head;
  147. if (current == NULL)
  148. return 0;
  149. else
  150. return tail->getValue();
  151. }
  152.  
  153. /* printList
  154.  *
  155.  * print all of the items in the linked list
  156.  */
  157. void CardLinkedList::printList()
  158. {
  159.  
  160. CardNode *l;
  161. printf("List: ");
  162. for(l = head; l != NULL; l = l->getNext())
  163. {
  164. l->printCardNode();
  165. printf(", ");
  166. }
  167. printf("\n");
  168.  
  169. /* Your print method printList has the following key features,
  170.  * some of which may need adjustment:
  171.  * tmp->printIntNode(); in its loop
  172.  * printf(", "); in its loop
  173.  * After the loop, printf("\n");
  174.  */
  175.  
  176. }
  177.  
  178. void CardLinkedList::addSorted(Card *c)
  179. {
  180.  
  181. }
  182.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:28: fatal error: CardLinkedList.h: No such file or directory
 #include "CardLinkedList.h"
                            ^
compilation terminated.
stdout
Standard output is empty