Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. class Node
  7. {
  8. public:
  9. int value;
  10. Node* prev;
  11. Node* next;
  12.  
  13. Node()
  14. {
  15. value = 0;
  16. prev = NULL;
  17. next = NULL;
  18. }
  19. Node(int val, Node* pre, Node* nex)
  20. {
  21. value = val;
  22. prev = pre;
  23. next = nex;
  24. }
  25. };
  26.  
  27. class DoublyLinkedList
  28. {
  29. public:
  30. Node* head;
  31.  
  32. DoublyLinkedList()
  33. {
  34. head = NULL;
  35. }
  36. ~DoublyLinkedList()
  37. {
  38. Node* current = head;
  39. while (current != NULL)
  40. {
  41. Node* temp = current->next;
  42. delete current;
  43. current = temp;
  44. }
  45. // DO NOT CALL DELETE THIS, THE COMPILER DOES IT.
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void PushToBack(int value)
  54. {
  55. Node *node = new Node(value, NULL, NULL);
  56.  
  57. if (head == NULL) {
  58. head = node;
  59. }
  60. else {
  61. Node *last = head;
  62. while (last->next != NULL)
  63. last = last->next;
  64. last->next = node;
  65. node->prev = last;
  66. }
  67. cout << "pushed Node of value: " << node->value << " to back of List" << endl;
  68. node = NULL; // remove associated memory
  69. delete node;
  70. }
  71.  
  72. void PushToFront(int value)
  73. {
  74. Node *node = new Node(value, NULL, NULL);
  75.  
  76. if (head == NULL) {
  77. head = node;
  78. }
  79. else {
  80. node->next = head;
  81. head->prev = node;
  82. head = node;
  83. }
  84. cout << "pushed Node of value: " << node->value << " to front of List"<< endl;
  85. node = NULL; // remove associated memory
  86. delete node;
  87. }
  88.  
  89. void PopFromBack()
  90. {
  91. Node* current = head;
  92. while (current != NULL)
  93. {
  94. current = current->next;
  95. // A > B > C
  96. // we want to remove C because C is the last,
  97. // tell B that B has no more next
  98. // delete C
  99. if (current->next == NULL)
  100. {
  101. current->prev->next = NULL;
  102. cout << "popped Node of value: " << current->value << endl;
  103. delete current;
  104. current = NULL;
  105. }
  106. }
  107. }
  108.  
  109. void PopFromFront() {
  110. // make sure there is something to pop front
  111. if (GetSize() == 0)
  112. return;
  113. // if popping front the last element in the list
  114. else if (GetSize() == 1)
  115. {
  116. head = NULL;
  117. }
  118. else
  119. {
  120. // create copy of head->next, we will need this
  121. Node *n = head->next;
  122. // prev should point to NULL
  123. cout << "popped Node of value: " << head->value << endl;
  124. n->prev = NULL;
  125. // delete head
  126. delete head;
  127. // assign head to the copy
  128. head = n;
  129. }
  130. }
  131.  
  132. void insert(int value, int position)
  133. {
  134. int copyInt = position;
  135. if (position <= 1) // we are trying to add to front of list
  136. {
  137. this->PushToFront(value); //we just create a node there with this info.
  138. }
  139.  
  140. else if (position >= GetSize()) // we are trying to add to back of list
  141. {
  142. this->PushToBack(value); //we just create a node there with this info.
  143. }
  144.  
  145. else
  146. {
  147. Node* newNode = new Node(value, NULL, NULL);
  148. Node *current = head;
  149. while (position-- > 1)
  150. current = current->next;
  151.  
  152. newNode->next = current;
  153.  
  154. if (current->prev)
  155. current->prev->next = newNode;
  156. current->prev = newNode;
  157. // prev should point to NULL
  158. cout << "inserted new Node of value: " << newNode->value << " at position " << copyInt << endl;
  159. newNode = NULL;
  160. delete newNode;
  161. }
  162. }
  163.  
  164. void removeAt(int position)
  165. {
  166. int IntCopy = position;
  167. if (position <= 1) // we are trying to pop front of list
  168. {
  169. this->PopFromFront();
  170. }
  171.  
  172. else if (position >= GetSize()) // we are trying to pop back of list
  173. {
  174. this->PopFromBack();
  175. }
  176.  
  177. else
  178. {
  179. Node *current = head;
  180. while (position-- > 1)
  181. current = current->next;
  182.  
  183. //A > B > C > D
  184. // remove C,
  185. // TELL B THAT HIS NEXT IS D
  186. current->prev->next = current->next;
  187. // TELL D THAT HIS PREV IS B
  188. current->prev = current->prev->next;
  189. cout << "removed Node of value: " << current->value << " from position " << IntCopy << endl;
  190. delete current;
  191. }
  192. }
  193.  
  194. void Swap(int pos1, int pos2)
  195. {
  196.  
  197. int IntCopy1 = pos1;
  198. int IntCopy2 = pos2;
  199. if (pos1 == pos2 || (GetSize() == 1) || GetSize() == 0) //don't swap if have 1 or 0 things inside
  200. //also don't swap same index
  201. return;
  202. Node* copyA;
  203. if (pos1 <= 1) {
  204. // we get the first guy
  205. copyA = head;
  206. }
  207. else if (pos1 >= GetSize()) {
  208. // we need to get the pointer of the last guy
  209. Node* current = head;
  210. while (current != NULL) {
  211. current = current->next;
  212. }
  213. //now current confirm pointing to the last guy.
  214. copyA = current;
  215. }
  216. else {
  217. Node *current = head;
  218. while (pos1-- > 1) {
  219. current = current->next;
  220. }
  221. //current is now correct.
  222. copyA = current;
  223. }
  224.  
  225. Node* copyB;
  226. if (pos2 <= 1) {
  227. // we get the first guy
  228. copyB = head;
  229. }
  230. else if (pos2 >= GetSize())
  231. {
  232. // we need to get the pointer of the last guy
  233. Node* current = head;
  234. while (current != NULL)
  235. {
  236. current = current->next;
  237. }
  238. //now current confirm pointing to the last guy.
  239. copyB = current;
  240. }
  241. else
  242. {
  243. Node *current = head;
  244. while (pos2-- > 1)
  245. {
  246. current = current->next;
  247. }
  248. //current is now correct.
  249. copyB = current;
  250. }
  251. Node* current = head;
  252. int temp = copyA->value;
  253. // no point swapping pointers and stuff, so we swap values.
  254. // this maintains the prev and next.
  255. while (current != NULL)
  256. {
  257. if (current == copyA)
  258. {
  259. cout << "Node[" << IntCopy1 << "] with value of " << copyA->value << endl;
  260. current->value = copyB->value;
  261. }
  262. else if (current == copyB)
  263. {
  264. cout << "Node[" << IntCopy2 << "] with value of " << copyB->value << endl;
  265. current->value = temp;
  266. }
  267. current = current->next;
  268. }
  269. //post swap checking
  270. cout << "Node[" << IntCopy1 << "] now has value of " << copyA->value << endl;
  271. cout << "Node[" << IntCopy2 << "] now has value of " << copyB->value << endl;
  272. this->Display();
  273. }
  274.  
  275. void Display(){
  276.  
  277.  
  278. int counter = 0;
  279. Node* current = head;
  280.  
  281. while (current != NULL)
  282. {
  283. cout << current->value << endl;
  284. current = current->next;
  285. counter++;
  286. }
  287. cout << "Total numbers in the linked list : " << counter << endl;
  288. }
  289.  
  290. int GetSize() //we use this to get the size of the list, the proper way.
  291. {
  292. int counter = 0;
  293. Node* current = head;
  294.  
  295. while (current != NULL)
  296. {
  297. current = current->next;
  298. counter++;
  299. }
  300. return counter;
  301. }
  302. };
  303.  
  304. int main()
  305. {
  306. DoublyLinkedList* numberList = new DoublyLinkedList();
  307.  
  308.  
  309. numberList->Display();
  310.  
  311.  
  312. numberList->PushToBack(11);
  313. cout << "=================" << endl;
  314. numberList->Display();
  315. cout << "=================\n" << endl;
  316.  
  317. numberList->PushToBack(22);
  318. cout << "=================" << endl;
  319. numberList->Display();
  320. cout << "=================\n" << endl;
  321.  
  322. numberList->PushToBack(33);
  323. cout << "=================" << endl;
  324. numberList->Display();
  325. cout << "=================\n" << endl;
  326.  
  327. numberList->PushToBack(44);
  328. cout << "=================" << endl;
  329. numberList->Display();
  330. cout << "=================\n" << endl;
  331.  
  332. numberList->removeAt(3);
  333. cout << "=================" << endl;
  334. numberList->Display();
  335. cout << "=================\n" << endl;
  336.  
  337. numberList->PushToFront(13);
  338. cout << "=================" << endl;
  339. numberList->Display();
  340. cout << "=================\n" << endl;
  341.  
  342. numberList->removeAt(3);
  343. cout << "=================" << endl;
  344. numberList->Display();
  345. cout << "=================\n" << endl;
  346.  
  347. numberList->insert(15, 2);
  348. cout << "=================" << endl;
  349. numberList->Display();
  350. cout << "=================\n" << endl;
  351.  
  352. cout << "=================" << endl;
  353. numberList->Display();
  354. cout<<"swap [1] with [3]"<<endl;
  355. numberList->Swap(1, 3);
  356. cout << "================\n"<<endl;
  357.  
  358. cout << "=================" << endl;
  359. numberList->Display();
  360. cout<<"swap [2] with [3]"<<endl;
  361. numberList->Swap(2, 3);
  362. cout << "================\n"<<endl;
  363.  
  364.  
  365. return 0;
  366. system("pause");
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement