Created
March 16, 2019 01:30
-
-
Save DustinAlandzes/68f6ce2c4d9ce1fcd1b111496d269ec8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self,initdata): | |
self.data = initdata | |
self.next = None | |
def getData(self): | |
return self.data | |
def getNext(self): | |
return self.next | |
def setData(self,newdata): | |
self.data = newdata | |
def setNext(self,newnext): | |
self.next = newnext | |
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.getData() == item: | |
found = True | |
else: | |
if current.getData() > item: | |
stop = True | |
else: | |
current = current.getNext() | |
return found | |
def add(self,item): | |
current = self.head | |
previous = None | |
stop = False | |
while current != None and not stop: | |
if current.getData() > item: | |
stop = True | |
else: | |
previous = current | |
current = current.getNext() | |
temp = Node(item) | |
if previous == None: | |
temp.setNext(self.head) | |
self.head = temp | |
else: | |
temp.setNext(current) | |
previous.setNext(temp) | |
def isEmpty(self): | |
return self.head == None | |
def size(self): | |
current = self.head | |
count = 0 | |
while current != None: | |
count = count + 1 | |
current = current.getNext() | |
return count | |
mylist = OrderedList() | |
mylist.add(31) | |
mylist.add(77) | |
mylist.add(17) | |
mylist.add(93) | |
mylist.add(26) | |
mylist.add(54) | |
print(mylist.size()) | |
print(mylist.search(93)) | |
print(mylist.search(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment