-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked.py
66 lines (58 loc) · 1.51 KB
/
linked.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
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None
#traversing list
def listprint(self):
temp = self.headval
while temp is not None:
print (temp.dataval)
temp = temp.nextval
#insert front
def insert_frount(self, item):
new = Node(item)
new.nextval= self.headval
self.headval = new
#insert rear
def insert_r(self, item):
new = Node(item)
if self.headval == None:
self.headval = new
return
temp = self.headval
while temp.nextval is not None:
temp = temp.nextval
temp.nextval = new
def delete_front(self):
if self.headval == None:
prin("list is empty")
return
temp = self.headval
self.headval = temp.nextval
print("the deleted front element is ", temp.dataval)
del temp
def delete_rear(self):
if self.headval == None:
prin("list is empty")
return
temp = self.headval
previous = None
while temp.nextval is not None:
previous = temp
temp = temp.nextval
print("the deleted rear element is ", temp.dataval)
del temp
previous.nextval = None
list1 = SLinkedList()
list1.insert_frount(10)
list1.insert_frount(20)
list1.insert_r(30)
list1.insert_r(40)
list1.insert_frount(50)
list1.delete_front()
list1.delete_rear()
list1.delete_front()
list1.listprint()