Python/data_structures/linked_list/singly_linked_list.py

105 lines
3.0 KiB
Python
Raw Normal View History

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
class Linked_List:
2018-11-01 15:34:05 +00:00
def __init__(self):
2019-10-19 18:14:37 +00:00
self.head = None # Initialize head to None
2018-11-01 15:34:05 +00:00
def insert_tail(self, data):
2019-10-19 18:14:37 +00:00
if self.head is None:
2019-10-05 05:14:13 +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-10-05 05:14:13 +00:00
while temp.next != None: # 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
2018-11-01 15:34:05 +00:00
def insert_head(self, data):
2019-10-05 05:14:13 +00:00
newNod = Node(data) # create a new node
2019-10-19 18:14:37 +00:00
if self.head != None:
newNod.next = self.head # link newNode to head
self.head = newNod # make NewNode as head
2018-10-19 12:48:28 +00:00
2018-11-01 15:34:05 +00:00
def printList(self): # print every node data
2019-10-19 18:14:37 +00:00
temp = self.head
while temp is not None:
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
if self.head != None:
self.head = self.head.next
2018-11-01 15:34:05 +00:00
temp.next = None
return temp
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
if self.head != None:
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-10-19 18:14:37 +00:00
while temp.next.next is not None: # find the 2nd last element
temp = temp.next
temp.next, temp = (
2019-10-05 05:14:13 +00:00
None,
2019-10-19 18:14:37 +00:00
temp.next,
) # (2nd last element).next = None and temp = last element
return temp
2018-10-19 12:48:28 +00:00
2018-11-01 15:34:05 +00:00
def isEmpty(self):
2019-10-19 18:14:37 +00:00
return self.head is None # Return 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-10-05 05:14:13 +00:00
2018-11-01 15:34:05 +00:00
def main():
A = Linked_List()
2019-10-19 18:14:37 +00:00
print("Inserting 1st at head")
2019-10-05 05:14:13 +00:00
a1 = input()
2019-04-07 15:55:32 +00:00
A.insert_head(a1)
2019-10-19 18:14:37 +00:00
print("Inserting 2nd at head")
2019-10-05 05:14:13 +00:00
a2 = input()
2019-04-07 15:55:32 +00:00
A.insert_head(a2)
2018-11-01 15:34:05 +00:00
print("\nPrint List : ")
A.printList()
2019-04-07 15:55:32 +00:00
print("\nInserting 1st at Tail")
2019-10-05 05:14:13 +00:00
a3 = input()
2019-04-07 15:55:32 +00:00
A.insert_tail(a3)
print("Inserting 2nd at Tail")
2019-10-05 05:14:13 +00:00
a4 = input()
2019-04-07 15:55:32 +00:00
A.insert_tail(a4)
2018-11-01 15:34:05 +00:00
print("\nPrint List : ")
A.printList()
2019-10-19 18:14:37 +00:00
print("\nDelete head")
2018-11-01 15:34:05 +00:00
A.delete_head()
print("Delete Tail")
A.delete_tail()
print("\nPrint List : ")
A.printList()
print("\nReverse Linked List")
A.reverse()
print("\nPrint List : ")
A.printList()
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
main()