-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove.py
98 lines (74 loc) · 2.07 KB
/
remove.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
class Node:
def __init__(self, data=None, next_node=None, prev_node=None):
self.data = data
self.next = next_node
self.prev = prev_node
class DoublyLinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
node = Node(data, self.head, None)
if self.head:
self.head.prev = node
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None, itr)
def print_forward(self):
if self.head is None:
print("Doubly Linked List is empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data) + ' <--> '
itr = itr.next
print(llstr)
def print_backward(self):
if self.head is None:
print("Doubly Linked List is empty")
return
itr = self.head
while itr.next:
itr = itr.next
llstr = ''
while itr:
llstr += str(itr.data) + ' <--> '
itr = itr.prev
print(llstr)
dll = DoublyLinkedList()
dll.insert_at_beginning(2)
dll.insert_at_beginning(4)
dll.insert_at_beginning(6)
dll.insert_at_beginning(8)
dll.insert_at_beginning(10)
dll.insert_at_end(111)
dll.insert_at_end(321)
dll.insert_at_end(1000)
dll.insert_at_end(2023)
dll.print_forward()
dll.print_backward()
#
# Doubly Linked List implementation
# class Node:
# def __init__(self, data=None, next=None, prev=None):
# self.data = data
# self.next = next
# self.prev = prev
# class doublyLinkedList:
# def __init__(self):
# self.head = None
# def insert_at_beginning(self,data):
# node = Node(data,self.head)
def remove_by_value(self,value):
itr = self.head
while itr:
if itr.data == value:
itr.next = itr.next.next
break
itr = itr.next