2018-10-19 12:48:28 +00:00
|
|
|
class Node: # create a Node
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data # given data
|
|
|
|
self.next = None # given next to None
|
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
def __repr__(self): # string representation of a Node
|
|
|
|
return f"Node({self.data})"
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-11-10 08:47:04 +00:00
|
|
|
|
|
|
|
class LinkedList:
|
2018-11-01 15:34:05 +00:00
|
|
|
def __init__(self):
|
2019-11-26 05:45:28 +00:00
|
|
|
self.head = None # initialize head to None
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
def insert_tail(self, data) -> None:
|
2019-10-19 18:14:37 +00:00
|
|
|
if self.head is None:
|
2019-11-26 05:45:28 +00:00
|
|
|
self.insert_head(data) # if this is first node, call insert_head
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2019-10-19 18:14:37 +00:00
|
|
|
temp = self.head
|
2019-11-10 08:47:04 +00:00
|
|
|
while temp.next: # traverse to last node
|
2018-11-01 15:34:05 +00:00
|
|
|
temp = temp.next
|
2019-10-05 05:14:13 +00:00
|
|
|
temp.next = Node(data) # create node & link to tail
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
def insert_head(self, data) -> None:
|
2020-01-14 09:21:02 +00:00
|
|
|
new_node = Node(data) # create a new node
|
2019-11-10 08:47:04 +00:00
|
|
|
if self.head:
|
2020-01-14 09:21:02 +00:00
|
|
|
new_node.next = self.head # link new_node to head
|
|
|
|
self.head = new_node # make NewNode as head
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
def print_list(self) -> None: # print every node data
|
2019-10-19 18:14:37 +00:00
|
|
|
temp = self.head
|
2019-11-10 08:47:04 +00:00
|
|
|
while temp:
|
2019-10-19 18:14:37 +00:00
|
|
|
print(temp.data)
|
|
|
|
temp = temp.next
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2018-11-01 15:34:05 +00:00
|
|
|
def delete_head(self): # delete from head
|
2019-10-19 18:14:37 +00:00
|
|
|
temp = self.head
|
2019-11-10 08:47:04 +00:00
|
|
|
if self.head:
|
2019-10-19 18:14:37 +00:00
|
|
|
self.head = self.head.next
|
2018-11-01 15:34:05 +00:00
|
|
|
temp.next = None
|
|
|
|
return temp
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2018-11-01 15:34:05 +00:00
|
|
|
def delete_tail(self): # delete from tail
|
2019-10-19 18:14:37 +00:00
|
|
|
temp = self.head
|
2019-11-10 08:47:04 +00:00
|
|
|
if self.head:
|
2019-10-19 18:14:37 +00:00
|
|
|
if self.head.next is None: # if head is the only Node in the Linked List
|
|
|
|
self.head = None
|
2018-11-01 15:34:05 +00:00
|
|
|
else:
|
2019-11-10 08:47:04 +00:00
|
|
|
while temp.next.next: # find the 2nd last element
|
2019-10-19 18:14:37 +00:00
|
|
|
temp = temp.next
|
2019-11-26 05:45:28 +00:00
|
|
|
# (2nd last element).next = None and temp = last element
|
|
|
|
temp.next, temp = None, temp.next
|
2019-10-19 18:14:37 +00:00
|
|
|
return temp
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
def is_empty(self) -> bool:
|
|
|
|
return self.head is None # return True if head is none
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2018-11-01 15:34:05 +00:00
|
|
|
def reverse(self):
|
2018-10-19 12:48:28 +00:00
|
|
|
prev = None
|
2019-10-19 18:14:37 +00:00
|
|
|
current = self.head
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
while current:
|
|
|
|
# Store the current node's next node.
|
|
|
|
next_node = current.next
|
|
|
|
# Make the current node's next point backwards
|
|
|
|
current.next = prev
|
|
|
|
# Make the previous node be the current node
|
|
|
|
prev = current
|
|
|
|
# Make the current node the next node (to progress iteration)
|
|
|
|
current = next_node
|
|
|
|
# Return prev in order to put the head at the end
|
2019-10-19 18:14:37 +00:00
|
|
|
self.head = prev
|
2018-11-01 15:34:05 +00:00
|
|
|
|
2019-11-10 08:47:04 +00:00
|
|
|
def __repr__(self): # String representation/visualization of a Linked Lists
|
|
|
|
current = self.head
|
|
|
|
string_repr = ""
|
|
|
|
while current:
|
2019-11-26 05:45:28 +00:00
|
|
|
string_repr += f"{current} --> "
|
2019-11-10 08:47:04 +00:00
|
|
|
current = current.next
|
|
|
|
# END represents end of the LinkedList
|
2019-11-26 05:45:28 +00:00
|
|
|
return string_repr + "END"
|
2019-11-10 08:47:04 +00:00
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
# Indexing Support. Used to get a node at particular position
|
2019-11-10 08:47:04 +00:00
|
|
|
def __getitem__(self, index):
|
|
|
|
current = self.head
|
|
|
|
|
2019-11-26 05:45:28 +00:00
|
|
|
# If LinkedList is empty
|
2019-11-10 08:47:04 +00:00
|
|
|
if current is None:
|
|
|
|
raise IndexError("The Linked List is empty")
|
|
|
|
|
|
|
|
# Move Forward 'index' times
|
|
|
|
for _ in range(index):
|
|
|
|
# If the LinkedList ends before reaching specified node
|
|
|
|
if current.next is None:
|
|
|
|
raise IndexError("Index out of range.")
|
|
|
|
current = current.next
|
|
|
|
return current
|
|
|
|
|
|
|
|
# Used to change the data of a particular node
|
|
|
|
def __setitem__(self, index, data):
|
|
|
|
current = self.head
|
|
|
|
# If list is empty
|
|
|
|
if current is None:
|
|
|
|
raise IndexError("The Linked List is empty")
|
|
|
|
for i in range(index):
|
|
|
|
if current.next is None:
|
|
|
|
raise IndexError("Index out of range.")
|
|
|
|
current = current.next
|
|
|
|
current.data = data
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-11-01 15:34:05 +00:00
|
|
|
def main():
|
2019-11-10 08:47:04 +00:00
|
|
|
A = LinkedList()
|
2019-11-26 05:45:28 +00:00
|
|
|
A.insert_head(input("Inserting 1st at head ").strip())
|
|
|
|
A.insert_head(input("Inserting 2nd at head ").strip())
|
|
|
|
print("\nPrint list:")
|
|
|
|
A.print_list()
|
|
|
|
A.insert_tail(input("\nInserting 1st at tail ").strip())
|
|
|
|
A.insert_tail(input("Inserting 2nd at tail ").strip())
|
|
|
|
print("\nPrint list:")
|
|
|
|
A.print_list()
|
2019-10-19 18:14:37 +00:00
|
|
|
print("\nDelete head")
|
2018-11-01 15:34:05 +00:00
|
|
|
A.delete_head()
|
2019-11-26 05:45:28 +00:00
|
|
|
print("Delete tail")
|
2018-11-01 15:34:05 +00:00
|
|
|
A.delete_tail()
|
2019-11-26 05:45:28 +00:00
|
|
|
print("\nPrint list:")
|
|
|
|
A.print_list()
|
|
|
|
print("\nReverse linked list")
|
2018-11-01 15:34:05 +00:00
|
|
|
A.reverse()
|
2019-11-26 05:45:28 +00:00
|
|
|
print("\nPrint list:")
|
|
|
|
A.print_list()
|
|
|
|
print("\nString representation of linked list:")
|
2019-11-10 08:47:04 +00:00
|
|
|
print(A)
|
2019-11-26 05:45:28 +00:00
|
|
|
print("\nReading/changing Node data using indexing:")
|
2019-11-10 08:47:04 +00:00
|
|
|
print(f"Element at Position 1: {A[1]}")
|
2019-11-26 05:45:28 +00:00
|
|
|
A[1] = input("Enter New Value: ").strip()
|
|
|
|
print("New list:")
|
2019-11-10 08:47:04 +00:00
|
|
|
print(A)
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|