Skip to content

Commit 608a462

Browse files
committed
Merge branch 'fix-singly_linked_list' of git://github.com/ashwek/Python-1 into ashwek-fix-singly_linked_list
2 parents d8e3302 + f89d3a9 commit 608a462

File tree

1 file changed

+66
-39
lines changed

1 file changed

+66
-39
lines changed

data_structures/linked_list/singly_linked_list.py

+66-39
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,53 @@ def __init__(self, data):
88

99

1010
class Linked_List:
11-
def insert_tail(Head, data):
12-
if Head.next is None:
13-
Head.next = Node(data)
11+
def __init__(self):
12+
self.Head = None # Initialize Head to None
13+
14+
def insert_tail(self, data):
15+
if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head
1416
else:
15-
Head.next.insert_tail(data)
17+
temp = self.Head
18+
while(temp.next != None): #traverse to last node
19+
temp = temp.next
20+
temp.next = Node(data) #create node & link to tail
1621

17-
def insert_head(Head, data):
18-
tamp = Head
19-
if tamp is None:
20-
newNod = Node() # create a new Node
21-
newNod.data = data
22-
newNod.next = None
23-
Head = newNod # make new node to Head
24-
else:
25-
newNod = Node()
26-
newNod.data = data
27-
newNod.next = Head # put the Head at NewNode Next
28-
Head = newNod # make a NewNode to Head
29-
return Head
22+
def insert_head(self, data):
23+
newNod = Node(data) # create a new node
24+
if self.Head != None:
25+
newNod.next = self.Head # link newNode to head
26+
self.Head = newNod # make NewNode as Head
3027

31-
def printList(Head): # print every node data
32-
tamp = Head
28+
def printList(self): # print every node data
29+
tamp = self.Head
3330
while tamp is not None:
3431
print(tamp.data)
3532
tamp = tamp.next
3633

37-
def delete_head(Head): # delete from head
38-
if Head is not None:
39-
Head = Head.next
40-
return Head # return new Head
41-
42-
def delete_tail(Head): # delete from tail
43-
if Head is not None:
44-
tamp = Node()
45-
tamp = Head
46-
while tamp.next.next is not None: # find the 2nd last element
47-
tamp = tamp.next
48-
# delete the last element by give next None to 2nd last Element
49-
tamp.next = None
50-
return Head
34+
def delete_head(self): # delete from head
35+
temp = self.Head
36+
if self.Head != None:
37+
self.Head = self.Head.next
38+
temp.next = None
39+
return temp
40+
41+
def delete_tail(self): # delete from tail
42+
tamp = self.Head
43+
if self.Head != None:
44+
if(self.Head.next is None): # if Head is the only Node in the Linked List
45+
self.Head = None
46+
else:
47+
while tamp.next.next is not None: # find the 2nd last element
48+
tamp = tamp.next
49+
tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element
50+
return tamp
5151

52-
def isEmpty(Head):
53-
return Head is None # Return if Head is none
52+
def isEmpty(self):
53+
return self.Head is None # Return if Head is none
5454

55-
def reverse(Head):
55+
def reverse(self):
5656
prev = None
57-
current = Head
57+
current = self.Head
5858

5959
while current:
6060
# Store the current node's next node.
@@ -66,5 +66,32 @@ def reverse(Head):
6666
# Make the current node the next node (to progress iteration)
6767
current = next_node
6868
# Return prev in order to put the head at the end
69-
Head = prev
70-
return Head
69+
self.Head = prev
70+
71+
def main():
72+
A = Linked_List()
73+
print("Inserting 10 at Head")
74+
A.insert_head(10)
75+
print("Inserting 0 at Head")
76+
A.insert_head(0)
77+
print("\nPrint List : ")
78+
A.printList()
79+
print("\nInserting 100 at Tail")
80+
A.insert_tail(100)
81+
print("Inserting 1000 at Tail")
82+
A.insert_tail(1000)
83+
print("\nPrint List : ")
84+
A.printList()
85+
print("\nDelete Head")
86+
A.delete_head()
87+
print("Delete Tail")
88+
A.delete_tail()
89+
print("\nPrint List : ")
90+
A.printList()
91+
print("\nReverse Linked List")
92+
A.reverse()
93+
print("\nPrint List : ")
94+
A.printList()
95+
96+
if __name__ == '__main__':
97+
main()

0 commit comments

Comments
 (0)