-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle Linkedlist.py
More file actions
116 lines (101 loc) · 3.29 KB
/
Single Linkedlist.py
File metadata and controls
116 lines (101 loc) · 3.29 KB
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
# Single linkedlist
class Node:
def __init__(self, data):
self.data = data # create data or node
self.next = None # default next is none
class Sll:
def __init__(self):
self.head = None # Head node is starting node
def traversal(self):
if self.head is None:
print("linked list is empty")
else:
a = self.head
while a is not None:
print(a.data, end=" ")
a = a.next
print()
def insert_beginning(self, data):
print("Insert at the beginning:")
nb = Node(data)
nb.next = self.head
self.head = nb
def insert_at_end(self, data):
ne = Node(data)
a = self.head
print()
while a.next is not None:
print(a.data, end=" ")
a = a.next
a.next = ne
def insert_at_middle(self, pos, data):
nm = Node(data)
a = self.head
for i in range(1, pos - 1):
a = a.next
nm.next = a.next
a.next = nm
def delete_at_begining(self):
a = self.head
self.head = a.next
a.next = None
# def delete_at_the_end(self):
# a = self.head
# while a.next.next:
# a = a.next
# a.next = None
def delete_at_the_end(self):
a = self.head
prev = None
while a.next is not None:
prev = a
a = a.next
prev.next = None
def delete_at_position(self, pos):
if pos == 1:
self.head = self.head.next
a = self.head
prev = None
for i in range(1, pos):
prev = a
a = a.next
prev.next = a.next
n1 = Node(5) # create a data=5 and pass value in Node class
sll = Sll() # create a object of sll
sll.head = n1 # now assign a n1 object to sll.head variable now sll.head=5
n2 = Node(10) # and create another data=10 node and pass value in Node class
n1.next = n2 # now connect the two nodes
n3 = Node(15)
n2.next = n3
n4 = Node(20)
n3.next = n4
sll.traversal()
sll.insert_beginning(1)
sll.traversal()
sll.insert_at_end(25)
sll.traversal()
sll.insert_at_middle(3, 12)
sll.traversal()
sll.delete_at_begining()
sll.traversal()
sll.delete_at_the_end()
sll.traversal()
sll.delete_at_position(3)
sll.traversal()
# Output:
# 5 10 15 20
#
# First we pass the 5 data in Node class. n1 have n1.data=5
# Then we create the object for Sll default constructor will execute then it sll.head = none.
# Then we assign the sll.head = n1 which is nothing but n1 so sll.head = n1
# Now we create another data for n2 which have Node(10) now n2.data=10 wil assign
# now we need to connect nodes n1.next = none is default but we are modifing now n1.next = n2
# same way we are mapping untill last node
# we finally mapped how to print it.
# For that reason in Sll class we create function called tranversal()
# sll.traversal we are calling at last now it will check sll.head is none or not
# sll.head is n1 so it will go to else part there we are printing a = self.head we are keeping
# then we are checking in while a is not none inside while print the a.data
# then after we are reassigning a = a.next which is n2 again it went to while its not none
# then n2.data will print then n2.next = n3 now a = n3
# it will repeat untill last node.