-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List.py
92 lines (77 loc) · 2.29 KB
/
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
84
85
86
87
88
89
90
91
92
class Node:
# create node
def __init__(self, val):
self.next = None
self.prev = None
self.val = val
# declare head, tail and size for linked list
class doublelinkedlist:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def add_node(self, val):
#declare node
node = Node(val) #refers to the first node and its value
if self.tail is None:
self.head = node
self.tail = node
self.size +=1
else:
#if it has values
self.tail.next = node
node.prev = self.tail
self.tail = node
self.size += 1
def remove_node(self,node):
# if the node is at the beginning or not
if node.prev is None:
self.head = node.next
else:
node.prev.next = node.next
if node.next is None:
self.tail = node.prev
else:
node.next.prev = node.prev
self.size -= 1
def remove_last_node(self):
if self.tail is not None:
self.remove_node(self.tail)
def remove_first_node(self):
if self.head is not None:
self.remove_node(self.head)
def remove(self,value):
node = self.head
while node is not None:
#if the value of node and given value is equal then remove it
if node.val == value: #the value which will be passed for removing
self.remove_node(node)
node = node.next #it will iterate to next node
def tracelastval(self):
return self.tail.val
def tracefirstval(self):
return self.head.val
def __str__(self):
vals=[]
node = self.head
while node is not None:
vals.append(node.val)
node = node.next
return f"[{', '.join(str(val) for val in vals)}]"
# mylist = doublelinkedlist()
# mylist.add_node(1)
# mylist.add_node(5)
# mylist.add_node(4)
# print(mylist)
# print(mylist.size)
# mylist.remove(4)
# print(mylist)
# print(mylist.size)
#
# mylist.remove_last_node()
# print(mylist)
# print(mylist.size)
#
# mylist.remove_first_node()
# print(mylist)
# print(mylist.size)