Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. //Zach Bowers
  2. //LinkedList HW5
  3. //LinkedList.cpp
  4.  
  5. #include "LinkedList.h"
  6. #include <iostream>
  7.  
  8. // creating an empty node
  9. LinkedList ::LinkedList() {
  10. //dynamically allocating a new node
  11. headPtr = new Node;
  12.  
  13. //headptr of nextptr points to null pointer
  14. headPtr ->nextPtr= nullptr;
  15. headPtr->data=' ';
  16. }
  17.  
  18. bool LinkedList ::insertAtFront(char value) {
  19.  
  20. //create a new node
  21. Node *newNode = new Node;
  22.  
  23. //fill the new node with data
  24. newNode->data=value;
  25.  
  26. //new node equals what head pointer points to
  27. newNode->nextPtr=headPtr->nextPtr;
  28.  
  29. //connect head pointer of nextptr to new node
  30. headPtr->nextPtr=newNode;
  31. return true;
  32. }
  33.  
  34. bool LinkedList:: insertAtBack(char value)
  35.  
  36. {
  37. //if headptr of nextptr equals nullptr
  38. if(headPtr->nextPtr == nullptr)
  39.  
  40. //if empty insert node at front
  41. return insertAtFront(value);
  42.  
  43. //temp node connects to what head pointer is pointing to
  44. Node *temp = headPtr->nextPtr;
  45.  
  46. //create new node
  47. Node *newNode = new Node;
  48.  
  49. //Fill node with data
  50. newNode->data = value;
  51.  
  52. //new node of nextptr equal to nullptr
  53. newNode->nextPtr = nullptr;
  54.  
  55. //loop while temp node of nextptr doesnt equal to nullptr
  56. while(temp->nextPtr != nullptr)
  57.  
  58. {
  59. //temp equals to temp of nextptr
  60. temp = temp->nextPtr;
  61.  
  62. }
  63. //temp of nextptr points to new node
  64. temp->nextPtr = newNode;
  65.  
  66. return true;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. bool LinkedList:: deleteAtFront()
  73.  
  74. {
  75. //if list is empty
  76. if(headPtr->nextPtr == nullptr)
  77.  
  78. return false;
  79.  
  80. //create temp node
  81. Node *temp = headPtr->nextPtr;
  82.  
  83. //points to first node
  84. headPtr->nextPtr = temp->nextPtr;
  85.  
  86. //delete node
  87. delete(temp);
  88.  
  89. return true;
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. bool LinkedList ::insertBeforePosition(char value, int index)
  97.  
  98. {
  99. //empty list
  100. if(index == 0)
  101.  
  102. return false;
  103.  
  104. //headptr of nextptr equals nullptr and the index >1
  105. if(headPtr->nextPtr == nullptr && index>1)
  106.  
  107. return false;
  108.  
  109. //checks to see if at front of list
  110. if(index == 1)
  111.  
  112. //returns insert at front function
  113. return insertAtFront(value);
  114.  
  115. //create temp node
  116. Node *temp = headPtr->nextPtr;
  117.  
  118. int i=1;
  119.  
  120. //new node
  121. Node *newNode = new Node;
  122.  
  123. //fill node with data
  124. newNode->data = value;
  125.  
  126. //connect new node to nullptr
  127. newNode->nextPtr = nullptr;
  128.  
  129. //loop while temp node doesnt equal nullptr
  130. while(temp != nullptr)
  131.  
  132. {
  133. //checks to see if at back of list
  134. if(i == index-1)
  135.  
  136. {
  137.  
  138. //temp node of nextptr equals nullptr
  139. if(temp->nextPtr == nullptr)
  140.  
  141. //return insert at back function
  142. return insertAtBack(value);
  143.  
  144. //new node connects to nextptr
  145. else{
  146.  
  147. //new node of nextptr equal to temp of nextptr
  148. newNode->nextPtr = temp->nextPtr;
  149.  
  150. //connect head ptr to new node
  151. temp->nextPtr = newNode;
  152.  
  153. return true;
  154.  
  155. }
  156.  
  157. }
  158. //+1 to i
  159. i++;
  160.  
  161. //temp connects to temp of nextptr
  162. temp = temp->nextPtr;
  163.  
  164. }
  165.  
  166. return false;
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. bool LinkedList::deleteAtBack()
  177.  
  178. {
  179. //empty list
  180. if(headPtr->nextPtr == nullptr)
  181.  
  182. return false;
  183.  
  184. //create temp node
  185. Node *temp = headPtr->nextPtr;
  186.  
  187. //if list has one node
  188. if(temp->nextPtr == nullptr)
  189.  
  190. //delete at front node
  191. return deleteAtFront();
  192.  
  193. //loop while nextptr doesnt point to nullptr
  194. while(temp->nextPtr->nextPtr!=nullptr)
  195.  
  196. {
  197. //temp equals temp of nextptr
  198. temp = temp->nextPtr;
  199.  
  200. }
  201. //create del node
  202. Node *del = temp->nextPtr;
  203.  
  204. //temp of nextptr equals nullptr
  205. temp->nextPtr = nullptr;
  206.  
  207. //delete del node
  208. delete(del);
  209.  
  210. return true;
  211.  
  212. }
  213.  
  214.  
  215. bool LinkedList::deletePosition(int index) {
  216.  
  217. Node *tempNode=headPtr;
  218.  
  219. Node *prevNode=headPtr;
  220.  
  221. //fill the new node with data
  222. //loop until the position of the previous node is
  223. while (tempNode->nextPtr !=nullptr)
  224. {
  225. //check if the current node has the same data
  226. if (tempNode->data==index)
  227. {
  228.  
  229. break;
  230. }
  231. prevNode=tempNode;
  232. tempNode=tempNode->nextPtr;
  233. }
  234. //now the new node should be added to point to the next pointer of the temp node
  235. //connect new node to what head pointer points to
  236. prevNode->nextPtr=tempNode->nextPtr;
  237.  
  238. // connect head pointer to new node
  239. tempNode->nextPtr= nullptr;
  240.  
  241. //delete tempNode
  242. delete tempNode;
  243. return true;
  244. }
  245.  
  246. void LinkedList:: clear()
  247.  
  248. {
  249. //headptr equals headptr of nextptr
  250. headPtr = headPtr->nextPtr;
  251.  
  252. //loop while headptr doesnt equal nullptr
  253. while(headPtr != nullptr)
  254.  
  255. {
  256. //temp node equals headptr
  257. Node *temp = headPtr;
  258.  
  259. //headptr equals headptr of nextptr
  260. headPtr =headPtr->nextPtr;
  261.  
  262. //delete temp node
  263. delete(temp);
  264.  
  265. }
  266.  
  267. }
  268.  
  269.  
  270.  
  271. ostream& operator << ( ostream& out, LinkedList &list)
  272. {
  273. Node *ptr=list.headPtr->nextPtr;
  274. cout <<"headPtr->";
  275. while(ptr !=nullptr)
  276. {
  277. //ptr of nextptr equals nullptr
  278. if(ptr->nextPtr==nullptr)
  279. out <<ptr->data<<"->nullptr"<<endl;
  280. else
  281. out<<ptr->data<<"->";
  282. ptr = ptr->nextPtr;
  283. }
  284. return out;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement