-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_example.py
139 lines (125 loc) · 3.83 KB
/
linked_list_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Example Problem Linked Lists
How to Reverse a List
"""
class LinkedList:
"""
LinkedList class to implement the data structure. The purpose of this example is to show how to reverse a Linked List
"""
class Node:
"""
Create the structure for a Node
"""
def __init__(self, data):
"""
Initialized the Node to the data provided
"""
self.data = data
self.next = None
self.prev = None
def __init__(self):
"""
Initiliaze an empty linked list
"""
self.head = None
self.tail = None
self.reversed_list = list()
def insert_head(self, value):
"""
Insert a new Node as the head of the list
"""
new_node = LinkedList.Node(value)
# Case 1 - List is empty
if self.head == None:
self.head = new_node
self.tail = new_node
# Case 2 - List is not empty
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
def insert_tail(self, value):
"""
Insert a new Node at the tail of the list
"""
new_node = LinkedList.Node(value)
# Case 1 - List is empty
if self.tail == None and self.head == None:
self.head = new_node
self.tail = new_node
# Case 2 - List is not empty
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
def insert_after(self, value):
"""
Insert new_value after the occurrence of value in the linked list
"""
# Start searching at the head of the list
current = self.head
while current is not None:
if current.data == value:
# Check if the value is the tail
# If so user the 'insert_tail() method'
if current == self.tail:
self.insert_tail(value)
# Insert the value in the middle of the list
else:
new_node = LinkedList.Node(value)
new_node.prev = current
new_node.next = current.next
current.next.prev = new_node
current.next = new_node
return
# Check the next value in the list
current = current.next
def rev_l(self):
"""
Return the reversed linked list
"""
# Set the current Node to the tail
current = self.tail
# Iterate over the list from the tail to the head
while current is not None:
# Append the value to the list
self.reversed_list.append(current.data)
# Set current to the previous node
current = current.prev
# Return the list
return self.reversed_list
def __str__(self):
"""
Returns a string representation of the object
"""
output = "linkedlist["
first = True
for value in self:
if first:
first = False
else:
output += ", "
output += str(value)
output += "]"
return output
def __iter__(self):
"""
Iterate forward through the Linked List
"""
# Start at the beginning of the list
current = self.head
# Iterate over the entire link
while current != None:
# Provide or give each item to the user
yield current.data
# Go to the next Node
current = current.next
# Test Cases
my_ll = LinkedList()
my_ll.insert_head(9)
my_ll.insert_head(7)
my_ll.insert_head(5)
my_ll.insert_tail(3)
my_ll.insert_tail(1)
my_ll.insert_tail(0)
print(my_ll)
print(my_ll.rev_l())