-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_list-Ex02.py
36 lines (33 loc) · 993 Bytes
/
Linked_list-Ex02.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
class Node:
def __init__(self,data = None):
self.data = data
self.reference = None
class Linked_list:
def __init__(self):
self.head = None
def traverse(self):
presentNode = self.head
while presentNode:
print("DATA VALUE = ",presentNode.data)
presentNode = presentNode.reference
def insert_at_end(self,data):
new_data = Node(data)
presentNode = self.head
while presentNode.reference != None:
presentNode = presentNode.reference
presentNode.reference = new_data
objNode1 = Node(1)
objNode2 = Node(2)
objNode3 = Node(3)
objNode4 = Node(4)
linkObj = Linked_list()
#head of the linked list to first object
linkObj.head = objNode1
#reference of the first node object to second object
linkObj.head.reference = objNode2
objNode2.reference = objNode3
objNode3.reference = objNode4
linkObj.insert_at_end(5)
linkObj.insert_at_end(6)
linkObj.insert_at_end(7)
linkObj.traverse()