-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-linked-list.py
83 lines (66 loc) · 1.91 KB
/
single-linked-list.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
from os import remove
class Node:
def __init__(self,dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None
def AtBegining(self,newdata):
NewNode = Node(newdata)
NewNode.nextval = self.headval
self.headval = NewNode
def AtEnd(self,newdata):
NewNode = Node(newdata)
curr = self.headval
while curr is not None:
if curr.nextval is None:
break
curr = curr.nextval
curr.nextval = NewNode
def InBetween(self,middle_node,newdata):
if middle_node is None:
print("middle node is null")
return
NewNode = Node("Sat")
NewNode.nextval = middle_node.nextval
middle_node.nextval = NewNode
def RemoveNode(self,removenode):
if removenode is None:
print("middle node is null")
return
curr = self.headval
prev = self.headval
while curr is not None:
if curr.dataval == removenode:
break
else:
prev = curr
curr = curr.nextval
nextnode = curr.nextval
prev.nextval = nextnode
def printll(self):
printvalue = self.headval
while printvalue is not None:
print(printvalue.dataval)
printvalue = printvalue.nextval
list1 = SLinkedList()
list1.headval = Node ("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
list1.headval.nextval = e2
e2.nextval = e3
#insert at the begining
list1.AtBegining("Sun")
list1.printll()
# at the end
list1.AtEnd("Thur")
list1.printll()
# in between
print("-----")
list1.InBetween(list1.headval,"Sat")
list1.printll()
# remove node
print("-----")
list1.RemoveNode("Sat")
list1.printll()