diff --git a/book/book_second_edition.pdf b/book/book_second_edition.pdf index c8dee89..4495060 100644 Binary files a/book/book_second_edition.pdf and b/book/book_second_edition.pdf differ diff --git a/src/abstract_structures/__init__.py b/src/abstract_structures/__init__.py new file mode 100644 index 0000000..be4c726 --- /dev/null +++ b/src/abstract_structures/__init__.py @@ -0,0 +1 @@ +__all__=["hash_tables", "heap", "linked_list", "queues", "stacks"] \ No newline at end of file diff --git a/src/abstract_structures/hash_table.py b/src/abstract_structures/hash_table.py deleted file mode 100644 index e367ea7..0000000 --- a/src/abstract_structures/hash_table.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -''' The first example design a Hash table using chaining to avoid collisions. The second - uses two lists.''' - -from linked_list_fifo import Node, LinkList - -class HashTableLL(object): - def __init__(self, size): - self.size = size - self.slots = [] - self._createHashTable() - - def _createHashTable(self): - for i in range(self.size) : - self.slots.append(LinkList()) - - def findIndex(self, item): - return item%self.size - - def addItem(self, item): - index = self.findIndex(item) - self.slots[index].addNode(item) - return self - - - def delItem(self, item): - index = self.findIndex(item) - self.slots[index].deleteNode(item) - return self - - - def printHashTable(self): - for i in range(self.size): - print('\nSlot {}:'.format(i)) - print(self.slots[i].printList()) - - - - - -def test_hash_tables(): - H1 = HashTableLL(3) - for i in range (0, 20): - H1.addItem(i) - H1.printHashTable() - print('\n\nNow deleting:') - H1.delItem(0) - H1.delItem(0) - H1.delItem(0) - H1.delItem(0) - H1.printHashTable() -''' - H2 = HashTable2L(3) - H2[54]="buffy" - H2[26]="xander" - H2[17]="giles" - print(H.2slots) -''' - - -if __name__ == '__main__': - test_hash_tables() diff --git a/src/abstract_structures/hash_tables/__init__.py b/src/abstract_structures/hash_tables/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/abstract_structures/hash_tables/hash_table.py b/src/abstract_structures/hash_tables/hash_table.py new file mode 100644 index 0000000..61c41a4 --- /dev/null +++ b/src/abstract_structures/hash_tables/hash_table.py @@ -0,0 +1,61 @@ +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + +''' design a Hash table using chaining to avoid collisions.''' + + +#import abstract_structures.linked_list.linked_list_fifo +#import abstract_structures.linked_list.node + +from linked_list_fifo import LinkedListFIFO + + + +class HashTableLL(object): + def __init__(self, size): + self.size = size + self.slots = [] + self._createHashTable() + + def _createHashTable(self): + for i in range(self.size) : + self.slots.append(LinkedListFIFO()) + + def _find(self, item): + return item%self.size + + def _add(self, item): + index = self._find(item) + self.slots[index].addNode(item) + + def _delete(self, item): + index = self._find(item) + self.slots[index].deleteNode(item) + + def _print(self): + for i in range(self.size): + print('\nSlot {}:'.format(i)) + print(self.slots[i]._printList()) + + + + +def test_hash_tables(): + H1 = HashTableLL(3) + for i in range (0, 20): + H1._add(i) + H1._print() + print('\n\nNow deleting:') + H1._delete(0) + H1._delete(1) + H1._delete(2) + H1._delete(0) + + H1._print() + + + +if __name__ == '__main__': + test_hash_tables() diff --git a/src/abstract_structures/linked_list/linked_list_fifo.py b/src/abstract_structures/linked_list/linked_list_fifo.py index 8a75751..1492795 100644 --- a/src/abstract_structures/linked_list/linked_list_fifo.py +++ b/src/abstract_structures/linked_list/linked_list_fifo.py @@ -50,12 +50,6 @@ class LinkedListFIFO(object): self.tail.pointer = node self.tail = node - # delete a node in some position - def _delete(self, prev, node): - self.length -= 1 - prev.pointer = node.pointer - if not node.pointer: - self.tail = prev # add nodes in general def addNode(self, value): @@ -81,8 +75,14 @@ class LinkedListFIFO(object): self._deleteFirst() else: node, prev, i = self._find(index) - if i == index: - self._delete(prev, node) + if i == index and node: + self.length -= 1 + if i == 0 or not prev : + self.head = node.pointer + else: + prev.pointer = node.pointer + if not self.tail == node: + self.tail = prev else: print('Node with index {} not found'.format(index)) diff --git a/src/abstract_structures/linked_list/linked_list_from_stack.py b/src/abstract_structures/linked_list/linked_list_from_stack.py deleted file mode 100644 index 52efaf6..0000000 --- a/src/abstract_structures/linked_list/linked_list_from_stack.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - - - -class Node(object): - def __init__(self,data=None,next=None): - self.data = data - self.next = next - - def setnext(self,next): - self.next = next - - def __str__(self): - return "%s" % self.data - - -class LinkedListStack(object): - ''' linked list from stack ''' - def __init__(self, max=0): - self.max = max - self.head = None - self.z = None - self.size = 0 - - def push(self, data): - if self.size == 0: - self.head = Node(data=data) - self.size += 1 - else: - head = self.head - temp = Node(data = data) - self.head = temp - self.head = temp - temp.setnext(head) - - def pop(self): - temp = self.head.next - self.head = temp - - def isEmpty(self): - return self.size == 0 - - def __str__(self): - d = "" - if self.isEmpty(): return "" - else: - temp = self.head - d += "%s\n" % temp - while temp.next != None: - temp = temp.next - d += "%s\n" % temp - return d - - - - -def test_ll_from_stack(): - ll = LinkedListStack(max = 20) - ll.push("1") - ll.push("2") - ll.push("3") - ll.push("4") - print(ll) - ll.pop() - print(ll) - - -if __name__ == '__main__': - test_ll_from_stack() diff --git a/src/abstract_structures/queues_stacks/queue_2_stacks.py b/src/abstract_structures/queues/queue_2_stacks.py similarity index 100% rename from src/abstract_structures/queues_stacks/queue_2_stacks.py rename to src/abstract_structures/queues/queue_2_stacks.py diff --git a/src/abstract_structures/queues_stacks/animal_shelter.py b/src/abstract_structures/stacks/queues_stacks/animal_shelter.py similarity index 100% rename from src/abstract_structures/queues_stacks/animal_shelter.py rename to src/abstract_structures/stacks/queues_stacks/animal_shelter.py diff --git a/src/abstract_structures/queues_stacks/setofStacks.py b/src/abstract_structures/stacks/queues_stacks/setofStacks.py similarity index 100% rename from src/abstract_structures/queues_stacks/setofStacks.py rename to src/abstract_structures/stacks/queues_stacks/setofStacks.py diff --git a/src/abstract_structures/queues_stacks/setofStacksList.py b/src/abstract_structures/stacks/queues_stacks/setofStacksList.py similarity index 100% rename from src/abstract_structures/queues_stacks/setofStacksList.py rename to src/abstract_structures/stacks/queues_stacks/setofStacksList.py diff --git a/src/abstract_structures/queues_stacks/sort_stack.py b/src/abstract_structures/stacks/queues_stacks/sort_stack.py similarity index 100% rename from src/abstract_structures/queues_stacks/sort_stack.py rename to src/abstract_structures/stacks/queues_stacks/sort_stack.py diff --git a/src/abstract_structures/queues_stacks/stackMinElemO1.py b/src/abstract_structures/stacks/queues_stacks/stackMinElemO1.py similarity index 100% rename from src/abstract_structures/queues_stacks/stackMinElemO1.py rename to src/abstract_structures/stacks/queues_stacks/stackMinElemO1.py diff --git a/src/abstract_structures/queues_stacks/stack_.py b/src/abstract_structures/stacks/queues_stacks/stack_.py similarity index 100% rename from src/abstract_structures/queues_stacks/stack_.py rename to src/abstract_structures/stacks/queues_stacks/stack_.py diff --git a/src/abstract_structures/queues_stacks/towers_of_hanoi.py b/src/abstract_structures/stacks/queues_stacks/towers_of_hanoi.py similarity index 100% rename from src/abstract_structures/queues_stacks/towers_of_hanoi.py rename to src/abstract_structures/stacks/queues_stacks/towers_of_hanoi.py