fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class List {
  5. private:
  6. struct Node {
  7. int data;
  8. Node* next;
  9. };
  10.  
  11. Node* head;
  12.  
  13. public:
  14.  
  15. // 1. Constructor
  16. List() {
  17. head = NULL;
  18. }
  19.  
  20. // 2. Destructor
  21. ~List() {
  22. while (head != NULL) {
  23. Node* temp = head;
  24. head = head->next;
  25. delete temp;
  26. }
  27. }
  28.  
  29. // 3. isEmpty
  30. bool isEmpty() {
  31. return head == NULL;
  32. }
  33.  
  34. // 4. addToHead
  35. void addToHead(int value) {
  36. Node* newNode = new Node;
  37. newNode->data = value;
  38. newNode->next = head;
  39. head = newNode;
  40. }
  41.  
  42. // 5. addToTail
  43. void addToTail(int value) {
  44. Node* newNode = new Node;
  45. newNode->data = value;
  46. newNode->next = NULL;
  47.  
  48. if (head == NULL) {
  49. head = newNode;
  50. return;
  51. }
  52.  
  53. Node* temp = head;
  54.  
  55. while (temp->next != NULL) {
  56. temp = temp->next;
  57. }
  58.  
  59. temp->next = newNode;
  60. }
  61.  
  62. // 6. findNode
  63. int findNode(int value) {
  64. Node* temp = head;
  65. int position = 1;
  66.  
  67. while (temp != NULL) {
  68. if (temp->data == value)
  69. return position;
  70.  
  71. temp = temp->next;
  72. position++;
  73. }
  74.  
  75. return -1;
  76. }
  77.  
  78. // 7. deleteFromHead
  79. int deleteFromHead() {
  80. if (head == NULL)
  81. return -1;
  82.  
  83. Node* temp = head;
  84. int value = temp->data;
  85.  
  86. head = head->next;
  87. delete temp;
  88.  
  89. return value;
  90. }
  91.  
  92. // 8. deleteFromTail
  93. int deleteFromTail() {
  94. if (head == NULL)
  95. return -1;
  96.  
  97. if (head->next == NULL) {
  98. int value = head->data;
  99. delete head;
  100. head = NULL;
  101. return value;
  102. }
  103.  
  104. Node* temp = head;
  105.  
  106. while (temp->next->next != NULL) {
  107. temp = temp->next;
  108. }
  109.  
  110. int value = temp->next->data;
  111. delete temp->next;
  112. temp->next = NULL;
  113.  
  114. return value;
  115. }
  116.  
  117. // 9. printList
  118. void printList() {
  119. Node* temp = head;
  120.  
  121. while (temp != NULL) {
  122. cout << temp->data << " ";
  123. temp = temp->next;
  124. }
  125.  
  126. cout << endl;
  127. }
  128.  
  129. // 10. countList
  130. int countList() {
  131. int count = 0;
  132. Node* temp = head;
  133.  
  134. while (temp != NULL) {
  135. count++;
  136. temp = temp->next;
  137. }
  138.  
  139. return count;
  140. }
  141.  
  142. // 11. getMinimum
  143. int getMinimum() {
  144. if (head == NULL)
  145. return -1;
  146.  
  147. int minimum = head->data;
  148. Node* temp = head->next;
  149.  
  150. while (temp != NULL) {
  151. if (temp->data < minimum)
  152. minimum = temp->data;
  153.  
  154. temp = temp->next;
  155. }
  156.  
  157. return minimum;
  158. }
  159.  
  160. // 12. sumOdd
  161. int sumOdd() {
  162. int sum = 0;
  163. Node* temp = head;
  164.  
  165. while (temp != NULL) {
  166. if (temp->data % 2 != 0)
  167. sum += temp->data;
  168.  
  169. temp = temp->next;
  170. }
  171.  
  172. return sum;
  173. }
  174.  
  175. // 13. copyList
  176. void copyList(const List& other) {
  177. while (head != NULL) {
  178. Node* temp = head;
  179. head = head->next;
  180. delete temp;
  181. }
  182.  
  183. Node* temp = other.head;
  184.  
  185. while (temp != NULL) {
  186. addToTail(temp->data);
  187. temp = temp->next;
  188. }
  189. }
  190. };
  191.  
  192. int main() {
  193. List myList;
  194.  
  195. myList.addToHead(10);
  196. myList.addToTail(20);
  197. myList.addToTail(15);
  198. myList.addToTail(40);
  199.  
  200. cout << "List: ";
  201. myList.printList();
  202.  
  203. cout << "Is Empty: " << myList.isEmpty() << endl;
  204. cout << "Count: " << myList.countList() << endl;
  205. cout << "Minimum: " << myList.getMinimum() << endl;
  206. cout << "Sum of Odd Numbers: " << myList.sumOdd() << endl;
  207. cout << "Position of 15: " << myList.findNode(15) << endl;
  208.  
  209. cout << "Deleted from Head: "
  210. << myList.deleteFromHead() << endl;
  211.  
  212. cout << "Deleted from Tail: "
  213. << myList.deleteFromTail() << endl;
  214.  
  215. cout << "List after deletion: ";
  216. myList.printList();
  217.  
  218. List secondList;
  219. secondList.copyList(myList);
  220.  
  221. cout << "Copied List: ";
  222. secondList.printList();
  223.  
  224. return 0;
  225. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
List: 10 20 15 40 
Is Empty: 0
Count: 4
Minimum: 10
Sum of Odd Numbers: 15
Position of 15: 3
Deleted from Head: 10
Deleted from Tail: 40
List after deletion: 20 15 
Copied List: 20 15