5
5
@author: mishr
6
6
"""
7
7
8
- #Singly Linked List
8
+ #Singly Linked List
9
+ #Node class
9
10
10
11
class Node :
11
-
12
+ # Function to initialise the node object
12
13
def __init__ (self , data = None , next_node = None ):
13
14
self .data = data
14
15
self .next_node = next_node
15
16
16
-
17
+ # Linked List class contains a Node object
17
18
18
19
class singly_linked_list :
20
+ # Function to initialize head
19
21
def __init__ (self ):
20
-
21
22
self .head = Node ()
22
-
23
+
24
+ # Functio to insert a new node
23
25
def append (self , data ):
24
26
25
27
new_node = Node (data )
26
28
current = self .head
27
29
while current .next_node != None :
28
30
current = current .next_node
29
31
current .next_node = new_node
30
-
32
+
33
+ # Functio to know the length of the linked list
31
34
def length (self ):
32
35
33
36
current = self .head
34
- counter = 0
37
+ count = 0
35
38
while current .next_node != None :
36
- counter += 1
39
+ count += 1
37
40
current = current .next_node
38
41
39
- return counter
42
+ return count
40
43
44
+ # Functio to get value at a position in the linked list
41
45
def get_value_at (self , index ):
42
46
43
47
if index >= self .length ():
@@ -51,6 +55,7 @@ def get_value_at(self, index):
51
55
return current_node .data
52
56
current_index += 1
53
57
58
+ # Functio to delete the node in the linked list
54
59
def delete_at (self , index ):
55
60
56
61
if index >= self .length ():
@@ -65,7 +70,8 @@ def delete_at(self, index):
65
70
last_node .next_node = current_node .next_node
66
71
return
67
72
current_index += 1
68
-
73
+
74
+ # Functio to traverse and show the linked list
69
75
def display (self ):
70
76
71
77
node_list = []
@@ -91,4 +97,4 @@ def display(self):
91
97
#can use loop to take inputs
92
98
93
99
94
-
100
+
0 commit comments