From ad048c7c7564f5b2234aa7d05a0597a00b98576b Mon Sep 17 00:00:00 2001 From: dholmes4 <dholmes4@huskers.unl.edu> Date: Thu, 27 Mar 2025 16:34:48 -0500 Subject: [PATCH] finalized lab. i'm not doing the bonus may god have mercy on me for not doing so --- lab - linked list lab/linked_list_example.py | 79 +++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/lab - linked list lab/linked_list_example.py b/lab - linked list lab/linked_list_example.py index 356f92f..565e9e4 100644 --- a/lab - linked list lab/linked_list_example.py +++ b/lab - linked list lab/linked_list_example.py @@ -75,9 +75,6 @@ class LinkedStack(object): def is_empty(self): return self.head is None -def main(): - test_linked_queue_duncan_version() - def test_linked_stack_duncan_version(): testing_stack = LinkedStack() print("testing_stack : "+str( testing_stack ) ) @@ -209,7 +206,7 @@ class LinkedDeque(object): implementation is very similar to code you wrote earlier.) """ def add_back(self, item): - new_node = Node(item) + new_node = doubly_linked_node(item) if self.head is None: self.head = new_node self.tail = new_node @@ -226,7 +223,7 @@ class LinkedDeque(object): for cleanly dealing with this kind of DRY problem.) """ def add_front(self, item): - new_node = Node(item) + new_node = doubly_linked_node(item) if self.head is None: self.head = new_node self.tail = new_node @@ -240,17 +237,44 @@ class LinkedDeque(object): item from the front of the deque. """ def remove_front(self): + """ + this also works but it's best to have symmetry with the twin removes + returnValue = self.head.item self.head = self.head.next_node 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 item from the back of the deque. """ def remove_back(self): + if self.tail is None: + raise IndexError("Deque is empty!") + returnValue = self.tail.item - self.tail = self.tail.previous_node + + if self.tail == self.head: + self.head = None + self.tail = None + else: + self.tail = self.tail.previous_node + self.tail.next_node = None + return returnValue """ @@ -260,19 +284,56 @@ class LinkedDeque(object): return self.head is None def print_linked_list(self): + + string_spit_out = "" + current_node = self.head while current_node is not None: - print(current_node.item) + string_spit_out += str(current_node.item) + " " 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(): + + """ + load it up frontwards + """ 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(2) + testing_linked_deque.add_front(1) + + #see that it's structured correclty + 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__": main() \ No newline at end of file -- GitLab