Update singly_linked_list.py (#1593)

* Update singly_linked_list.py

printing current.data rather than node address in __repr__ for a more readable print statement

* eval(repr(c)) == c

The output of `__repr__()` _should look like a valid Python expression that could be used to recreate an object with the same value_.

https://docs.python.org/3.4/reference/datamodel.html#object.__repr__

* += --> +
This commit is contained in:
Vikas Kumar 2019-11-26 11:15:28 +05:30 committed by Christian Clauss
parent 2ad5a1f083
commit 0d3c9d586c

View File

@ -3,30 +3,30 @@ class Node: # create a Node
self.data = data # given data self.data = data # given data
self.next = None # given next to None self.next = None # given next to None
def __repr__(self): # String Representation of a Node def __repr__(self): # string representation of a Node
return f"<Node: {self.data}>" return f"Node({self.data})"
class LinkedList: class LinkedList:
def __init__(self): def __init__(self):
self.head = None # Initialize head to None self.head = None # initialize head to None
def insert_tail(self, data): def insert_tail(self, data) -> None:
if self.head is None: if self.head is None:
self.insert_head(data) # If this is first node, call insert_head self.insert_head(data) # if this is first node, call insert_head
else: else:
temp = self.head temp = self.head
while temp.next: # traverse to last node while temp.next: # traverse to last node
temp = temp.next temp = temp.next
temp.next = Node(data) # create node & link to tail temp.next = Node(data) # create node & link to tail
def insert_head(self, data): def insert_head(self, data) -> None:
newNod = Node(data) # create a new node newNod = Node(data) # create a new node
if self.head: if self.head:
newNod.next = self.head # link newNode to head newNod.next = self.head # link newNode to head
self.head = newNod # make NewNode as head self.head = newNod # make NewNode as head
def printList(self): # print every node data def print_list(self) -> None: # print every node data
temp = self.head temp = self.head
while temp: while temp:
print(temp.data) print(temp.data)
@ -47,14 +47,12 @@ class LinkedList:
else: else:
while temp.next.next: # find the 2nd last element while temp.next.next: # find the 2nd last element
temp = temp.next temp = temp.next
temp.next, temp = ( # (2nd last element).next = None and temp = last element
None, temp.next, temp = None, temp.next
temp.next,
) # (2nd last element).next = None and temp = last element
return temp return temp
def isEmpty(self): def is_empty(self) -> bool:
return self.head is None # Return if head is none return self.head is None # return True if head is none
def reverse(self): def reverse(self):
prev = None prev = None
@ -76,17 +74,16 @@ class LinkedList:
current = self.head current = self.head
string_repr = "" string_repr = ""
while current: while current:
string_repr += f"{current} ---> " string_repr += f"{current} --> "
current = current.next current = current.next
# END represents end of the LinkedList # END represents end of the LinkedList
string_repr += "END" return string_repr + "END"
return string_repr
# Indexing Support. Used to get a node at particaular position # Indexing Support. Used to get a node at particaular position
def __getitem__(self, index): def __getitem__(self, index):
current = self.head current = self.head
# If LinkedList is Empty # If LinkedList is empty
if current is None: if current is None:
raise IndexError("The Linked List is empty") raise IndexError("The Linked List is empty")
@ -113,39 +110,30 @@ class LinkedList:
def main(): def main():
A = LinkedList() A = LinkedList()
print("Inserting 1st at head") A.insert_head(input("Inserting 1st at head ").strip())
a1 = input() A.insert_head(input("Inserting 2nd at head ").strip())
A.insert_head(a1) print("\nPrint list:")
print("Inserting 2nd at head") A.print_list()
a2 = input() A.insert_tail(input("\nInserting 1st at tail ").strip())
A.insert_head(a2) A.insert_tail(input("Inserting 2nd at tail ").strip())
print("\nPrint List : ") print("\nPrint list:")
A.printList() A.print_list()
print("\nInserting 1st at Tail")
a3 = input()
A.insert_tail(a3)
print("Inserting 2nd at Tail")
a4 = input()
A.insert_tail(a4)
print("\nPrint List : ")
A.printList()
print("\nDelete head") print("\nDelete head")
A.delete_head() A.delete_head()
print("Delete Tail") print("Delete tail")
A.delete_tail() A.delete_tail()
print("\nPrint List : ") print("\nPrint list:")
A.printList() A.print_list()
print("\nReverse Linked List") print("\nReverse linked list")
A.reverse() A.reverse()
print("\nPrint List : ") print("\nPrint list:")
A.printList() A.print_list()
print("\nString Representation of Linked List:") print("\nString representation of linked list:")
print(A) print(A)
print("\n Reading/Changing Node Data using Indexing:") print("\nReading/changing Node data using indexing:")
print(f"Element at Position 1: {A[1]}") print(f"Element at Position 1: {A[1]}")
p1 = input("Enter New Value: ") A[1] = input("Enter New Value: ").strip()
A[1] = p1 print("New list:")
print("New List:")
print(A) print(A)