From 7b04c7fe7fc653884e7e2246d682ecd74deeaccc Mon Sep 17 00:00:00 2001 From: Ayush Mishra <110256959+002ayush@users.noreply.github.com> Date: Wed, 26 Oct 2022 02:49:59 +0530 Subject: [PATCH] Create linked_list.py --- linked_list.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 linked_list.py diff --git a/linked_list.py b/linked_list.py new file mode 100644 index 0000000..cf5a661 --- /dev/null +++ b/linked_list.py @@ -0,0 +1,51 @@ +class Node: + def __init__(self,data): + self.data = data + self.ref = None +class LinkedList: + def __init__(self): + self.head = None + def making_list(self,data): + if self.head is None: + new_node = Node(data) + self.head = new_node + + def add_begin(self,data): + new_node = Node(data) + new_node.ref = self.head + self.head = new_node + def add_end(self,data): + if self.head is None: + print("Linked list is empty") + else: + n = self.head + while n.ref is not None: + n = n.ref + new_node = Node(data) + n.ref = new_node + + + + def printList(self): + if self.head is None: + print("Linked List is empty") + else: + n = self.head + while n is not None: + print(n.data) + n = n.ref +l1 = LinkedList() +n1 = int(input("Enter a number:")) +l1.making_list(n1) +n2 = int(input("Enter a number:")) +l1.add_begin(n2) +n3 = int(input("Enter a number:")) +l1.add_begin(n3) +n4 = int(input("Enter a number:")) +l1.add_begin(n4) +n5 = int(input("Enter a number:")) +l1.add_end(n5) +n6 = int(input("Enter a number:")) +l1.add_end(n6) +l1.printList() +