Skip to content

Commit 0d3c9d5

Browse files
vikasit12cclauss
authored andcommitted
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__ * += --> +
1 parent 2ad5a1f commit 0d3c9d5

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

Diff for: data_structures/linked_list/singly_linked_list.py

+32-44
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ def __init__(self, data):
33
self.data = data # given data
44
self.next = None # given next to None
55

6-
def __repr__(self): # String Representation of a Node
7-
return f"<Node: {self.data}>"
6+
def __repr__(self): # string representation of a Node
7+
return f"Node({self.data})"
88

99

1010
class LinkedList:
1111
def __init__(self):
12-
self.head = None # Initialize head to None
12+
self.head = None # initialize head to None
1313

14-
def insert_tail(self, data):
14+
def insert_tail(self, data) -> None:
1515
if self.head is None:
16-
self.insert_head(data) # If this is first node, call insert_head
16+
self.insert_head(data) # if this is first node, call insert_head
1717
else:
1818
temp = self.head
1919
while temp.next: # traverse to last node
2020
temp = temp.next
2121
temp.next = Node(data) # create node & link to tail
2222

23-
def insert_head(self, data):
23+
def insert_head(self, data) -> None:
2424
newNod = Node(data) # create a new node
2525
if self.head:
2626
newNod.next = self.head # link newNode to head
2727
self.head = newNod # make NewNode as head
2828

29-
def printList(self): # print every node data
29+
def print_list(self) -> None: # print every node data
3030
temp = self.head
3131
while temp:
3232
print(temp.data)
@@ -47,14 +47,12 @@ def delete_tail(self): # delete from tail
4747
else:
4848
while temp.next.next: # find the 2nd last element
4949
temp = temp.next
50-
temp.next, temp = (
51-
None,
52-
temp.next,
53-
) # (2nd last element).next = None and temp = last element
50+
# (2nd last element).next = None and temp = last element
51+
temp.next, temp = None, temp.next
5452
return temp
5553

56-
def isEmpty(self):
57-
return self.head is None # Return if head is none
54+
def is_empty(self) -> bool:
55+
return self.head is None # return True if head is none
5856

5957
def reverse(self):
6058
prev = None
@@ -76,17 +74,16 @@ def __repr__(self): # String representation/visualization of a Linked Lists
7674
current = self.head
7775
string_repr = ""
7876
while current:
79-
string_repr += f"{current} ---> "
77+
string_repr += f"{current} --> "
8078
current = current.next
8179
# END represents end of the LinkedList
82-
string_repr += "END"
83-
return string_repr
80+
return string_repr + "END"
8481

8582
# Indexing Support. Used to get a node at particaular position
8683
def __getitem__(self, index):
8784
current = self.head
8885

89-
# If LinkedList is Empty
86+
# If LinkedList is empty
9087
if current is None:
9188
raise IndexError("The Linked List is empty")
9289

@@ -113,39 +110,30 @@ def __setitem__(self, index, data):
113110

114111
def main():
115112
A = LinkedList()
116-
print("Inserting 1st at head")
117-
a1 = input()
118-
A.insert_head(a1)
119-
print("Inserting 2nd at head")
120-
a2 = input()
121-
A.insert_head(a2)
122-
print("\nPrint List : ")
123-
A.printList()
124-
print("\nInserting 1st at Tail")
125-
a3 = input()
126-
A.insert_tail(a3)
127-
print("Inserting 2nd at Tail")
128-
a4 = input()
129-
A.insert_tail(a4)
130-
print("\nPrint List : ")
131-
A.printList()
113+
A.insert_head(input("Inserting 1st at head ").strip())
114+
A.insert_head(input("Inserting 2nd at head ").strip())
115+
print("\nPrint list:")
116+
A.print_list()
117+
A.insert_tail(input("\nInserting 1st at tail ").strip())
118+
A.insert_tail(input("Inserting 2nd at tail ").strip())
119+
print("\nPrint list:")
120+
A.print_list()
132121
print("\nDelete head")
133122
A.delete_head()
134-
print("Delete Tail")
123+
print("Delete tail")
135124
A.delete_tail()
136-
print("\nPrint List : ")
137-
A.printList()
138-
print("\nReverse Linked List")
125+
print("\nPrint list:")
126+
A.print_list()
127+
print("\nReverse linked list")
139128
A.reverse()
140-
print("\nPrint List : ")
141-
A.printList()
142-
print("\nString Representation of Linked List:")
129+
print("\nPrint list:")
130+
A.print_list()
131+
print("\nString representation of linked list:")
143132
print(A)
144-
print("\n Reading/Changing Node Data using Indexing:")
133+
print("\nReading/changing Node data using indexing:")
145134
print(f"Element at Position 1: {A[1]}")
146-
p1 = input("Enter New Value: ")
147-
A[1] = p1
148-
print("New List:")
135+
A[1] = input("Enter New Value: ").strip()
136+
print("New list:")
149137
print(A)
150138

151139

0 commit comments

Comments
 (0)