@@ -9,37 +9,36 @@ def __int__(self,data):
9
9
class Linked_List :
10
10
pass
11
11
def insert_tail (Head ,data ): #insert the data at tail
12
- tamp = Head
13
- if (tamp == None ):
12
+ tamp = Head #create a tamp as a head
13
+ if (tamp == None ): # if linkedlist is empty
14
14
newNod = Node () #create newNode Node type and given data and next
15
15
newNod .data = data
16
16
newNod .next = None
17
17
Head = newNod
18
18
19
19
else :
20
- while tamp .next != None : #reaches the last Node
20
+ while tamp .next != None : #find the last Node
21
21
tamp = tamp .next
22
22
newNod = Node () #create a new node
23
23
newNod .data = data
24
24
newNod .next = None
25
25
tamp .next = newNod #put the newnode into last node
26
26
27
- return Head
27
+ return Head #return first node of linked list
28
28
def insert_head (Head ,data ):
29
29
tamp = Head
30
30
if (tamp == None ):
31
31
newNod = Node () #create a new Node
32
32
newNod .data = data
33
33
newNod .next = None
34
34
Head = newNod #make new node to Head
35
- # print(Head.data)
36
35
return Head
37
36
else :
38
37
newNod = Node ()
39
38
newNod .data = data
40
39
newNod .next = Head #put the Head at NewNode Next
41
40
Head = newNod # make a NewNode to Head
42
- # print(tamp.data)
41
+
43
42
return Head
44
43
45
44
@@ -70,9 +69,9 @@ def delete_tail(Head): #delete from tail
70
69
else :
71
70
tamp = Node ()
72
71
tamp = Head
73
- while (tamp .next ).next != None : #reach tha 2nd last element
72
+ while (tamp .next ).next != None : #find the 2nd last element
74
73
tamp = tamp .next
75
- tamp .next = None #delet the last element by give next None to 2nd last Element
74
+ tamp .next = None #delete the last element by give next None to 2nd last Element
76
75
77
76
78
77
@@ -86,25 +85,4 @@ def isEmpty(Head):
86
85
87
86
88
87
89
- ##check
90
-
91
- Head = None
92
- Head = Linked_List .insert_tail (Head ,5 )
93
- Head = Linked_List .insert_tail (Head ,6 )
94
- Head = Linked_List .insert_head (Head ,7 )
95
- Head = Linked_List .insert_head (Head ,9 )
96
-
97
- Linked_List .Print (Head )
98
-
99
- print ("delete_tail" )
100
- Linked_List .delete_tail (Head )
101
- Linked_List .Print (Head )
102
-
103
-
104
- print ("delete_head" )
105
- Head = Linked_List .delete_head (Head )
106
- Linked_List .Print (Head )
107
-
108
- Linked_List .isEmpty (Head )
109
-
110
88
0 commit comments