-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd4376
commit c48f277
Showing
4 changed files
with
165 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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,67 @@ | ||
class Node: | ||
def __init__(self, val): | ||
self.val = val | ||
self.next = None | ||
self.prev = None | ||
|
||
|
||
class DoublyLinkedList: | ||
def __init__(self): | ||
self.main = None | ||
|
||
def append_(self, val): | ||
if self.main is None: | ||
self.main = Node(val) | ||
return | ||
temp = self.main | ||
while temp is not None: | ||
if temp.next is None: | ||
temp.next = Node(val) | ||
temp.next.prev = temp | ||
return | ||
else: | ||
temp = temp.next | ||
|
||
def print_(self): | ||
temp = None | ||
if self.main is None: | ||
print("Doubly Linked List is Empty") | ||
return | ||
else: | ||
temp = self.main | ||
while temp is not None: | ||
print("_" * 12) | ||
print("|" + f"{temp.val}".center(10) + "|") | ||
print("־" * 12) | ||
print(" ʌ".center(12)) | ||
print("| |".center(12)) | ||
print("V ".center(12)) | ||
temp = temp.next | ||
|
||
def delete_(self, value): | ||
if self.main.val == value: | ||
self.main = self.main.next | ||
temp = self.main | ||
if temp is not None: | ||
while temp is not None: | ||
if temp.val == value: | ||
temp.prev.next = temp.next | ||
return | ||
else: | ||
temp = temp.next | ||
|
||
def prepend_(self, val): | ||
self.main.prev = Node(val) | ||
self.main.prev.next = self.main | ||
self.main = self.main.prev | ||
|
||
|
||
if __name__ == "__main__": | ||
dll = DoublyLinkedList() | ||
dll.append_(1) | ||
dll.append_(2) | ||
dll.append_(2) | ||
dll.append_(3) | ||
dll.prepend_(0) | ||
dll.delete_(2) | ||
dll.print_() |
This file contains 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,67 @@ | ||
class Node: | ||
def __init__(self, val): | ||
self.val = val | ||
self.next = None | ||
|
||
|
||
class SinglyLinkedList: | ||
def __init__(self): | ||
self.main = None | ||
|
||
def append_(self, val): | ||
temp = None | ||
if self.main is None: | ||
self.main = Node(val) | ||
return | ||
else: | ||
temp = self.main | ||
while temp.next is not None: | ||
temp = temp.next | ||
temp.next = Node(val) | ||
|
||
def print_(self): | ||
temp = None | ||
if self.main is None: | ||
print("Singly Linked List is Empty") | ||
return | ||
else: | ||
temp = self.main | ||
while temp is not None: | ||
print("_" * 12) | ||
print("|" + f"{temp.val}".center(10) + "|") | ||
print("־" * 12) | ||
print("|".center(12)) | ||
print("V".center(12)) | ||
temp = temp.next | ||
|
||
def prepend_(self, val): | ||
temp = self.main | ||
self.main = Node(val) | ||
self.main.next = temp | ||
|
||
def delete_(self, value): | ||
""" | ||
Give the value in the list to be deleted from the list. | ||
""" | ||
temp = self.main | ||
if temp is None: | ||
print("No values in the List!") | ||
return | ||
while temp is not None: | ||
if temp.next is not None: | ||
if temp.next.val == value: | ||
temp.next = temp.next.next | ||
return | ||
else: | ||
temp = temp.next | ||
|
||
|
||
if __name__ == "__main__": | ||
ll = SinglyLinkedList() | ||
ll.append_(2) | ||
ll.append_(2) | ||
ll.prepend_(1) | ||
ll.delete_(2) | ||
ll.append_(3) | ||
ll.append_(4) | ||
ll.print_() |
This file contains 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
This file contains 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