diff --git a/stacks_queues/linked_list.py b/stacks_queues/linked_list.py index 584e8c4..d1114e1 100644 --- a/stacks_queues/linked_list.py +++ b/stacks_queues/linked_list.py @@ -22,11 +22,13 @@ def __init__(self): # Space Complexity O(1) def add_first(self, value): new_node = Node(value) - new_node.next = self.head - if self.head: + old_head = self.head self.head.previous = new_node - self.head = new_node + self.head = new_node + new_node.next = old_head + else: + self.head = new_node if not self.tail: self.tail = self.head @@ -240,7 +242,6 @@ def get_last(self): def __str__(self): values = [] - current = self.head while current: values.append(str(current.value)) @@ -248,8 +249,8 @@ def __str__(self): return ", ".join(values) -ll = LinkedList() -ll.add_first(5) -ll.add_first(25) -ll.add_last(1) -print(ll) +# ll = LinkedList() +# ll.add_first(5) +# ll.add_first(25) +# ll.add_last(1) +# print(ll) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..8eef6fd 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,4 +1,7 @@ +from tracemalloc import start + + INITIAL_QUEUE_SIZE = 20 class QueueFullException(Exception): @@ -13,7 +16,8 @@ def __init__(self): self.store = [None] * INITIAL_QUEUE_SIZE self.buffer_size = INITIAL_QUEUE_SIZE self.front = -1 - self.rear = -1 + self.rear = -1 + # -1 is just a marker to indicate nothing in the list, we can mark as None as well self.size = 0 @@ -23,34 +27,53 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if self.size == self.buffer_size: + raise QueueFullException('Queue is full!') + + if self.size == 0: + self.front = 0 + self.rear = 0 + self.store[self.rear] = element #insert where rear and front were pointing at first. But rear is the one moving to indicate the position of insert + self.rear = (self.rear + 1) % self.buffer_size #the next insert position + self.size += 1 def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if The Queue is empty. """ - pass + if self.empty(): + raise QueueEmptyException + front_element = self.store[self.front] + self.store[self.front] = None + self.front = (self.front + 1) % self.buffer_size + self.size -= 1 + return front_element + # check if empty, if so raise an exception + # find and store the front element + # move front to the next index + # return the old front elements + # front and rear are indexes, like 0, 1 def front(self): """ Returns an element from the front of the Queue and None if the Queue is empty. Does not remove anything. """ - pass + return self.store[self.front] def size(self): """ Returns the number of elements in The Queue """ - pass + return self.size def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + return self.front == self.rear def __str__(self): """ Returns the Queue in String form like: @@ -58,4 +81,14 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + queue_list = [] + start_index = self.front + while (start_index % INITIAL_QUEUE_SIZE) < INITIAL_QUEUE_SIZE and len(queue_list) < self.size: + if self.store[start_index % INITIAL_QUEUE_SIZE] is not None: + queue_list.append(self.store[start_index % INITIAL_QUEUE_SIZE]) + start_index += 1 + else: + start_index += 1 + return str(queue_list) + + diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..7631d3b 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -12,8 +12,8 @@ def push(self, element): """ Adds an element to the top of the Stack. Returns None """ - pass - + self.store.add_first(element) + def pop(self): """ Removes an element from the top Of the Stack @@ -21,13 +21,15 @@ def pop(self): The Stack is empty. returns None """ - pass + + return self.store.remove_first() + def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + return self.store.empty() def __str__(self): """ Returns the Stack in String form like: @@ -35,4 +37,10 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + current = self.store.head + storeStacks = [] + while current: + storeStacks.append(str(current.value)) + current = current.next + return ", ".join(storeStacks) +