|
| 1 | +class Node: |
| 2 | + def __init__(self, initdata): |
| 3 | + self.data = initdata |
| 4 | + self.next = None |
| 5 | + |
| 6 | + def getData(self): |
| 7 | + return self.data |
| 8 | + |
| 9 | + def getNext(self): |
| 10 | + return self.next |
| 11 | + |
| 12 | + def setData(self, newdata): |
| 13 | + self.data = newdata |
| 14 | + |
| 15 | + def setNext(self, newnext): |
| 16 | + self.next = newnext |
| 17 | + |
| 18 | + |
| 19 | +class UnorderedList: |
| 20 | + def __init__(self): |
| 21 | + self.head = None |
| 22 | + |
| 23 | + def isEmpty(self): |
| 24 | + return self.head == None |
| 25 | + |
| 26 | + def add(self, item): |
| 27 | + temp = Node(item) |
| 28 | + temp.setNext(self.head) |
| 29 | + self.head = temp |
| 30 | + |
| 31 | + def size(self): |
| 32 | + current = self.head |
| 33 | + count = 0 |
| 34 | + while current != None: |
| 35 | + count = count + 1 |
| 36 | + current = current.getNext() |
| 37 | + return count |
| 38 | + |
| 39 | + def search(self, item): |
| 40 | + current = self.head |
| 41 | + found = False |
| 42 | + while current != None and not found: |
| 43 | + if current.getData() == item: |
| 44 | + found = True |
| 45 | + else: |
| 46 | + current = current.getNext() |
| 47 | + return found |
| 48 | + |
| 49 | + def remove(self, item): |
| 50 | + current = self.head |
| 51 | + previous = None |
| 52 | + found = False |
| 53 | + while not found: |
| 54 | + if current.getData() == item: |
| 55 | + found = True |
| 56 | + else: |
| 57 | + previous = current |
| 58 | + current = current.getNext() |
| 59 | + |
| 60 | + if previous == None: |
| 61 | + self.head = current.getNext() |
| 62 | + else: |
| 63 | + previous.setNext(current.getNext()) |
| 64 | + |
| 65 | + def append(self, item): |
| 66 | + current = self.head |
| 67 | + if current: |
| 68 | + while current.getNext() != None: |
| 69 | + current = current.getNext() |
| 70 | + current.setNext(Node(item)) |
| 71 | + else: |
| 72 | + self.head = Node(item) |
| 73 | + |
| 74 | +class OrderedList: |
| 75 | + def __init__(self): |
| 76 | + self.head = None |
| 77 | + |
| 78 | + def isEmpty(self): |
| 79 | + return self.head == None |
| 80 | + |
| 81 | + def size(self): |
| 82 | + current = self.head |
| 83 | + count = 0 |
| 84 | + while current != None: |
| 85 | + count += 1 |
| 86 | + current = current.getNext() |
| 87 | + return count |
| 88 | + |
| 89 | + def search(self, item): |
| 90 | + current = self.head |
| 91 | + found = False |
| 92 | + stop = False |
| 93 | + while current != None and not found and not stop: |
| 94 | + if current.getData() == item: |
| 95 | + found = True |
| 96 | + else: |
| 97 | + if current.getData() > item: |
| 98 | + stop = True |
| 99 | + else: |
| 100 | + current = current.getNext() |
| 101 | + return found |
| 102 | + |
| 103 | + def add(self, item): |
| 104 | + current = self.head |
| 105 | + previous = None |
| 106 | + stop = False |
| 107 | + while current != None and not stop: |
| 108 | + if current.getData() > item: |
| 109 | + stop = True |
| 110 | + else: |
| 111 | + previous = current |
| 112 | + current = current.getNext() |
| 113 | + |
| 114 | + temp = Node(item) |
| 115 | + if previous == None: |
| 116 | + temp.setNext(self.head) |
| 117 | + self.head = temp |
| 118 | + else: |
| 119 | + temp.setNext(current) |
| 120 | + previous.setNext(temp) |
| 121 | + |
| 122 | +mylist = OrderedList() |
| 123 | +mylist.add(31) |
| 124 | +mylist.add(77) |
| 125 | +mylist.add(17) |
| 126 | +mylist.add(93) |
| 127 | +mylist.add(26) |
| 128 | +mylist.add(54) |
| 129 | + |
| 130 | +print(mylist.size()) |
| 131 | +print(mylist.search(93)) |
| 132 | +print(mylist.search(100)) |
0 commit comments