|
| 1 | +# Data structures are basically just that - they are structures which can hold some |
| 2 | +# data together. In other words, they are used to store a collection of related data. |
| 3 | + |
| 4 | +# array |
| 5 | +import array |
| 6 | + |
| 7 | +one_dimensional = array.array('i', [3, 6, 9, 12, 15]) |
| 8 | +for i in range(0, len(one_dimensional)): |
| 9 | + print(one_dimensional[i]) |
| 10 | +# 3 |
| 11 | +# 6 |
| 12 | +# 9 |
| 13 | +# 12 |
| 14 | +# 15 |
| 15 | + |
| 16 | +# Accessing every other element of the array |
| 17 | +one_dimensional = array.array('i', [3, 6, 9, 12, 15]) |
| 18 | +for i in range(0, len(one_dimensional), 2): |
| 19 | + print(one_dimensional[i]) |
| 20 | +# 3 |
| 21 | +# 9 |
| 22 | +# 15 |
| 23 | + |
| 24 | +# Accessing an element directly |
| 25 | +one_dimensional = array.array('i', [3, 6, 9, 12, 15]) |
| 26 | + |
| 27 | +print(one_dimensional[4]) |
| 28 | +# 15 |
| 29 | + |
| 30 | +# Lists are more user friendly than true arrays |
| 31 | +empty_list = [] |
| 32 | + |
| 33 | +list_of_ints = [3, 6, 9] |
| 34 | + |
| 35 | +mixed_list = [2, 'Boo', 3.14] |
| 36 | + |
| 37 | +two_dimensional_list = [[3, 6, 9], [2, 'Boo', 3.14]] |
| 38 | + |
| 39 | + |
| 40 | +# Singly linked list data structure |
| 41 | +class Node(object): |
| 42 | + def __init__(self, val): |
| 43 | + self.val = val |
| 44 | + self.next = None |
| 45 | + |
| 46 | + def get_data(self): |
| 47 | + return self.val |
| 48 | + |
| 49 | + def set_data(self, val): |
| 50 | + self.val = val |
| 51 | + |
| 52 | + def get_next(self): |
| 53 | + return self.next |
| 54 | + |
| 55 | + def set_next(self, next): |
| 56 | + self.next = next |
| 57 | + |
| 58 | + |
| 59 | +class LinkedList(object): |
| 60 | + def __init__(self, head=None): |
| 61 | + self.head = head |
| 62 | + self.count = 0 |
| 63 | + |
| 64 | + def get_count(self): |
| 65 | + return self.count |
| 66 | + |
| 67 | + def insert(self, data): |
| 68 | + new_node = Node(data) |
| 69 | + new_node.set_next(self.head) |
| 70 | + self.head = new_node |
| 71 | + self.count += 1 |
| 72 | + |
| 73 | + def find(self, val): |
| 74 | + item = self.head |
| 75 | + while (item != None): |
| 76 | + if item.get_data() == val: |
| 77 | + return item |
| 78 | + else: |
| 79 | + item = item.get_next() |
| 80 | + return None |
| 81 | + |
| 82 | + def delete(self, index): |
| 83 | + if index > self.count: |
| 84 | + return |
| 85 | + if self.head == None: |
| 86 | + return |
| 87 | + else: |
| 88 | + tempIndex = 0 |
| 89 | + node = self.head |
| 90 | + while tempIndex < index - 1: |
| 91 | + node = node.get_next() |
| 92 | + tempIndex += 1 |
| 93 | + node.set_next(node.get_next().get_next()) |
| 94 | + self.count -= 1 |
| 95 | + |
| 96 | + def print_list(self): |
| 97 | + tempnode = self.head |
| 98 | + while (tempnode != None): |
| 99 | + print('Node: ', tempnode.get_data()) |
| 100 | + tempnode = tempnode.get_next() |
| 101 | + |
| 102 | + |
| 103 | +linkedlist = LinkedList() |
| 104 | +linkedlist.insert(3) |
| 105 | +linkedlist.insert(6) |
| 106 | +linkedlist.insert(9) |
| 107 | +linkedlist.insert(12) |
| 108 | +linkedlist.insert(15) |
| 109 | +linkedlist.print_list() |
| 110 | +# Node: 15 |
| 111 | +# Node: 12 |
| 112 | +# Node: 9 |
| 113 | +# Node: 6 |
| 114 | +# Node: 3 |
| 115 | + |
| 116 | +print('Number of items in List: ', linkedlist.get_count()) |
| 117 | +# 5 |
| 118 | + |
| 119 | +print('Finding item: ', linkedlist.find(6)) |
| 120 | +# Finding item: <__main__.Node object at 0x03512FD0> |
| 121 | +print('Finding item: ', linkedlist.find(9)) |
| 122 | +# Finding item: <__main__.Node object at 0x03538028> |
| 123 | + |
| 124 | +linkedlist.delete(3) |
| 125 | +print('Number of items in List: ', linkedlist.get_count()) |
| 126 | +print('Finding item: ', linkedlist.find(12)) |
| 127 | +linkedlist.print_list() |
| 128 | +# Number of items in List: 4 |
| 129 | +# Finding item: <__main__.Node object at 0x031A8058> |
| 130 | +# Node: 15 |
| 131 | +# Node: 12 |
| 132 | +# Node: 9 |
| 133 | +# Node: 3 |
| 134 | + |
| 135 | +# Stack Data Structure |
| 136 | +stack = [] |
| 137 | + |
| 138 | +stack.append('Tom') |
| 139 | +stack.append('Dick') |
| 140 | +stack.append('Harry') |
| 141 | +stack.append('Bosch') |
| 142 | +print(stack) |
| 143 | +# ['Tom', 'Dick', 'Harry', 'Bosch'] |
| 144 | + |
| 145 | +popped = stack.pop() |
| 146 | +print(popped) |
| 147 | +print(stack) |
| 148 | +# Bosch |
| 149 | +# ['Tom', 'Dick', 'Harry'] |
| 150 | + |
| 151 | +# Stack as a Class |
| 152 | +class Stack: |
| 153 | + def __init__(self): |
| 154 | + self.stack = [] |
| 155 | + |
| 156 | + def __bool__(self): |
| 157 | + return bool(self.stack) |
| 158 | + |
| 159 | + def __str__(self): |
| 160 | + return str(self.stack) |
| 161 | + |
| 162 | + def push(self, data): |
| 163 | + self.stack.append(data) |
| 164 | + |
| 165 | + def pop(self): |
| 166 | + if self.stack: |
| 167 | + return self.stack.pop() |
| 168 | + else: |
| 169 | + raise IndexError('Stack is empty') |
| 170 | + |
| 171 | + def size(self): |
| 172 | + return len(self.stack) |
| 173 | + |
| 174 | + |
| 175 | +stack = Stack() |
| 176 | +for i in range(5): |
| 177 | + stack.push(i) |
| 178 | + |
| 179 | +print('Initial stack: ' + str(stack)) |
| 180 | +print('pop(): ' + str(stack.pop())) |
| 181 | +print('After pop(), the stack is now: ' + str(stack)) |
| 182 | +stack.push(7) |
| 183 | +print('After push(7), the stack is now: ' + str(stack)) |
| 184 | +print('The size is: ' + str(stack.size())) |
| 185 | +# Initial stack: [0, 1, 2, 3, 4] |
| 186 | +# pop(): 4 |
| 187 | +# After pop(), the stack is now: [0, 1, 2, 3] |
| 188 | +# After push(7), the stack is now: [0, 1, 2, 3, 7] |
| 189 | +# The size is: 5 |
| 190 | + |
| 191 | +# Queue data structure |
| 192 | +from collections import deque |
| 193 | + |
| 194 | +queue = deque() |
| 195 | +queue.append('Monday') |
| 196 | +queue.append('Tuesday') |
| 197 | +queue.append('Wednesday') |
| 198 | +queue.append('Thursday') |
| 199 | +queue.append('Friday') |
| 200 | +print(queue) |
| 201 | +# deque(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']) |
| 202 | + |
| 203 | +popped = queue.popleft() |
| 204 | +print(popped) |
| 205 | +print(queue) |
| 206 | +# Monday |
| 207 | +# deque(['Tuesday', 'Wednesday', 'Thursday', 'Friday']) |
| 208 | + |
| 209 | +# Hash Table Data Structure |
| 210 | +hashone = dict({'firstkey': 1, 'secondkey': 2, 'thirdkey': 'three'}) |
| 211 | +print(hashone) |
| 212 | +# {'firstkey': 1, 'secondkey': 2, 'thirdkey': 'three'} |
| 213 | + |
| 214 | +hashtwo = {} |
| 215 | +hashtwo['firstkey'] = 1 |
| 216 | +hashtwo['secondkey'] = 2 |
| 217 | +hashtwo['thirdkey'] = 3 |
| 218 | +print(hashtwo) |
| 219 | +# {'firstkey': 1, 'secondkey': 2, 'thirdkey': 3} |
| 220 | + |
| 221 | +hashtwo['secondkey'] = 'two' |
| 222 | +print(hashtwo) |
| 223 | +# {'firstkey': 1, 'secondkey': 'two', 'thirdkey': 3} |
| 224 | + |
| 225 | +for key, value in hashtwo.items(): |
| 226 | + print('key: ', key, ' value: ', value) |
| 227 | +# key: firstkey value: 1 |
| 228 | +# key: secondkey value: two |
| 229 | +# key: thirdkey value: 3 |
0 commit comments