Skip to content

Commit d8ebfbb

Browse files
Update
1 parent 0674277 commit d8ebfbb

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

Chapter04/doubly_linked_list.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def __init__ (self, data = None, next = None, prev = None):
44
self.next = next
55
self.prev = prev
66

7-
class DoublyLinkedList(object):
7+
class DoublyLinkedList:
88
def __init__ (self):
99
self.head = None
1010
self.tail = None
@@ -49,21 +49,68 @@ def append_at_a_location(self, data):
4949
prev = current
5050
current = current.next
5151

52+
def iter(self):
53+
current = self.head
54+
while current:
55+
val = current.data
56+
current = current.next
57+
yield val
58+
59+
60+
def contains(self, data):
61+
for node_data in self.iter():
62+
if data == node_data:
63+
print(" Data item is present in the list. ")
64+
return
65+
print(" Data item is not present in the list. ")
66+
return
67+
68+
5269

5370

5471
words = DoublyLinkedList()
5572
words.append('egg')
5673
words.append('ham')
5774
words.append('spam')
5875

76+
print("Items in doubly linked list before append")
5977
current = words.head
6078
while current:
6179
print(current.data)
6280
current = current.next
6381

82+
words.append_at_start('book')
83+
84+
print("Items in doubly linked list after append")
85+
current = words.head
86+
while current:
87+
print(current.data)
88+
current = current.next
89+
90+
91+
words.append('book')
92+
93+
print("Items in doubly linked list after adding element at end.")
94+
current = words.head
95+
while current:
96+
print(current.data)
97+
current = current.next
6498

99+
100+
65101
words.append_at_a_location('ham')
102+
print("Doubly linked list after adding an element after word \"ham\" in the list.")
66103
current = words.head
67104
while current:
68105
print(current.data)
69106
current = current.next
107+
108+
109+
words = DoublyLinkedList()
110+
words.append('egg')
111+
words.append('ham')
112+
words.append('spam')
113+
114+
115+
words.contains("ham")
116+
words.contains("ham2")

0 commit comments

Comments
 (0)