Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified LinkedList API #191

Merged
merged 7 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 67 additions & 53 deletions pydatastructs/linear_data_structures/linked_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ def __len__(self):
def is_empty(self):
return self.size == 0

def search(self, key):
curr_node = self.head
while curr_node is not None:
if curr_node.key == key:
return curr_node
curr_node = curr_node.next
if curr_node is self.head:
return None
return None

def __str__(self):
"""
For printing the linked list.
"""
elements = []
current_node = self.head
while current_node is not None:
elements.append(current_node.data)
elements.append(str(current_node))
current_node = current_node.next
if current_node == self.head:
break
Expand All @@ -43,19 +53,19 @@ class DoublyLinkedList(LinkedList):
>>> from pydatastructs import DoublyLinkedList
>>> dll = DoublyLinkedList()
>>> dll.append(6)
>>> dll[0].data
>>> dll[0].key
6
>>> dll.head.data
>>> dll.head.key
6
>>> dll.append(5)
>>> dll.append_left(2)
>>> str(dll)
'[2, 6, 5]'
>>> dll[0].data = 7.2
>>> dll.extract(1).data
"['2', '6', '5']"
>>> dll[0].key = 7.2
>>> dll.extract(1).key
6
>>> str(dll)
'[7.2, 5]'
"['7.2', '5']"

References
==========
Expand All @@ -72,7 +82,7 @@ def __new__(cls):
obj.size = 0
return obj

def append_left(self, data):
def append_left(self, key, data=None):
"""
Pushes a new node at the start i.e.,
the left of the list.
Expand All @@ -83,9 +93,9 @@ def append_left(self, data):
data
Any valid data to be stored in the node.
"""
self.insert_at(0, data)
self.insert_at(0, key, data)

def append(self, data):
def append(self, key, data=None):
"""
Appends a new node at the end of the list.

Expand All @@ -95,9 +105,9 @@ def append(self, data):
data
Any valid data to be stored in the node.
"""
self.insert_at(self.size, data)
self.insert_at(self.size, key, data)

def insert_after(self, prev_node, data):
def insert_after(self, prev_node, key, data=None):
"""
Inserts a new node after the prev_node.

Expand All @@ -112,7 +122,7 @@ def insert_after(self, prev_node, data):
Any valid data to be stored in the node.
"""
self.size += 1
new_node = LinkedListNode(data,
new_node = LinkedListNode(key, data,
links=['next', 'prev'],
addrs=[None, None])
new_node.next = prev_node.next
Expand All @@ -124,7 +134,7 @@ def insert_after(self, prev_node, data):
if new_node.next is None:
self.tail = new_node

def insert_before(self, next_node, data):
def insert_before(self, next_node, key, data=None):
"""
Inserts a new node before the next_node.

Expand All @@ -139,7 +149,7 @@ def insert_before(self, next_node, data):
Any valid data to be stored in the node.
"""
self.size += 1
new_node = LinkedListNode(data,
new_node = LinkedListNode(key, data,
links=['next', 'prev'],
addrs=[None, None])
new_node.prev = next_node.prev
Expand All @@ -150,7 +160,7 @@ def insert_before(self, next_node, data):
else:
self.head = new_node

def insert_at(self, index, data):
def insert_at(self, index, key, data=None):
"""
Inserts a new node at the input index.

Expand All @@ -173,7 +183,7 @@ def insert_at(self, index, data):
raise IndexError('%d index is out of range.'%(index))

self.size += 1
new_node = LinkedListNode(data,
new_node = LinkedListNode(key, data,
links=['next', 'prev'],
addrs=[None, None])
if self.size == 1:
Expand Down Expand Up @@ -300,19 +310,19 @@ class SinglyLinkedList(LinkedList):
>>> from pydatastructs import SinglyLinkedList
>>> sll = SinglyLinkedList()
>>> sll.append(6)
>>> sll[0].data
>>> sll[0].key
6
>>> sll.head.data
>>> sll.head.key
6
>>> sll.append(5)
>>> sll.append_left(2)
>>> str(sll)
'[2, 6, 5]'
>>> sll[0].data = 7.2
>>> sll.extract(1).data
"['2', '6', '5']"
>>> sll[0].key = 7.2
>>> sll.extract(1).key
6
>>> str(sll)
'[7.2, 5]'
"['7.2', '5']"

References
==========
Expand All @@ -329,7 +339,7 @@ def __new__(cls):
obj.size = 0
return obj

def append_left(self, data):
def append_left(self, key, data=None):
"""
Pushes a new node at the start i.e.,
the left of the list.
Expand All @@ -340,9 +350,9 @@ def append_left(self, data):
data
Any valid data to be stored in the node.
"""
self.insert_at(0, data)
self.insert_at(0, key, data)

def append(self, data):
def append(self, key, data=None):
"""
Appends a new node at the end of the list.

Expand All @@ -352,9 +362,9 @@ def append(self, data):
data
Any valid data to be stored in the node.
"""
self.insert_at(self.size, data)
self.insert_at(self.size, key, data)

def insert_after(self, prev_node, data):
def insert_after(self, prev_node, key, data=None):
"""
Inserts a new node after the prev_node.

Expand All @@ -369,7 +379,7 @@ def insert_after(self, prev_node, data):
Any valid data to be stored in the node.
"""
self.size += 1
new_node = LinkedListNode(data,
new_node = LinkedListNode(key, data,
links=['next'],
addrs=[None])
new_node.next = prev_node.next
Expand All @@ -378,7 +388,7 @@ def insert_after(self, prev_node, data):
if new_node.next is None:
self.tail = new_node

def insert_at(self, index, data):
def insert_at(self, index, key, data=None):
"""
Inserts a new node at the input index.

Expand All @@ -401,7 +411,7 @@ def insert_at(self, index, data):
raise IndexError('%d index is out of range.'%(index))

self.size += 1
new_node = LinkedListNode(data,
new_node = LinkedListNode(key, data,
links=['next'],
addrs=[None])
if self.size == 1:
Expand Down Expand Up @@ -524,19 +534,19 @@ class SinglyCircularLinkedList(SinglyLinkedList):
>>> from pydatastructs import SinglyCircularLinkedList
>>> scll = SinglyCircularLinkedList()
>>> scll.append(6)
>>> scll[0].data
>>> scll[0].key
6
>>> scll.head.data
>>> scll.head.key
6
>>> scll.append(5)
>>> scll.append_left(2)
>>> str(scll)
'[2, 6, 5]'
>>> scll[0].data = 7.2
>>> scll.extract(1).data
"['2', '6', '5']"
>>> scll[0].key = 7.2
>>> scll.extract(1).key
6
>>> str(scll)
'[7.2, 5]'
"['7.2', '5']"

References
==========
Expand All @@ -545,7 +555,7 @@ class SinglyCircularLinkedList(SinglyLinkedList):

"""

def insert_after(self, prev_node, data):
def insert_after(self, prev_node, key, data=None):
"""
Inserts a new node after the prev_node.

Expand All @@ -559,11 +569,12 @@ def insert_after(self, prev_node, data):
data
Any valid data to be stored in the node.
"""
super(SinglyCircularLinkedList, self).insert_after(prev_node, data)
super(SinglyCircularLinkedList, self).\
insert_after(prev_node, key, data)
if prev_node.next.next == self.head:
self.tail = prev_node.next

def insert_at(self, index, data):
def insert_at(self, index, key, data=None):
"""
Inserts a new node at the input index.

Expand All @@ -576,7 +587,7 @@ def insert_at(self, index, data):
data
Any valid data to be stored in the node.
"""
super(SinglyCircularLinkedList, self).insert_at(index, data)
super(SinglyCircularLinkedList, self).insert_at(index, key, data)
if self.size == 1:
self.head.next = self.head
new_node = self.__getitem__(index)
Expand Down Expand Up @@ -618,27 +629,27 @@ class DoublyCircularLinkedList(DoublyLinkedList):
>>> from pydatastructs import DoublyCircularLinkedList
>>> dcll = DoublyCircularLinkedList()
>>> dcll.append(6)
>>> dcll[0].data
>>> dcll[0].key
6
>>> dcll.head.data
>>> dcll.head.key
6
>>> dcll.append(5)
>>> dcll.append_left(2)
>>> str(dcll)
'[2, 6, 5]'
>>> dcll[0].data = 7.2
>>> dcll.extract(1).data
"['2', '6', '5']"
>>> dcll[0].key = 7.2
>>> dcll.extract(1).key
6
>>> str(dcll)
'[7.2, 5]'
"['7.2', '5']"

References
==========

.. [1] https://en.wikipedia.org/wiki/Doubly_linked_list#Circular_doubly_linked_lists

"""
def insert_after(self, prev_node, data):
def insert_after(self, prev_node, key, data=None):
"""
Inserts a new node after the prev_node.

Expand All @@ -652,11 +663,12 @@ def insert_after(self, prev_node, data):
data
Any valid data to be stored in the node.
"""
super(DoublyCircularLinkedList, self).insert_after(prev_node, data)
super(DoublyCircularLinkedList, self)\
.insert_after(prev_node, key, data)
if prev_node.next.next == self.head:
self.tail = prev_node.next

def insert_before(self, next_node, data):
def insert_before(self, next_node, key, data=None):
"""
Inserts a new node before the next_node.

Expand All @@ -670,11 +682,12 @@ def insert_before(self, next_node, data):
data
Any valid data to be stored in the node.
"""
super(DoublyCircularLinkedList, self).insert_before(next_node,data)
super(DoublyCircularLinkedList, self).\
insert_before(next_node, key, data)
if next_node == self.head:
self.head = next_node.prev

def insert_at(self, index, data):
def insert_at(self, index, key, data=None):
"""
Inserts a new node at the input index.

Expand All @@ -687,7 +700,8 @@ def insert_at(self, index, data):
data
Any valid data to be stored in the node.
"""
super(DoublyCircularLinkedList, self).insert_at(index, data)
super(DoublyCircularLinkedList, self).\
insert_at(index, key, data)
if self.size == 1:
self.head.next = self.head
self.head.prev = self.head
Expand Down
Loading