Guest User

Untitled

a guest
Dec 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. main.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include "Header.h"
  6.  
  7.  
  8.  
  9. LinkedList list;
  10.  
  11. do {
  12. cout << "n~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  13. cout << "Linked List Options:n" << endl;
  14. cout << "1 - Add an item" << endl;
  15. cout << "2 - Find an item" << endl;
  16. cout << "3 - Remove an item" << endl;
  17. cout << "4 - Display list" << endl;
  18. cout << "5 - Destroy list" << endl;
  19. cout << "6 - Destroy list and Exit" << endl;
  20. cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  21. cin >> option;
  22.  
  23. switch (option)
  24. {
  25.  
  26. case 1:
  27. {
  28. cout << "Enter a number to add to the list:" << endl;
  29. cin >> num;
  30. cout << "Choose a place to insert the number:n 1 - First n 2 - Last" << endl;
  31.  
  32. do {
  33. cin >> choice;
  34.  
  35. if (choice == 1)
  36. {
  37. placement = true;
  38. break;
  39. }
  40. else if (choice == 2)
  41. {
  42. placement = false;
  43. break;
  44. }
  45. else
  46. {
  47. cout << "nInvalid entry. Choose again: " << endl;
  48. }
  49. }while(choice != 1 || choice != 2);
  50.  
  51. list.insertItem(num, placement);
  52. break;
  53. }
  54.  
  55. case 2:
  56. {
  57. cout << "Enter a value to search for:" << endl;
  58. cin >> num;
  59. list.findItem(num);
  60. break;
  61. }
  62.  
  63. case 3:
  64. {
  65. cout << "Remove item via: n1 - Item Position n2 - Value" << endl;
  66. cin >> choice;
  67.  
  68. if (choice == 1)
  69. {
  70. cout << "Enter an item's position, the first would be "1"" << endl;
  71. cin >> num;
  72. list.removeNodePosition(num);
  73. }
  74. else if (choice == 2)
  75. {
  76. cout << "Enter a value to remove:" << endl;
  77. cin >> num;
  78. list.removeValue(num);
  79. }
  80. else
  81. cout << "Invalid entry... restarting menu" << endl;
  82. break;
  83. }
  84.  
  85. case 4:
  86. {
  87. list.display();
  88. break;
  89. }
  90.  
  91. case 5:
  92. {
  93. list.destroyList();
  94. break;
  95. }
  96. case 6:
  97. {
  98. list.destroyList();
  99. break;
  100. }
  101. };
  102.  
  103. } while (option != 6);
  104.  
  105. return 0;
  106. };
  107.  
  108.  
  109.  
  110. // driver.cpp
  111. // GemperlineA-Lab9
  112. //
  113. // Created by Adam Gemperline on 12/15/17.
  114. // Copyright © 2017 Adam Gemperline. All rights reserved.
  115. //
  116.  
  117.  
  118. #include "Header.h"
  119. #include <iostream>
  120. using namespace std;
  121.  
  122.  
  123. void LinkedList::insertItem(int n, bool atHead)
  124. {
  125. Node *newNode;
  126. Node *nodePtr;
  127. nodePtr = nullptr;
  128.  
  129. //Allocate the new node
  130. newNode = new Node;
  131.  
  132. //Store the value
  133. newNode->val = n;
  134.  
  135. //Redirect pointer to null
  136. newNode->next = nullptr;
  137.  
  138. if (atHead == true)
  139. {
  140. if (!head)
  141. {
  142. head = newNode;
  143. cout << "There were no nodes, first node created" << endl;
  144. }
  145. else
  146. {
  147. newNode->next = head;
  148. head = newNode;
  149. }
  150. }
  151. else
  152. {
  153. if (!head)
  154. {
  155. // if head is NULL, set the new node at the head of the list
  156. head = newNode;
  157. }
  158. else
  159. {
  160. nodePtr = head;
  161.  
  162. // traverse through the list
  163. while (nodePtr->next)
  164. {
  165. //find the last node in the list
  166. nodePtr = nodePtr->next;
  167. };
  168.  
  169. //insert newNode as the last node
  170. nodePtr->next = newNode;
  171. };
  172. }
  173. }
  174.  
  175.  
  176. void LinkedList::display()
  177. {
  178. Node *nodePtr;
  179. nodePtr = head;
  180.  
  181. while (nodePtr != nullptr)
  182. {
  183. cout << nodePtr->val << " " << endl;
  184. nodePtr = nodePtr->next;
  185. };
  186. if (nodePtr == nullptr)
  187. {
  188. cout << "nEnd of list" << endl;
  189. }
  190.  
  191. }
  192.  
  193. int LinkedList::findItem(int n)
  194. {
  195. Node *nodePtr;
  196. nodePtr = head;
  197.  
  198. while (nodePtr != nullptr)
  199. {
  200. if (nodePtr->val == n)
  201. {
  202. cout << "Yes, the list contains that number!" << endl;
  203. nodePtr = nodePtr->next;
  204. }
  205. else if (nodePtr->val != n && nodePtr != nullptr)
  206. nodePtr = nodePtr->next;
  207. else
  208. cout << "That number is not in the list..." << endl;
  209. }
  210. return 0;
  211. }
  212.  
  213. bool LinkedList::removeNodePosition(int n)
  214. {
  215. Node *nodePtr;
  216. Node *previousNode;
  217.  
  218. //if the list is empty, do nothing
  219. if (!head)
  220. {
  221. cout << "The list is empty." << endl;
  222. }
  223.  
  224. for (int counter = 1; counter <= n; counter++)
  225. {
  226. //initialize nodePtr to head of the list
  227. nodePtr = head;
  228.  
  229. //skip nodes whose value does not equal search value
  230. while (nodePtr != nullptr && counter != n)
  231. {
  232. previousNode = nodePtr;
  233. nodePtr = nodePtr->next;
  234. };
  235.  
  236. if (counter == n)
  237. {
  238. previousNode = nodePtr;
  239. previousNode->next = nodePtr->next;
  240. delete nodePtr;
  241. cout << "nItem deleted." << endl;
  242. }
  243. }
  244. return 0;
  245. }
  246.  
  247.  
  248. bool LinkedList::removeValue(int n)
  249. {
  250. Node *nodePtr;
  251. Node *previousNode;
  252.  
  253. //initialize nodePtr to head of the list
  254. nodePtr = head;
  255.  
  256.  
  257. //if the list is empty, do nothing
  258. if (!head)
  259. {
  260. cout << "The list is empty." << endl;
  261. }
  262.  
  263. // determine if the first node is the one
  264. if (head->val == n)
  265. {
  266. nodePtr = head->next;
  267. delete head;
  268. head = nodePtr;
  269. cout << "nItem deleted." << endl;
  270. }
  271. else
  272. {
  273. //skip nodes whose value does not equal search value
  274. while (nodePtr != nullptr && nodePtr->val != n)
  275. {
  276. previousNode = nodePtr;
  277. nodePtr = nodePtr->next;
  278. }
  279.  
  280. //if nodePtr is not at end of list, link previous node to the node after nodePtr
  281. if (nodePtr)
  282. {
  283. previousNode = nodePtr;
  284. previousNode->next = nodePtr->next;
  285. delete nodePtr;
  286. cout << "nItem deleted." << endl;
  287. }
  288. }
  289. return 0;
  290. }
  291.  
  292. void LinkedList::destroyList()
  293. {
  294. char choice;
  295.  
  296. cout << "Are you sure you want to destroy? (Y/N)" << endl;
  297.  
  298. do {
  299. cin >> choice;
  300.  
  301. if (choice == 'Y' || choice == 'y')
  302. {
  303. LinkedList::~LinkedList();
  304. return;
  305. }
  306. else if (choice == 'N' || choice == 'n')
  307. {
  308. return;
  309. }
  310. else
  311. cout << "Invalid entry. Choose again:" << endl;
  312. }while(choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n');
  313. }
  314.  
  315. LinkedList::~LinkedList()
  316. {
  317. int count = 1;
  318.  
  319. Node *nodePtr;
  320. Node *nextNode;
  321.  
  322. // position nodePtr at the head of list
  323. nodePtr = head;
  324.  
  325. // loop to traverse the list
  326. while(nodePtr != nullptr)
  327. {
  328. //set next pointer to the next node after nodePtr
  329. nextNode = nodePtr->next;
  330.  
  331. delete nodePtr;
  332.  
  333. cout << "Node " << count << " destroyed..." << endl;
  334.  
  335. //set node Ptr to the next node
  336. nodePtr = nextNode;
  337. count ++;
  338. }
  339. cout << "List destroyed" << endl;
  340. }
  341.  
  342.  
  343.  
  344.  
  345. // Header.h
  346.  
  347. #pragma once
  348.  
  349. #ifndef HEADER_H
  350. #define HEADER_H
  351.  
  352. // Declare a structure for the list
  353. struct Node
  354. {
  355. int val;
  356. Node *next;
  357. };
  358.  
  359.  
  360.  
  361. class LinkedList
  362. {
  363. private:
  364.  
  365. int nodeCount;
  366. Node *head;
  367.  
  368. public:
  369.  
  370. // Constructor
  371. LinkedList()
  372. {
  373. head = nullptr;
  374. };
  375.  
  376. // Destructor
  377. ~LinkedList();
  378.  
  379.  
  380.  
  381. // Linked List Operations
  382. void insertItem(int, bool);
  383. int findItem(int);
  384. //int getNodeCount();
  385. bool removeNodePosition(int);
  386. bool removeValue(int);
  387. void destroyList();
  388. void display();
  389.  
  390. };
  391.  
  392.  
  393. #endif
Add Comment
Please, Sign In to add comment