From cd3e49e9591d5190a9022a026762c0c5a6457690 Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Sun, 9 Oct 2022 19:39:32 -0700 Subject: [PATCH 1/4] fixes insertion logic for append method --- Chapter04/append_at_any_location_singly_linkedlist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index c2e1f2f..05aabe3 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -28,12 +28,12 @@ def append_at_a_location(self, data, index): node = Node(data) count = 1 while current: - if count == 1: + if index == 1: node.next = current self.head = node print(count) return - elif index == index: + elif count == index: node.next = current prev.next = node return From cea779127ef5e5ab2637de1bffef007f5e79eed5 Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Sun, 9 Oct 2022 19:45:38 -0700 Subject: [PATCH 2/4] append to check if index equals one once --- ...ppend_at_any_location_singly_linkedlist.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index 05aabe3..7f39b4c 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -27,19 +27,20 @@ def append_at_a_location(self, data, index): prev = self.head node = Node(data) count = 1 - while current: - if index == 1: + if index == 1: node.next = current self.head = node print(count) - return - elif count == index: - node.next = current - prev.next = node - return - count += 1 - prev = current - current = current.next + return + else: + while current: + if count == index: + node.next = current + prev.next = node + return + count += 1 + prev = current + current = current.next if count < index: print("The list has less number of elements") From 44e2762850b7a7408c848d5c836f1759b4e89d90 Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Sun, 9 Oct 2022 21:47:48 -0700 Subject: [PATCH 3/4] fixed double linked list delete method and size operations --- Chapter04/append_at_any_location_singly_linkedlist.py | 7 +++++-- Chapter04/delete_operation_doubly_linkedlist.py | 10 +++++----- Chapter04/delete_operation_singly_linkedlist.py | 6 ++++-- Chapter04/doubly_linked_list.py | 8 ++++---- Chapter04/faster_append_singly_linked_list.py | 1 + Chapter04/list_traversal_singly_linklist.py | 7 ++++--- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index 7f39b4c..ef31d98 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -15,12 +15,13 @@ def append(self, data): # Encapsulate the data in a Node node = Node(data) if self.head is None: - self.head = node + self.head = node else: current = self.head while current.next: current = current.next current.next = node + self.size += 1 def append_at_a_location(self, data, index): current = self.head @@ -30,13 +31,15 @@ def append_at_a_location(self, data, index): if index == 1: node.next = current self.head = node + self.size += 1 print(count) return else: while current: if count == index: node.next = current - prev.next = node + prev.next = node + self.size += 1 return count += 1 prev = current diff --git a/Chapter04/delete_operation_doubly_linkedlist.py b/Chapter04/delete_operation_doubly_linkedlist.py index 8bdf96d..99dc5c7 100644 --- a/Chapter04/delete_operation_doubly_linkedlist.py +++ b/Chapter04/delete_operation_doubly_linkedlist.py @@ -8,7 +8,7 @@ class DoublyLinkedList: def __init__ (self): self.head = None self.tail = None - self.count = 0 + self.size = 0 def append(self, data): #Append an item to the list. @@ -20,7 +20,7 @@ def append(self, data): new_node.prev = self.tail self.tail.next = new_node self.tail = new_node - self.count += 1 + self.size += 1 @@ -31,9 +31,9 @@ def delete(self, data): if current is None: #List is empty print("List is empty") elif current.data == data: #Item to be deleted is found at starting of list - self.head.prev = None - node_deleted = True self.head = current.next + self.head.prev = None + node_deleted = True elif self.tail.data == data: #Item to be deleted is found at the end of list. self.tail = self.tail.prev @@ -50,7 +50,7 @@ def delete(self, data): if node_deleted == False: #Item to be deleted is not found in the list print("Item not found") if node_deleted: - self.count -= 1 + self.size -= 1 diff --git a/Chapter04/delete_operation_singly_linkedlist.py b/Chapter04/delete_operation_singly_linkedlist.py index c181c0c..8fd8350 100644 --- a/Chapter04/delete_operation_singly_linkedlist.py +++ b/Chapter04/delete_operation_singly_linkedlist.py @@ -13,12 +13,13 @@ def append(self, data): # Encapsulate the data in a Node node = Node(data) if self.head is None: - self.head = node + self.head = node else: current = self.head while current.next: - current = current.next + current = current.next current.next = node + self.size += 1 def delete_first_node (self): current = self.head @@ -26,6 +27,7 @@ def delete_first_node (self): print("No data element to delete") elif current == self.head: self.head = current.next + self.size -= 1 def delete_last_node (self): diff --git a/Chapter04/doubly_linked_list.py b/Chapter04/doubly_linked_list.py index 4eb2f94..4011a19 100644 --- a/Chapter04/doubly_linked_list.py +++ b/Chapter04/doubly_linked_list.py @@ -8,7 +8,7 @@ class DoublyLinkedList: def __init__ (self): self.head = None self.tail = None - self.count = 0 + self.size = 0 def append(self, data): #Append an item at the end of the list. @@ -20,7 +20,7 @@ def append(self, data): new_node.prev = self.tail self.tail.next = new_node self.tail = new_node - self.count += 1 + self.size += 1 def append_at_start(self, data): @@ -33,7 +33,7 @@ def append_at_start(self, data): new_node.next = self.head self.head.prev = new_node self.head = new_node - self.count += 1 + self.size += 1 def append_at_a_location(self, data): current = self.head @@ -45,7 +45,7 @@ def append_at_a_location(self, data): new_node.next = current prev.next = new_node current.prev = new_node - self.count += 1 + self.size += 1 prev = current current = current.next diff --git a/Chapter04/faster_append_singly_linked_list.py b/Chapter04/faster_append_singly_linked_list.py index 1d0b25b..e205055 100644 --- a/Chapter04/faster_append_singly_linked_list.py +++ b/Chapter04/faster_append_singly_linked_list.py @@ -19,6 +19,7 @@ def append(self, data): else: self.head = node self.tail = node + self.size += 1 words = SinglyLinkedList() diff --git a/Chapter04/list_traversal_singly_linklist.py b/Chapter04/list_traversal_singly_linklist.py index 5b6d259..b991d42 100644 --- a/Chapter04/list_traversal_singly_linklist.py +++ b/Chapter04/list_traversal_singly_linklist.py @@ -14,13 +14,14 @@ def append(self, data): # Encapsulate the data in a Node node = Node(data) if self.head is None: - self.head = node + self.head = node else: current = self.head while current.next: - current = current.next + current = current.next current.next = node - + self.size += 1 + def iter(self): current = self.head while current: From e746541261bf44e46fbaea5ab31e3bb2ba4a1121 Mon Sep 17 00:00:00 2001 From: Chris Thompson Date: Sun, 9 Oct 2022 22:08:57 -0700 Subject: [PATCH 4/4] fixed white space and consistency with book --- Chapter04/CircularList.py | 12 +++++------- ...ppend_at_any_location_singly_linkedlist.py | 11 +++-------- .../delete_operation_doubly_linkedlist.py | 19 ++++++++----------- .../delete_operation_singly_linkedlist.py | 7 +++---- Chapter04/doubly_linked_list.py | 5 ++--- Chapter04/list_traversal_singly_linklist.py | 2 +- 6 files changed, 22 insertions(+), 34 deletions(-) diff --git a/Chapter04/CircularList.py b/Chapter04/CircularList.py index e179121..cecfd17 100644 --- a/Chapter04/CircularList.py +++ b/Chapter04/CircularList.py @@ -3,10 +3,8 @@ class Node: def __init__(self, data=None): self.data = data self.next = None - - - - + + class CircularList: def __init__ (self): self.tail = None @@ -16,13 +14,13 @@ def __init__ (self): def append(self, data): node = Node(data) if self.tail: - self.tail.next = node - self.tail = node + self.tail.next = node node.next = self.head + self.tail = node else: self.head = node self.tail = node - self.tail.next = self.tail + self.tail.next = self.head self.size += 1 def delete(self, data): diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index ef31d98..b0993fc 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -4,7 +4,7 @@ def __init__(self, data=None): self.data = data self.next = None - + class SinglyLinkedList: def __init__ (self): self.tail = None @@ -47,10 +47,7 @@ def append_at_a_location(self, data, index): if count < index: print("The list has less number of elements") - - - - + words = SinglyLinkedList() words.append('egg') words.append('ham') @@ -60,9 +57,7 @@ def append_at_a_location(self, data, index): while current: print(current.data) current = current.next - - - + words.append_at_a_location('new', 2) current = words.head diff --git a/Chapter04/delete_operation_doubly_linkedlist.py b/Chapter04/delete_operation_doubly_linkedlist.py index 99dc5c7..a842188 100644 --- a/Chapter04/delete_operation_doubly_linkedlist.py +++ b/Chapter04/delete_operation_doubly_linkedlist.py @@ -3,7 +3,8 @@ def __init__ (self, data = None, next = None, prev = None): self.data = data self.next = next self.prev = prev - + + class DoublyLinkedList: def __init__ (self): self.head = None @@ -22,8 +23,6 @@ def append(self, data): self.tail = new_node self.size += 1 - - def delete(self, data): # Delete a node from the list. current = self.head @@ -32,14 +31,14 @@ def delete(self, data): print("List is empty") elif current.data == data: #Item to be deleted is found at starting of list self.head = current.next - self.head.prev = None + self.head.prev = None node_deleted = True elif self.tail.data == data: #Item to be deleted is found at the end of list. - self.tail = self.tail.prev - self.tail.next = None - node_deleted = True - + self.tail = self.tail.prev + self.tail.next = None + node_deleted = True + else: while current: #search item to be deleted, and delete that node if current.data == data: @@ -52,9 +51,7 @@ def delete(self, data): if node_deleted: self.size -= 1 - - - + #Code to create for a doubly linked list words = DoublyLinkedList() words.append('egg') diff --git a/Chapter04/delete_operation_singly_linkedlist.py b/Chapter04/delete_operation_singly_linkedlist.py index 8fd8350..8b05d10 100644 --- a/Chapter04/delete_operation_singly_linkedlist.py +++ b/Chapter04/delete_operation_singly_linkedlist.py @@ -4,6 +4,7 @@ def __init__(self, data=None): self.data = data self.next = None + class SinglyLinkedList: def __init__ (self): self.head = None @@ -28,8 +29,7 @@ def delete_first_node (self): elif current == self.head: self.head = current.next self.size -= 1 - - + def delete_last_node (self): current = self.head prev = self.head @@ -39,8 +39,7 @@ def delete_last_node (self): self.size -= 1 prev = current current = current.next - - + def delete(self, data): current = self.head prev = self.head diff --git a/Chapter04/doubly_linked_list.py b/Chapter04/doubly_linked_list.py index 4011a19..8cfcfb7 100644 --- a/Chapter04/doubly_linked_list.py +++ b/Chapter04/doubly_linked_list.py @@ -3,7 +3,8 @@ def __init__ (self, data = None, next = None, prev = None): self.data = data self.next = next self.prev = prev - + + class DoublyLinkedList: def __init__ (self): self.head = None @@ -66,8 +67,6 @@ def contains(self, data): return - - words = DoublyLinkedList() words.append('egg') words.append('ham') diff --git a/Chapter04/list_traversal_singly_linklist.py b/Chapter04/list_traversal_singly_linklist.py index b991d42..bf7ef20 100644 --- a/Chapter04/list_traversal_singly_linklist.py +++ b/Chapter04/list_traversal_singly_linklist.py @@ -29,7 +29,7 @@ def iter(self): current = current.next yield val - + words = SinglyLinkedList() words.append('egg') words.append('ham')