Skip to content
Snippets Groups Projects
Commit ad048c7c authored by Duncan Holmes's avatar Duncan Holmes
Browse files

finalized lab.

i'm not doing the bonus
may god have mercy on me for not doing so 
parent 7a266135
No related branches found
No related tags found
No related merge requests found
...@@ -75,9 +75,6 @@ class LinkedStack(object): ...@@ -75,9 +75,6 @@ class LinkedStack(object):
def is_empty(self): def is_empty(self):
return self.head is None return self.head is None
def main():
test_linked_queue_duncan_version()
def test_linked_stack_duncan_version(): def test_linked_stack_duncan_version():
testing_stack = LinkedStack() testing_stack = LinkedStack()
print("testing_stack : "+str( testing_stack ) ) print("testing_stack : "+str( testing_stack ) )
...@@ -209,7 +206,7 @@ class LinkedDeque(object): ...@@ -209,7 +206,7 @@ class LinkedDeque(object):
implementation is very similar to code you wrote earlier.) implementation is very similar to code you wrote earlier.)
""" """
def add_back(self, item): def add_back(self, item):
new_node = Node(item) new_node = doubly_linked_node(item)
if self.head is None: if self.head is None:
self.head = new_node self.head = new_node
self.tail = new_node self.tail = new_node
...@@ -226,7 +223,7 @@ class LinkedDeque(object): ...@@ -226,7 +223,7 @@ class LinkedDeque(object):
for cleanly dealing with this kind of DRY problem.) for cleanly dealing with this kind of DRY problem.)
""" """
def add_front(self, item): def add_front(self, item):
new_node = Node(item) new_node = doubly_linked_node(item)
if self.head is None: if self.head is None:
self.head = new_node self.head = new_node
self.tail = new_node self.tail = new_node
...@@ -240,17 +237,44 @@ class LinkedDeque(object): ...@@ -240,17 +237,44 @@ class LinkedDeque(object):
item from the front of the deque. item from the front of the deque.
""" """
def remove_front(self): def remove_front(self):
"""
this also works but it's best to have symmetry with the twin removes
returnValue = self.head.item returnValue = self.head.item
self.head = self.head.next_node self.head = self.head.next_node
return returnValue return returnValue
"""
if self.head is None:
raise IndexError("Deque is empty!")
returnValue = self.head.item
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next_node
self.head.previous_node = None
return returnValue
""" """
Implement a remove_back method that removes and returns an Implement a remove_back method that removes and returns an
item from the back of the deque. item from the back of the deque.
""" """
def remove_back(self): def remove_back(self):
if self.tail is None:
raise IndexError("Deque is empty!")
returnValue = self.tail.item returnValue = self.tail.item
if self.tail == self.head:
self.head = None
self.tail = None
else:
self.tail = self.tail.previous_node self.tail = self.tail.previous_node
self.tail.next_node = None
return returnValue return returnValue
""" """
...@@ -260,19 +284,56 @@ class LinkedDeque(object): ...@@ -260,19 +284,56 @@ class LinkedDeque(object):
return self.head is None return self.head is None
def print_linked_list(self): def print_linked_list(self):
string_spit_out = ""
current_node = self.head current_node = self.head
while current_node is not None: while current_node is not None:
print(current_node.item) string_spit_out += str(current_node.item) + " "
current_node = current_node.next_node current_node = current_node.next_node
print(f"Our deque String as a whole is \n{string_spit_out}")
def test_linked_deque_duncan_version(): def test_linked_deque_duncan_version():
"""
load it up frontwards
"""
testing_linked_deque = LinkedDeque() testing_linked_deque = LinkedDeque()
testing_linked_deque.add_front(1)
testing_linked_deque.add_front(2)
testing_linked_deque.add_front(3) testing_linked_deque.add_front(3)
testing_linked_deque.add_front(2)
testing_linked_deque.add_front(1)
#see that it's structured correclty
testing_linked_deque.print_linked_list() testing_linked_deque.print_linked_list()
print("\n")
#unload it
while not testing_linked_deque.is_empty():
testing_linked_deque.remove_front()
testing_linked_deque.add_back(1)
testing_linked_deque.add_back(2)
testing_linked_deque.add_back(3)
testing_linked_deque.print_linked_list()
print("\ni'm gonna take 1 from the front")
testing_linked_deque.remove_front()
testing_linked_deque.print_linked_list()
print("\nreplacing the 1 i just took away ")
testing_linked_deque.add_front(1)
testing_linked_deque.print_linked_list()
print("\ni'm gonna take 3 from the back")
testing_linked_deque.remove_back()
testing_linked_deque.print_linked_list()
def main():
test_linked_deque_duncan_version()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment