|
12 | 12 | "output_type": "stream",
|
13 | 13 | "text": [
|
14 | 14 | "Sebastian Raschka \n",
|
15 |
| - "last updated: 2016-05-09 \n", |
| 15 | + "last updated: 2016-08-02 \n", |
16 | 16 | "\n",
|
17 | 17 | "CPython 3.5.1\n",
|
18 |
| - "IPython 4.0.3\n" |
| 18 | + "IPython 5.0.0\n" |
19 | 19 | ]
|
20 | 20 | }
|
21 | 21 | ],
|
|
413 | 413 | "name": "stdout",
|
414 | 414 | "output_type": "stream",
|
415 | 415 | "text": [
|
416 |
| - "<__main__.SLLNode object at 0x1043b4748> [4, 5, 6]\n", |
417 |
| - "<__main__.SLLNode object at 0x1043b4710> [7, 8, 9]\n", |
418 |
| - "<__main__.SLLNode object at 0x1043b44e0> [1, 2, 3]\n" |
| 416 | + "<__main__.SLLNode object at 0x1047c5208> [4, 5, 6]\n", |
| 417 | + "<__main__.SLLNode object at 0x1047c51d0> [7, 8, 9]\n", |
| 418 | + "<__main__.SLLNode object at 0x1047baf98> [1, 2, 3]\n" |
419 | 419 | ]
|
420 | 420 | }
|
421 | 421 | ],
|
|
594 | 594 | "source": [
|
595 | 595 | "So, that's basically \"Single Linked Lists\" in a nutshell!"
|
596 | 596 | ]
|
| 597 | + }, |
| 598 | + { |
| 599 | + "cell_type": "markdown", |
| 600 | + "metadata": {}, |
| 601 | + "source": [ |
| 602 | + "# Ordered Lists" |
| 603 | + ] |
| 604 | + }, |
| 605 | + { |
| 606 | + "cell_type": "markdown", |
| 607 | + "metadata": {}, |
| 608 | + "source": [ |
| 609 | + "The singly-linked-list implementation above can is a so-called unordered data structure or list. The reason is that it collects items in an unsorted manner. On the other hand, ordered lists store items sorted by their values. For example, in our unordered singly-linked example, we may have a list of items in the following order:\n", |
| 610 | + "\n", |
| 611 | + "- [5, 1, 3]\n", |
| 612 | + "\n", |
| 613 | + "and and we can add items at an aribtrary position. For example, we can add a new number 4 as follows\n", |
| 614 | + "\n", |
| 615 | + "- [4, 5, 1, 3]\n", |
| 616 | + "- [5, 4, 1, 3]\n", |
| 617 | + "- [5, 1, 4, 3]\n", |
| 618 | + "- [5, 1, 3, 4]\n", |
| 619 | + "\n", |
| 620 | + "In an ordered list, the sorting order is always maintained. Assuming that we have an ordered list that keeps the items in ascending order, [1, 3, 5], the \"add\" method will add the number 4 only as follows:\n", |
| 621 | + "\n", |
| 622 | + "- [1, 3, 4, 5]" |
| 623 | + ] |
| 624 | + }, |
| 625 | + { |
| 626 | + "cell_type": "code", |
| 627 | + "execution_count": 12, |
| 628 | + "metadata": { |
| 629 | + "collapsed": false |
| 630 | + }, |
| 631 | + "outputs": [], |
| 632 | + "source": [ |
| 633 | + "class SLLNode(object):\n", |
| 634 | + " def __init__(self, data, next_node=None):\n", |
| 635 | + " self.data = data\n", |
| 636 | + " self.next_node = next_node\n", |
| 637 | + "\n", |
| 638 | + " \n", |
| 639 | + "class OrderedList(object):\n", |
| 640 | + " def __init__(self, head=None, datatype=int):\n", |
| 641 | + " self.head = head\n", |
| 642 | + " self.datatype = datatype\n", |
| 643 | + " \n", |
| 644 | + " def __repr__(self):\n", |
| 645 | + " s = ''\n", |
| 646 | + " if self.head is not None:\n", |
| 647 | + " current_node = self.head\n", |
| 648 | + " else:\n", |
| 649 | + " current_node = None\n", |
| 650 | + " \n", |
| 651 | + " while current_node is not None:\n", |
| 652 | + " s += ' --> %s' % current_node.data\n", |
| 653 | + " current_node = current_node.next_node\n", |
| 654 | + " return 'Size: %s\\nData: ' % self.size() + s[5:]\n", |
| 655 | + " \n", |
| 656 | + " def insert(self, data):\n", |
| 657 | + " if not isinstance(data, self.datatype):\n", |
| 658 | + " raise AttributeError('New element must have type %s' % self.datatype)\n", |
| 659 | + " new_node = SLLNode(data=data, next_node=None)\n", |
| 660 | + " \n", |
| 661 | + " inserted = False\n", |
| 662 | + " \n", |
| 663 | + " if self.head is None:\n", |
| 664 | + " print('new node')\n", |
| 665 | + " self.head = new_node\n", |
| 666 | + " inserted = True\n", |
| 667 | + "\n", |
| 668 | + " current = self.head\n", |
| 669 | + " previous = None\n", |
| 670 | + " while current is not None and not inserted:\n", |
| 671 | + " if current.data >= new_node.data:\n", |
| 672 | + " new_node.next_node = current\n", |
| 673 | + " if previous is not None:\n", |
| 674 | + " previous.next_node = new_node\n", |
| 675 | + " else:\n", |
| 676 | + " self.head = new_node\n", |
| 677 | + " inserted = True\n", |
| 678 | + " previous = current\n", |
| 679 | + " current = current.next_node\n", |
| 680 | + " \n", |
| 681 | + " if not inserted:\n", |
| 682 | + " previous.next_node = new_node\n", |
| 683 | + " new_node.next_node = current\n", |
| 684 | + " \n", |
| 685 | + " def size(self):\n", |
| 686 | + " current_node = self.head\n", |
| 687 | + " cnt = 0\n", |
| 688 | + " while current_node:\n", |
| 689 | + " cnt += 1\n", |
| 690 | + " current_node = current_node.next_node\n", |
| 691 | + " return cnt\n", |
| 692 | + " \n", |
| 693 | + " def search(self, data):\n", |
| 694 | + " current_node = self.head\n", |
| 695 | + " target_node = None\n", |
| 696 | + " while current_node is not None and target_node is None:\n", |
| 697 | + " if current_node.data == data:\n", |
| 698 | + " target_node = current_node\n", |
| 699 | + " else:\n", |
| 700 | + " current_node = current_node.next_node\n", |
| 701 | + " return target_node \n", |
| 702 | + " \n", |
| 703 | + " def delete(self, data):\n", |
| 704 | + " current_node = self.head\n", |
| 705 | + " previous_node = None\n", |
| 706 | + " found = False\n", |
| 707 | + "\n", |
| 708 | + " while current_node is not None and not found:\n", |
| 709 | + " if current_node.data == data:\n", |
| 710 | + " found = True\n", |
| 711 | + " if previous_node is None:\n", |
| 712 | + " self.head = current_node.next_node\n", |
| 713 | + " else:\n", |
| 714 | + " previous_node.next_node = current_node.next_node\n", |
| 715 | + " \n", |
| 716 | + " else:\n", |
| 717 | + " previous_node = current_node\n", |
| 718 | + " current_node = current_node.next_node" |
| 719 | + ] |
| 720 | + }, |
| 721 | + { |
| 722 | + "cell_type": "code", |
| 723 | + "execution_count": 13, |
| 724 | + "metadata": { |
| 725 | + "collapsed": false |
| 726 | + }, |
| 727 | + "outputs": [ |
| 728 | + { |
| 729 | + "name": "stdout", |
| 730 | + "output_type": "stream", |
| 731 | + "text": [ |
| 732 | + "Size: 0\n", |
| 733 | + "Data: \n", |
| 734 | + "\n", |
| 735 | + "new node\n", |
| 736 | + "Size: 1\n", |
| 737 | + "Data: 1\n", |
| 738 | + "None\n" |
| 739 | + ] |
| 740 | + } |
| 741 | + ], |
| 742 | + "source": [ |
| 743 | + "sll = OrderedList(datatype=int)\n", |
| 744 | + "print(sll)\n", |
| 745 | + "\n", |
| 746 | + "print()\n", |
| 747 | + "\n", |
| 748 | + "sll.insert(data=1)\n", |
| 749 | + "print(sll)\n", |
| 750 | + "print(sll.head.next_node)" |
| 751 | + ] |
| 752 | + }, |
| 753 | + { |
| 754 | + "cell_type": "code", |
| 755 | + "execution_count": 14, |
| 756 | + "metadata": { |
| 757 | + "collapsed": false |
| 758 | + }, |
| 759 | + "outputs": [ |
| 760 | + { |
| 761 | + "name": "stdout", |
| 762 | + "output_type": "stream", |
| 763 | + "text": [ |
| 764 | + "Size: 2\n", |
| 765 | + "Data: 1 --> 2\n" |
| 766 | + ] |
| 767 | + } |
| 768 | + ], |
| 769 | + "source": [ |
| 770 | + "sll.insert(data=2)\n", |
| 771 | + "print(sll)" |
| 772 | + ] |
| 773 | + }, |
| 774 | + { |
| 775 | + "cell_type": "code", |
| 776 | + "execution_count": 15, |
| 777 | + "metadata": { |
| 778 | + "collapsed": false |
| 779 | + }, |
| 780 | + "outputs": [ |
| 781 | + { |
| 782 | + "name": "stdout", |
| 783 | + "output_type": "stream", |
| 784 | + "text": [ |
| 785 | + "Size: 3\n", |
| 786 | + "Data: 1 --> 2 --> 2\n" |
| 787 | + ] |
| 788 | + } |
| 789 | + ], |
| 790 | + "source": [ |
| 791 | + "sll.insert(data=2)\n", |
| 792 | + "print(sll)" |
| 793 | + ] |
| 794 | + }, |
| 795 | + { |
| 796 | + "cell_type": "code", |
| 797 | + "execution_count": 16, |
| 798 | + "metadata": { |
| 799 | + "collapsed": false |
| 800 | + }, |
| 801 | + "outputs": [ |
| 802 | + { |
| 803 | + "name": "stdout", |
| 804 | + "output_type": "stream", |
| 805 | + "text": [ |
| 806 | + "Size: 4\n", |
| 807 | + "Data: 1 --> 2 --> 2 --> 3\n" |
| 808 | + ] |
| 809 | + } |
| 810 | + ], |
| 811 | + "source": [ |
| 812 | + "sll.insert(data=3)\n", |
| 813 | + "print(sll)" |
| 814 | + ] |
| 815 | + }, |
| 816 | + { |
| 817 | + "cell_type": "code", |
| 818 | + "execution_count": 17, |
| 819 | + "metadata": { |
| 820 | + "collapsed": false |
| 821 | + }, |
| 822 | + "outputs": [ |
| 823 | + { |
| 824 | + "name": "stdout", |
| 825 | + "output_type": "stream", |
| 826 | + "text": [ |
| 827 | + "Size: 5\n", |
| 828 | + "Data: 1 --> 2 --> 2 --> 3 --> 10\n" |
| 829 | + ] |
| 830 | + } |
| 831 | + ], |
| 832 | + "source": [ |
| 833 | + "sll.insert(data=10)\n", |
| 834 | + "print(sll)" |
| 835 | + ] |
| 836 | + }, |
| 837 | + { |
| 838 | + "cell_type": "code", |
| 839 | + "execution_count": 18, |
| 840 | + "metadata": { |
| 841 | + "collapsed": false |
| 842 | + }, |
| 843 | + "outputs": [ |
| 844 | + { |
| 845 | + "name": "stdout", |
| 846 | + "output_type": "stream", |
| 847 | + "text": [ |
| 848 | + "Size: 6\n", |
| 849 | + "Data: 1 --> 2 --> 2 --> 3 --> 10 --> 10\n" |
| 850 | + ] |
| 851 | + } |
| 852 | + ], |
| 853 | + "source": [ |
| 854 | + "sll.insert(data=10)\n", |
| 855 | + "print(sll)" |
| 856 | + ] |
| 857 | + }, |
| 858 | + { |
| 859 | + "cell_type": "code", |
| 860 | + "execution_count": 19, |
| 861 | + "metadata": { |
| 862 | + "collapsed": false |
| 863 | + }, |
| 864 | + "outputs": [ |
| 865 | + { |
| 866 | + "name": "stdout", |
| 867 | + "output_type": "stream", |
| 868 | + "text": [ |
| 869 | + "Size: 7\n", |
| 870 | + "Data: 1 --> 2 --> 2 --> 2 --> 3 --> 10 --> 10\n" |
| 871 | + ] |
| 872 | + } |
| 873 | + ], |
| 874 | + "source": [ |
| 875 | + "sll.insert(data=2)\n", |
| 876 | + "print(sll)" |
| 877 | + ] |
| 878 | + }, |
| 879 | + { |
| 880 | + "cell_type": "code", |
| 881 | + "execution_count": 20, |
| 882 | + "metadata": { |
| 883 | + "collapsed": false |
| 884 | + }, |
| 885 | + "outputs": [ |
| 886 | + { |
| 887 | + "name": "stdout", |
| 888 | + "output_type": "stream", |
| 889 | + "text": [ |
| 890 | + "Size: 8\n", |
| 891 | + "Data: 0 --> 1 --> 2 --> 2 --> 2 --> 3 --> 10 --> 10\n" |
| 892 | + ] |
| 893 | + } |
| 894 | + ], |
| 895 | + "source": [ |
| 896 | + "sll.insert(data=0)\n", |
| 897 | + "print(sll)" |
| 898 | + ] |
| 899 | + }, |
| 900 | + { |
| 901 | + "cell_type": "code", |
| 902 | + "execution_count": 21, |
| 903 | + "metadata": { |
| 904 | + "collapsed": false |
| 905 | + }, |
| 906 | + "outputs": [ |
| 907 | + { |
| 908 | + "name": "stdout", |
| 909 | + "output_type": "stream", |
| 910 | + "text": [ |
| 911 | + "Size: 7\n", |
| 912 | + "Data: 0 --> 2 --> 2 --> 2 --> 3 --> 10 --> 10\n" |
| 913 | + ] |
| 914 | + } |
| 915 | + ], |
| 916 | + "source": [ |
| 917 | + "sll.delete(data=1)\n", |
| 918 | + "print(sll)" |
| 919 | + ] |
| 920 | + }, |
| 921 | + { |
| 922 | + "cell_type": "code", |
| 923 | + "execution_count": 22, |
| 924 | + "metadata": { |
| 925 | + "collapsed": false |
| 926 | + }, |
| 927 | + "outputs": [ |
| 928 | + { |
| 929 | + "data": { |
| 930 | + "text/plain": [ |
| 931 | + "3" |
| 932 | + ] |
| 933 | + }, |
| 934 | + "execution_count": 22, |
| 935 | + "metadata": {}, |
| 936 | + "output_type": "execute_result" |
| 937 | + } |
| 938 | + ], |
| 939 | + "source": [ |
| 940 | + "sll.search(data=3).data" |
| 941 | + ] |
597 | 942 | }
|
598 | 943 | ],
|
599 | 944 | "metadata": {
|
|
0 commit comments