-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Adds operations for circular linked list #1584
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
Merged
cclauss
merged 11 commits into
TheAlgorithms:master
from
onlinejudge95:data-structures/circular-linked-list
Nov 19, 2019
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cfa71dd
Adds, append, len, print operations for circular linked list
onlinejudge95 b6ffc18
Adds, prepend support
onlinejudge95 67b2c64
Adds, delete from front of the list
onlinejudge95 296dba0
Adds, delete_rear support
onlinejudge95 53d6ba9
Adds, method documentations
onlinejudge95 cfd7a9d
Adds, type checking and doctests
onlinejudge95 906670e
Updates doctest for delete ops
onlinejudge95 b209021
Addressing requested changes
onlinejudge95 90b8463
Removes unused import
onlinejudge95 ee2c0e3
Fixes failing doctests
onlinejudge95 33e8152
Minor modifications...
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
import typing | ||
|
||
|
||
class Node: | ||
""" | ||
Class to represent a single node. | ||
|
||
Each node has following attributes | ||
* data | ||
* next_ptr | ||
""" | ||
|
||
def __init__(self, data: typing.Any): | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.data = data | ||
self.next_ptr = None | ||
|
||
def set_data(self, value: typing.Any): | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Set the data field of the node to given value. | ||
>>> node = Node(1) | ||
>>> node.set_data(2) | ||
>>> node.get_data() | ||
2 | ||
""" | ||
self.data = value | ||
|
||
def get_data(self) -> typing.Any: | ||
""" | ||
Returns the data field of the current node. | ||
>>> node = Node(1) | ||
>>> node.get_data() | ||
1 | ||
""" | ||
return self.data | ||
|
||
def set_next(self, value: typing.Type['Node']): | ||
""" | ||
Sets the next pointer of the current node. | ||
>>> node, node1 = Node(1), Node(2) | ||
>>> node.set_next(node1) | ||
>>> node.get_next().get_data() | ||
2 | ||
""" | ||
self.next_ptr = value | ||
|
||
def get_next(self) -> typing.Type['Node']: | ||
""" | ||
Returns the next pointer of the current node. | ||
>>> node = Node(1) | ||
>>> node.get_next() is None | ||
True | ||
""" | ||
return self.next_ptr | ||
|
||
def has_next(self) -> bool: | ||
""" | ||
Checks if the current node has a valid next node. | ||
>>> node = Node(1) | ||
>>> node.has_next() | ||
False | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return self.next_ptr is not None | ||
|
||
|
||
class CircularLinkedList: | ||
""" | ||
Class to represent the CircularLinkedList. | ||
|
||
CircularLinkedList has following attributes. | ||
* HEAD | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* length | ||
""" | ||
|
||
def __init__(self): | ||
self.head = None | ||
self.length = 0 | ||
|
||
def __len__(self) -> int: | ||
""" | ||
Dunder method to return length of the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> len(cll) | ||
0 | ||
>>> cll.append(1) | ||
>>> len(cll) | ||
1 | ||
""" | ||
return self.length | ||
|
||
def __str__(self) -> str: | ||
""" | ||
Dunder method to represent the string representation of the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> print(cll) | ||
Empty linked list | ||
>>> cll.append(1) | ||
>>> cll.append(2) | ||
>>> print(cll) | ||
<Node data=1> => <Node data=2> | ||
""" | ||
current_node = self.head | ||
if not current_node: | ||
return "Empty linked list" | ||
|
||
result = [f"<Node data={current_node.get_data()}>"] | ||
current_node = current_node.get_next() | ||
|
||
while current_node != self.head: | ||
result.append(f"<Node data={current_node.get_data()}>") | ||
current_node = current_node.get_next() | ||
|
||
return " => ".join(result) | ||
|
||
def append(self, data: typing.Any): | ||
""" | ||
Adds a node with given data to the end of the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> cll.append(1) | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> cll.append(2) | ||
>>> len(cll) | ||
2 | ||
>>> print(cll) | ||
<Node data=1> => <Node data=2> | ||
""" | ||
current_node = self.head | ||
|
||
new_node = Node(data) | ||
new_node.set_next(new_node) | ||
|
||
if current_node is None: | ||
self.head = new_node | ||
else: | ||
while current_node.get_next() != self.head: | ||
current_node = current_node.get_next() | ||
|
||
current_node.set_next(new_node) | ||
new_node.set_next(self.head) | ||
|
||
self.length += 1 | ||
|
||
def prepend(self, data: typing.Any): | ||
""" | ||
Adds a ndoe with given data to the front of the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> cll.prepend(1) | ||
>>> cll.prepend(2) | ||
>>> len(cll) | ||
2 | ||
>>> print(cll) | ||
<Node data=2> => <Node data=1> | ||
""" | ||
current_node = self.head | ||
|
||
new_node = Node(data) | ||
new_node.set_next(new_node) | ||
|
||
if current_node is None: | ||
self.head = new_node | ||
else: | ||
while current_node.get_next() != self.head: | ||
current_node = current_node.get_next() | ||
|
||
current_node.set_next(new_node) | ||
new_node.set_next(self.head) | ||
|
||
self.head = new_node | ||
|
||
self.length += 1 | ||
|
||
def delete_front(self): | ||
""" | ||
Removes the 1st node from the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> cll.append(1) | ||
>>> cll.append(2) | ||
>>> print(cll) | ||
<Node data=1> => <Node data=2> | ||
>>> cll.delete_front() | ||
>>> print(cll) | ||
<Node data=2> | ||
""" | ||
if self.head is None: | ||
raise IndexError() | ||
|
||
current_node = self.head | ||
|
||
if current_node.get_next() == current_node: | ||
self.head, self.length = None, 0 | ||
else: | ||
while current_node.get_next() != self.head: | ||
current_node = current_node.get_next() | ||
|
||
current_node.set_next(self.head.get_next()) | ||
self.head = self.head.get_next() | ||
|
||
self.length -= 1 | ||
|
||
def delete_rear(self): | ||
""" | ||
Removes the last node from the CircularLinkedList | ||
>>> cll = CircularLinkedList() | ||
>>> cll.append(1) | ||
>>> cll.append(2) | ||
>>> print(cll) | ||
<Node data=1> => <Node data=2> | ||
>>> cll.delete_rear() | ||
>>> print(cll) | ||
<Node data=1> | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if self.head is None: | ||
raise IndexError() | ||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
temp_node, current_node = self.head, self.head | ||
|
||
if current_node.get_next() == current_node: | ||
self.head, self.length = None, 0 | ||
else: | ||
while current_node.get_next() != self.head: | ||
temp_node = current_node | ||
current_node = current_node.get_next() | ||
|
||
temp_node.set_next(current_node.get_next()) | ||
|
||
self.length -= 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.