A brief tutorial for linked list from the Stanford University.
Linked list at Youtube:
Tutorial: https://www.youtube.com/watch?v=U-MfAoL6qjM
Reverse a linked list
Detect a loop in a linked list
Find the junction node of two lists
Find the mid point of a list by just one scanning
Linked list at Youtube:
Tutorial: https://www.youtube.com/watch?v=U-MfAoL6qjM
Reverse a linked list
Detect a loop in a linked list
Find the junction node of two lists
Find the mid point of a list by just one scanning
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
/****************************************************************************************** ******************************************************************************************* Chapter 2 Linked List: a class and three interview questions By: Hamed Kiani (July 25, 2015) ****************************************************************************************** ******************************************************************************************/ #include "stdafx.h" #include <iostream> #include <cstring> using namespace std; // a struct of linked list node, // contains two elements, _data: the value of node, _next: a pointer to the next node/NULL struct Node{ int _data; Node* _next; }; // LinkedList class, // private: // _head: keeps the head address (first node) // _tail: keeps the tail address (last node) // _size: the number of nodes in a linked list // public: // LinkedList(): constructor // ~LinkedList(): destructor // size(): return the _size of the list // addFront(data): add a node at begining/head of the list with _data = data // addTail(data): add a node at end/tail of the list with _data = data // deleteFront(): delete the first node of the list // deleteTail(): delete the tail node // getHead(): return the address of list's head // getTail(): return the address of list's tail // insertAfter(position, data): instert a node with data value after the position address // deleteThisNode(position): delete the node at the position address // searchInList(data): find data in the list // reverseList(): reverse the linked list // addWithList(listTemp): sum the list with the listTemp // appendTheList(listTemp): append the listTemp to the end of the list // printList(): print the list // printListRecursion(temp): recursion version of print list // remove_deuplicate_fun_1(): remove duplicate values in the list // nth_to_end_node(n): return the address of the n-th to end of the list // class declaration class LinkedList{ private: Node* _head; Node* _tail; int _size; public: LinkedList(); ~LinkedList(); int size(); void addFront(int data); void addTail(int data); void deleteFront(); void deleteTail(); Node* getHead(); Node* getTail(); void insertAfter(Node* position, int data); void deleteThisNode(Node* position); int searchInList(int data); void reverseList(); void addWithList(const LinkedList *listTemp); void appendTheList(const LinkedList *listTemp); void printList(); void printListRecursion(Node* temp); void remove_deuplicate_fun_1(); Node* nth_to_end_node(int n); }; LinkedList::LinkedList() { _head = NULL; _tail = NULL; _size = 0; } LinkedList::~LinkedList() { cout << "**********DESTRUCTOR***********" << endl; cout << "**********DESTRUCTOR***********" << endl; cout << "**********DESTRUCTOR***********" << endl; cout << "the destructor is called! deleting the linked list by calling deleteFront() function!" << endl; while (_size>0) deleteFront(); } int LinkedList::size() { return _size; } void LinkedList::addFront(int data) { Node* nodeTemp = new Node; if (_head == NULL) { nodeTemp->_data = data; _head = nodeTemp; _tail = nodeTemp; nodeTemp->_next = NULL; _size = 1; } else { nodeTemp->_data = data; nodeTemp->_next = _head; _head = nodeTemp; _size++; } cout << "the value: " << data << " is added to the front of list! " << "current size is: " << _size << endl; } void LinkedList::addTail(int data) { Node* nodeTemp = new Node; nodeTemp->_data = data; nodeTemp->_next = NULL; if (_tail != NULL) { _tail->_next = nodeTemp; _tail = nodeTemp; _size++; cout << "the value: " << data << " is added to the tail of list! " << "current size is: " << _size << endl; } else addFront(data); } void LinkedList::printList() { cout << "***********************************" << endl; cout << "The list containts: " << endl; int i = _size; Node* temp = _head; while (i>0) { cout << temp->_data << " " ; temp = temp->_next; i--; } cout << endl; cout << "***********************************" << endl; } void LinkedList::deleteFront() { if (_head == NULL) return; int data = _head->_data; Node* temp; temp = _head; if (_head->_next == NULL) { _head = NULL; _tail = NULL; } else _head = _head->_next; _size--; delete temp; cout << "the value: " << data << " is deleted from the front of list! " << "current size is: " << _size << endl; } void LinkedList::deleteTail() { if (_tail == NULL) return; int data = _tail->_data; Node* temp = _head; while (temp->_next != _tail) temp = temp->_next; _tail = temp; temp = temp->_next; _tail->_next = NULL; delete temp; _size--; cout << "the value: " << data << " is deleted from the tail of list! " << "current size is: " << _size << endl; } Node* LinkedList::getHead() { return _head; } Node* LinkedList::getTail() { return _tail; } void LinkedList::insertAfter(Node* position, int data) { if (position == NULL) { addFront(data); return; } if (position->_next == NULL) { addTail(data); return; } Node* temp = new Node; temp->_data = data; temp->_next = position->_next; position->_next = temp; _size++; } void LinkedList::deleteThisNode(Node* position) { if (position == NULL) return; if (position == _head) { deleteFront(); return; } if (position->_next == NULL) { deleteTail(); return; } Node * temp = _head; while (temp->_next != position) temp = temp->_next; temp->_next = position->_next; delete position; _size--; } int LinkedList::searchInList(int data) { Node* temp = _head; int i = 1; while (temp != NULL) { if (temp->_data == data) return i; temp = temp->_next; i++; } return -1; } void LinkedList::reverseList() { // keep the next node to add to reverse list. Node* next; // keep the head of the reversed list so far. // at the first, the reversed list is empty - null Node* prev = NULL; // a loop to traverse the list while (_head) { next = _head->_next; _head->_next = prev; prev = _head; _head = next; } // at the end, the head of reversed list is pointed by prev _head = prev; // we need to update the tail too next = _head; while (next->_next != NULL) next = next->_next; _tail = next; } void LinkedList::addWithList(const LinkedList *listTemp) { int s = listTemp->_size; if (s != _size) return; Node* t1 = listTemp->_head; Node* t2 = _head; while (t1 != NULL) { t2->_data += t1->_data; t1 = t1->_next; t2 = t2->_next; } } void LinkedList::appendTheList(const LinkedList *listTemp) { Node* temp = listTemp->_head; while (temp != NULL) { Node* newNode = new Node; newNode->_data = temp->_data; newNode->_next = NULL; _tail->_next = newNode; _tail = _tail->_next; temp = temp->_next; _size++; } } void LinkedList::printListRecursion(Node* temp) { if (temp == NULL) return; printListRecursion(temp->_next); cout << temp->_data << endl; } // inplace removing, O(n^2) time and O(1) additional space void LinkedList::remove_deuplicate_fun_1() { Node *head; Node *end; head = _head; int l = 0; if (!head) return; end = head->_next; Node *c; head = head->_next; while (head != NULL) { c = _head; while (c != end){ if (c->_data == head->_data) break; else c = c->_next; } if (c == end) { end->_data = head->_data; end = end->_next; } head = head->_next; } head = _head; while (head->_next != end) { l++; head = head->_next; _tail = head; } _size = ++l; head->_next = NULL; _tail->_next = NULL; } Node* LinkedList::nth_to_end_node(int n) { Node *p, *q; p = _head; q = _head; int i = 0; while (i < n & q != NULL) { q = q->_next; i++; } if (q == NULL) return NULL; while (q->_next != NULL) { q = q->_next; p = p->_next; } return p; } // some useful functions and linked list questions int pow(int a, int b) { if (b == 0) return 1; return a*pow(a, b - 1); } // time: O(N), additional space:O(N) void add_two_lists(LinkedList* l1, LinkedList* l2, LinkedList* l3) { if (!l1 & !l2) return; if (!l1) l3 = l2; if (!l2) l3 = l1; // convert the linked list to a decimal digit int d1 = 0; int d2 = 0; int i = 0; Node* temp1 = l1->getHead(); Node* temp2 = l2->getHead(); while (temp1 != NULL) { d1 += temp1->_data * pow(10, i); temp1 = temp1->_next; i++; } cout << "the digit of first link: " << d1 << endl; i = 0; while (temp2 != NULL) { d2 += temp2->_data * pow(10, i); temp2 = temp2->_next; i++; } cout << "the digit of 2nd link: " << d2 << endl; int add = d1 + d2; while ((add) > 0) { l3->addFront(add % 10); add /= 10; } } // without converting to decimal and re-digiting into the third linked list void add_two_lists_2(LinkedList* l1, LinkedList* l2, LinkedList* l3) { if (!l1 & !l2) return; if (!l1) l3 = l2; if (!l2) l3 = l1; int carry = 0; Node* temp1 = l1->getHead(); Node* temp2 = l2->getHead(); while (temp1 || temp2) { int t1 = (!temp1) ? 0 : temp1->_data; int t2 = (!temp2) ? 0 : temp2->_data; l3->addFront((t1 + t2 + carry) % 10); carry = (t1 + t2 + carry) / 10; if (temp1 != NULL) temp1 = temp1->_next; if (temp2 != NULL) temp2 = temp2->_next; } if (carry > 0) l3->addFront(1); } // find the joint node of two linked list: time O(N), memory: O(N) Node* find_joint_node_of_two_links(Node* h1, Node* h2) { if (!h1 | !h2) return NULL; int l1, l2; l1 = 0; l2 = 0; Node *temp = h1; while (temp != NULL) { l1++; temp = temp->_next; } temp = h2; while (temp != NULL) { l2++; temp = temp->_next; } int diff = (l1 - l2>0) ? (l1 - l2) : (l2 - l1); for (int i = 0; i < diff; i++) { if (l1 > l2) h1 = h1->_next; else h2 = h2->_next; } while (h1 != NULL & h2 != NULL){ if (h1 == h2) return h1; h1 = h1->_next; h2 = h2->_next; } return NULL; } // check if there is a loop in the linked list Node* is_loop_in_linkedlist(Node* head) { Node* slow = head; // moves one node Node* fast = head; // moves two nodes if (!head) return NULL; while (fast != NULL & fast->_next != NULL & slow != NULL) { slow = slow->_next; fast = fast->_next->_next; if (fast == slow) return slow; } return NULL; } // check the video and description in the post for more details Node* find_the_first_node_of_loop(Node* head) { Node* meetPoint = is_loop_in_linkedlist(head); if (meetPoint == NULL) return NULL; Node* temp; temp = head; while (temp != meetPoint) { temp = temp->_next; meetPoint = meetPoint->_next; } return temp; } void main() { // test the basics functions // a linked list object with five nodes LinkedList myList; myList.addFront(5); myList.addFront(4); myList.addFront(3); myList.addFront(2); myList.addFront(1); // print the list myList.printList(); // myList.printListRecursion(myList.getHead()); // add to the tail myList.addTail(1); myList.addTail(3); myList.addTail(3); // print the list myList.printList(); cout << "delete from front" << endl; myList.deleteFront(); myList.deleteTail(); myList.printList(); // print the head and tail values Node* tail = myList.getTail(); Node* head = myList.getHead(); cout << "the value in the first node is: " << head->_data << endl; cout << "the value in the last node is: " << tail->_data << endl; // insert a node in the list cout << "insert a new value of " << 200 << " in the third place: " << endl; Node* position = myList.getHead(); myList.insertAfter(position->_next, 200); myList.printList(); // delete a node from the list cout << "delete the third node: " << endl; position = myList.getHead(); myList.deleteThisNode(position->_next->_next); myList.printList(); // search a value in the linked list int pos = myList.searchInList(10); (pos < 0) ? (cout << "the value 10 is no found" << endl) : (cout << "the value 10 is found at node : " << pos << endl); // reverse the list myList.reverseList(); myList.printList(); // a new linked list object LinkedList myList_2; myList_2.addFront(7); myList_2.addFront(6); myList_2.addFront(5); myList_2.addFront(4); myList_2.addFront(4); myList_2.addFront(4); // adding the myList_2 to myList myList.addWithList(&myList_2); myList.printList(); // appending two lists myList.appendTheList(&myList_2); myList.printList(); // remove duplicates form myList myList.remove_deuplicate_fun_1(); myList.printList(); // return the n-th node to the end int n = 4; position = myList.nth_to_end_node(n); if (position) cout << " the " << n << " to end element is :" << position->_data << endl; else cout << "is longer the length of the list ! " << endl; // Problem1: adding the values of two linked lists LinkedList myList_3; add_two_lists(&myList, &myList_2, &myList_3); // add_two_lists_2(&myList, &myList_2, &myList_3); myList_3.printList(); // Problem 2: find the junction of two lists Node n1, n2, n3, n4, n5, n6; n1._data = 1; n2._data = 2; n3._data = 3; n4._data = 4; n5._data = 5; n6._data = 6; n1._next = &n2; n2._next = &n3; n3._next = &n4; n4._next = &n5; n5._next = NULL; n6._next = &n4; Node* conj = find_joint_node_of_two_links(&n1, &n6); if (!conj) cout << "there is no conjuction " << endl; else cout << "the conjuction node contains the value of : " << conj->_data << endl; // Problem 3: is there a cycle in the linked list n1._next = &n2; n2._next = &n3; n3._next = &n4; n4._next = &n5; n5._next = &n2; conj = is_loop_in_linkedlist(&n1); if (conj) cout << "there is a loop" << endl; else cout << "there is not loop" << endl; // Problem 4: if there is a cycle in the linked list, return the start node of the loop conj = find_the_first_node_of_loop(&n1); if (conj) cout << "the value of the start-loop node is : " << conj->_data << endl; else cout << "there is not loop" << endl; } |