-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubly_Linked_List.py
143 lines (128 loc) · 4.47 KB
/
Doubly_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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class Node:
__slots__ = 'user_name', 'user_mail', '_next', '_previous'
def __init__(self, user_name, user_mail, _next, _previous):
self.user_name = user_name
self.user_mail = user_mail
self._next = _next
self._previous = _previous
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def __len__(self):
return self.size
def is_empty(self):
return self.size == 0
def create_user(self, name, mail):
new_user = Node(name, mail, None, None)
if self.is_empty():
self.head = new_user
self.tail = new_user
else:
self.tail._next = new_user
new_user._previous = self.tail
self.tail = new_user
self.size += 1
def display(self):
if self.is_empty():
print('The list is empty\n')
return False
else:
p = self.head
while p is not None:
print('User:', p.user_name)
print('Mail:', p.user_mail, '\n')
p = p._next
def display_reverse(self):
if self.is_empty():
print('The list is empty\n')
return False
else:
p = self.tail
while p is not None:
print('User:', p.user_name)
print('Mail:', p.user_mail, '\n')
p = p._previous
def search(self, mail):
if self.is_empty():
print('The list is empty\n')
return False
else:
p = self.head
while p is not None:
if p.user_mail == mail:
print('User with the mail', '"' + mail + '"', 'is found!')
print('User:', p.user_name)
print('Mail:', p.user_mail, '\n')
return True
p = p._next
print('User with mail', mail, 'is not found\n')
return False
def add_to_beginning(self, name, mail):
new_user = Node(name, mail, None, None)
if self.is_empty():
self.head = new_user
self.tail = new_user
else:
self.head._previous = new_user
new_user._next = self.head
self.head = new_user
self.size += 1
def add_user_to_random_place(self, add_name, add_mail, position):
if position < 1 or position > len(self):
print('The list contains', self.size, 'elements')
print("You can't insert in non existing position!\n")
return False
inserting_user = Node(add_name, add_mail, None, None)
p = self.head
i = 0
while i < position - 1:
p = p._next
i += 1
if p == self.head:
self.head._previous = inserting_user
inserting_user._next = self.head
self.head = inserting_user
self.size += 1
return inserting_user
elif p == self.tail:
inserting_user._next = self.tail
inserting_user._previous = self.tail._previous
self.tail._previous._next = inserting_user
self.tail._previous = inserting_user
self.size += 1
return inserting_user
inserting_user._next = p._next
p._next._previous = inserting_user
p._next = inserting_user
inserting_user._previous = p
self.size += 1
def remove_user(self, mail):
if self.is_empty():
print('The list is empty\n')
return False
else:
p = self.head
while p is not None:
if p.user_mail == mail:
break
p = p._next
if p is None:
print('User with mail', mail, 'is not found\n')
return False
elif p == self.head:
self.head = self.head._next
p._next = None
p = self.head
self.size -= 1
return p
elif p == self.tail:
self.tail = self.tail._previous
self.tail._next = None
self.size -= 1
return p
p._previous._next = p._next
p._next._previous = p._previous
self.size -= 1
user = DoublyLinkedList()