|
| 1 | +class Node: |
| 2 | + """ |
| 3 | + An object for storing a single node of a linked list |
| 4 | + model two attributes - data and the link to the next node in the list |
| 5 | + """ |
| 6 | + |
| 7 | + data = None |
| 8 | + next_node = None |
| 9 | + |
| 10 | + def __init__(self, data): |
| 11 | + self.data = data |
| 12 | + |
| 13 | + def __repr__(self): |
| 14 | + return "<Node data: %s>" % self.data |
| 15 | + |
| 16 | + |
| 17 | +class LinkedList: |
| 18 | + """ |
| 19 | + singly linked list |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + self.head = None |
| 24 | + |
| 25 | + def is_empty(self): |
| 26 | + return self.head == None |
| 27 | + |
| 28 | + def size(self): |
| 29 | + """ |
| 30 | + returns the number of nodes in the list |
| 31 | + takes 0(n) time |
| 32 | + """ |
| 33 | + current = self.head |
| 34 | + count = 0 |
| 35 | + |
| 36 | + while current: # or while current!= None: |
| 37 | + count += 1 |
| 38 | + current = current.next_node |
| 39 | + return count |
| 40 | + |
| 41 | + def add(self, data): |
| 42 | + """ |
| 43 | + adds new node containing data at head of the list |
| 44 | + takes 0(1) time |
| 45 | + """ |
| 46 | + new_node = Node(data) |
| 47 | + new_node.next_node = self.head |
| 48 | + self.head = new_node |
| 49 | + |
| 50 | + def search(self, key): |
| 51 | + """ |
| 52 | + search for the first node containing data that matches the key |
| 53 | + returns the node or "None" if not found |
| 54 | + takes 0(n) time |
| 55 | + """ |
| 56 | + current = self.head |
| 57 | + while current: |
| 58 | + if current.data == key: |
| 59 | + return current |
| 60 | + else: |
| 61 | + current = current.next_node |
| 62 | + return None |
| 63 | + |
| 64 | + def insert(self, data, index): |
| 65 | + """ |
| 66 | + insert a new node containing data at index position |
| 67 | + insertion takes 0(1) time but finding the node at the |
| 68 | + insertion point takes 0(n) time |
| 69 | + takes overall 0(n) time |
| 70 | + """ |
| 71 | + if index == 0: |
| 72 | + self.add(data) |
| 73 | + if index > 0: |
| 74 | + new = Node(data) |
| 75 | + |
| 76 | + position = index |
| 77 | + current = self.head |
| 78 | + |
| 79 | + while position > 1: |
| 80 | + current = node.next_node |
| 81 | + position -= 1 |
| 82 | + |
| 83 | + prev_node = current |
| 84 | + next_node = current.next_node |
| 85 | + |
| 86 | + prev_node.next_node = new |
| 87 | + new.next_node = next_node |
| 88 | + |
| 89 | + def remove(self, key): |
| 90 | + """ |
| 91 | + removes nodes containing data that matches the key |
| 92 | + returns the node or "none" if the key doesn't exist |
| 93 | + takes 0(n) time |
| 94 | + """ |
| 95 | + current = self.head |
| 96 | + previous = None |
| 97 | + found = False |
| 98 | + |
| 99 | + while current and not found: |
| 100 | + if current.data == key and current == self.head: # or current is self.head |
| 101 | + found = True |
| 102 | + self.head = current.next_node |
| 103 | + elif current.data == key: |
| 104 | + found = True |
| 105 | + previous.next_node = current.next_node |
| 106 | + else: |
| 107 | + previous = current |
| 108 | + current = current.next_node |
| 109 | + |
| 110 | + return current |
| 111 | + |
| 112 | + def node_at_index(self, index): |
| 113 | + if index == 0: |
| 114 | + return self.head |
| 115 | + else: |
| 116 | + current = self.head |
| 117 | + position = 0 |
| 118 | + |
| 119 | + while position < index: |
| 120 | + current = current.next_node |
| 121 | + position += 1 |
| 122 | + return current |
| 123 | + |
| 124 | + def __repr__(self): |
| 125 | + """ |
| 126 | + returns a string representation of the list |
| 127 | + takes 0(n) time |
| 128 | + """ |
| 129 | + nodes = [] |
| 130 | + current = self.head |
| 131 | + |
| 132 | + while current: |
| 133 | + if current is self.head: |
| 134 | + nodes.append("[head: %s]" % current.data) |
| 135 | + elif current.next_node is None: |
| 136 | + nodes.append("[Tail: %s]" % current.data) |
| 137 | + else: |
| 138 | + nodes.append("[%s]" % current.data) |
| 139 | + |
| 140 | + current = current.next_node |
| 141 | + return "->".join(nodes) |
0 commit comments