Advertisement
Guest User

Untitled

a guest
May 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include "IntList.h"
  6.  
  7. IntList::IntList() {
  8. headNode = NULL;
  9. tailNode = NULL;
  10. }
  11.  
  12. IntList::~IntList() {
  13.  
  14. IntNode *temp = 0;
  15. while (headNode != NULL) {
  16. temp = headNode->next;
  17. delete headNode;
  18. headNode = temp;
  19. }
  20. }
  21.  
  22. void IntList::display() const {
  23. IntNode *temp = 0;
  24.  
  25. temp = headNode;
  26. while (temp != NULL) {
  27. cout << " " << temp->data;
  28. temp = temp->next;
  29. }
  30. return;
  31. }
  32.  
  33. void IntList::pop_front() {
  34. IntNode *temp = 0;
  35.  
  36. temp = headNode;
  37. headNode = headNode->next;
  38. delete temp;
  39. if (empty()) {
  40. headNode = tailNode = 0;
  41. }
  42. return;
  43. }
  44.  
  45. bool IntList::empty() const {
  46. if (headNode == NULL) {
  47. return true;
  48. } else {
  49. return false;
  50. }
  51.  
  52. }
  53.  
  54. void IntList::push_front(int value) {
  55. IntNode *temp = new IntNode(value); //default constructor for node struct
  56. //temp is a pointer that points to newly constructed node
  57. temp->data = value;
  58. temp->next = headNode; //next points to headNode
  59. headNode = temp; //temp becomes headNode
  60.  
  61. return;
  62.  
  63. }
  64.  
  65. IntList::IntList(const IntList& cpy) {
  66. headNode = tailNode = 0;
  67. if (!cpy.empty()) {
  68.  
  69. IntNode *n = new IntNode(0);
  70. *n = *(cpy.headNode); //????
  71.  
  72. while (n != NULL) {
  73. push_back(n->data);
  74. n = n->next;
  75. }
  76. }
  77. return;
  78. }
  79.  
  80. IntList & IntList::operator=(const IntList &rhs) {
  81. if (rhs.empty()) {
  82. headNode = tailNode = 0;
  83. return *this;
  84. } else if(rhs.empty()) {
  85. headNode = 0;
  86. tailNode = 0;
  87. }
  88. else if (this != &rhs) {
  89. clear(); //replaces
  90. IntNode *n = 0;
  91. n = rhs.headNode;
  92.  
  93. while (n != NULL) {
  94. push_back(n->data);
  95. n = n->next;
  96. }
  97.  
  98. }
  99. return *this;
  100. }
  101.  
  102. void IntList::push_back(int value) {
  103. IntNode *newTail = new IntNode(value);
  104.  
  105. if (headNode == NULL) {
  106. headNode = newTail;
  107. tailNode = newTail;
  108. } else {
  109. tailNode->next = newTail;
  110. tailNode = newTail;
  111. }
  112.  
  113. return;
  114.  
  115. }
  116.  
  117. void IntList::selection_sort() {
  118. if (empty()) {
  119. return;
  120. } else {
  121. IntNode *min = 0;
  122. IntNode *first = 0;
  123. IntNode *second = 0;
  124.  
  125. for (first = headNode; first->next != NULL; first = first->next) {
  126. min = first; //min points to whatever first is pointing to
  127.  
  128. for (second = first->next; second != NULL; second = second->next) {
  129.  
  130. if (second->data < min->data) {
  131. min = second;
  132. }
  133. }
  134. int temp = 0;
  135. temp = first->data;
  136. first->data = min->data;
  137. min->data = temp;
  138.  
  139. }
  140. }
  141. return;
  142. }
  143.  
  144. void IntList::insert_ordered(int value) {
  145.  
  146. /*IntNode *newNode = new IntNode(value);
  147. newNode->data = value;
  148.  
  149. IntNode *n = 0;
  150. IntNode *temp = 0;
  151. //compare newNode's data with list's data
  152. for (n = headNode; n->next != NULL; n = n->next) {
  153.  
  154. if ((n->data < value) && ((n->next)->data > value)) {
  155. temp = n->next;
  156. n->next = newNode;
  157. newNode->next = temp;
  158. break;
  159. }
  160. }
  161. return;*/
  162. push_back(value);
  163. selection_sort();
  164. return;
  165.  
  166. }
  167.  
  168. void IntList::remove_duplicates() { //selection sort algorithm required?
  169. if (empty()) {
  170. return;
  171. } else {
  172. IntNode *first = 0;
  173. IntNode *second = 0;
  174. IntNode *previous = 0;
  175.  
  176. for (first = headNode; first!= NULL; first = first->next) {
  177. previous = first;
  178. for (second = first->next; second != NULL; second = previous->next) {
  179. if (second->data == first->data) {
  180. if (second == tailNode) {
  181. delete second;
  182. tailNode = previous;
  183. tailNode->next = NULL;
  184.  
  185. } else {
  186. previous->next = second->next;
  187. delete second;
  188. }
  189. } else {
  190. previous = previous->next;
  191. }
  192.  
  193. }
  194. }
  195.  
  196. }
  197. }
  198.  
  199. ostream & operator<<(ostream &out, const IntList &rhs) {
  200. if (rhs.empty()) {
  201. return out;
  202. } else {
  203. IntNode *n = new IntNode(0);
  204. n = rhs.headNode;
  205. out << n->data;
  206. while (n->next != NULL) {
  207. n = n->next;
  208. out << " " << n->data;
  209. }
  210. }
  211. return out;
  212. }
  213.  
  214. void IntList::clear() {
  215. while (!empty()) {
  216. pop_front();
  217. }
  218. return;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement