forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly_LinkedList.py
110 lines (79 loc) · 2.61 KB
/
singly_LinkedList.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
class Node: #create a Node
def __int__(self,data):
self.data=data #given data
self.next=None #given next to None
class Linked_List:
pass
def insert_tail(Head,data): #insert the data at tail
tamp=Head
if(tamp==None):
newNod=Node() #create newNode Node type and given data and next
newNod.data=data
newNod.next=None
Head=newNod
else:
while tamp.next!=None: #reaches the last Node
tamp=tamp.next
newNod = Node() #create a new node
newNod.data = data
newNod.next = None
tamp.next=newNod #put the newnode into last node
return Head
def insert_head(Head,data):
tamp = Head
if (tamp == None):
newNod = Node() #create a new Node
newNod.data = data
newNod.next = None
Head = newNod #make new node to Head
# print(Head.data)
return Head
else:
newNod = Node()
newNod.data = data
newNod.next = Head #put the Head at NewNode Next
Head=newNod # make a NewNode to Head
# print(tamp.data)
return Head
def Print(Head): #print every node data
tamp=Node()
tamp=Head
while tamp!=None:
print(tamp.data)
tamp=tamp.next
def delete_head(Head): #delete from head
if Head==None:
print("List is empty cannot delete")
else:
Head=Head.next
return Head #return new Head
def delete_tail(Head): #delete from tail
if Head==None:
print("List is empty cannot delete")
else:
tamp = Node()
tamp = Head
while (tamp.next).next!= None: #reach tha 2nd last element
tamp = tamp.next
tamp.next=None #delet the last element by give next None to 2nd last Element
def isEmpty(Head):
if(Head==None): #check Head is None or Not
print("list is empty")
return True #return Ture if it is none
else:
print("Not empty")
return False #check False if it's not none
##check
Head=None
Head=Linked_List.insert_tail(Head,5)
Head=Linked_List.insert_tail(Head,6)
Head=Linked_List.insert_head(Head,7)
Head=Linked_List.insert_head(Head,9)
Linked_List.Print(Head)
print("delete_tail")
Linked_List.delete_tail(Head)
Linked_List.Print(Head)
print("delete_head")
Head=Linked_List.delete_head(Head)
Linked_List.Print(Head)
Linked_List.isEmpty(Head)