-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singular linkeList.py
130 lines (102 loc) · 2.8 KB
/
Singular linkeList.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Node:
def __init__(self,data):
self.data = data
self.ref = None
class LinkedList:
def __init__(self):
self.head = None
def traverse(self):
n = self.head
if n is None:
print("LinkedList is empty")
else:
while n is not None:
print(n.data, end="==>")
n = n.ref
def add_begin(self,data):
node1 = Node(data)
node1.ref = self.head
self.head = node1
def add_end(self,data):
n = self.head
node1 = Node(data)
if n is None:
self.head = node1
else:
while n.ref is not None:
n = n.ref
n.ref = node1
def add_afternode(self,data,x):
n = self.head
while n.ref is not None:
if n.data == x:
break
n = n.ref
if n is None:
print("node is not present in Linked List")
else:
node1 = Node(data)
node1.ref = n.ref
n.ref = node1
def add_beforenode(self,data,x):
if self.head is None:
print("Linked list is empty")
elif self.head.data == x:
node1 = Node(data)
node1.ref = self.head
self.head = node1
n = self.head
while n.ref is not None:
if n.ref.data == x:
break
n = n.ref
if n.ref is None:
print("Node is not found")
else:
node1 = Node(data)
node1.ref = n.ref
n.ref = node1
def delete_begin(self):
if self.head is None:
print("LnkedList is empty")
else:
self.head = self.head.ref
def delete_end(self):
if self.head is None:
print("LnkedList is empty")
elif self.head.ref is None:
self.head = None
else:
n = self.head
while n.ref.ref is not None:
n = n.ref
n.ref = None
def delete_by_value(self,x):
if self.head is None:
print("LinkList is empty")
elif self.head.data == x:
self.head = self.head.ref
else:
n = self.head
while n.ref is not None:
if x == n.ref.data:
break
n = n.ref
if n.ref is None:
print("Node is not present")
else:
n.ref = n.ref.ref
ll = LinkedList()
ll.add_begin(30)
ll.add_end(100)
ll.add_afternode(50,30)
ll.add_begin(20)
ll.add_begin(10)
ll.add_afternode(35,30)
ll.add_afternode(15,10)
ll.add_beforenode(5,10)
ll.add_beforenode(25,30)
ll.delete_begin()
ll.delete_end()
ll.delete_by_value(5)
ll.traverse()