Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create linked_list.py #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions linked_list.py
Original file line number Diff line number Diff line change
@@ -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()