In [1]:
from pythonds9.basic.node import Node
In [3]:
class OrderedList:
    def __init__(self):
        self.head = None
        
    def search(self, item):
        current = self.head
        found = False
        stop = False
        
        while current != None and not found and not stop:
            if current.get_data() == item:
                found = True
            else:
                if current.get_data() > item:
                    stop = True
                else:
                    current = current.get_next()
                    
        return found
    
    
    def add(self, item):
        current = self.head
        previous = None
        stop = False
        
        while current != None and not stop:
            if current.get_data() > item:
                stop = True
            else:
                previous = current
                current = current.get_next()
                
        temp = Node(item)
        if previous == None:
            temp.set_next(self.head)
            self.head = temp
        else:
            temp.set_next(current)
            previous.set_next(temp)
In [4]:
from random import sample
In [5]:
nums = sample(range(100), k=50)
In [7]:
print(nums)
[20, 57, 36, 80, 96, 32, 88, 72, 71, 10, 94, 69, 35, 4, 46, 33, 26, 47, 27, 2, 53, 18, 51, 92, 48, 86, 42, 3, 7, 74, 11, 84, 66, 15, 81, 39, 60, 64, 58, 50, 31, 67, 8, 95, 90, 52, 83, 21, 87, 54]
In [8]:
od = OrderedList()
In [9]:
for n in nums:
    od.add(n)
In [12]:
current = od.head
while current != None:
    print(current.data, end=" ")
    current = current.get_next()
2 3 4 7 8 10 11 15 18 20 21 26 27 31 32 33 35 36 39 42 46 47 48 50 51 52 53 54 57 58 60 64 66 67 69 71 72 74 80 81 83 84 86 87 88 90 92 94 95 96