Created
February 21, 2017 07:30
-
-
Save pbesra/42a88e0ff5d7e35d1753a31e88f98068 to your computer and use it in GitHub Desktop.
custom Linked List in Python; version : Python 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python 2.7 | |
#Linked List implementation using python | |
class linkedList(object): | |
def __init__(self): | |
self.head=None | |
self.next=None | |
# insert number into linked list (singly linked list) | |
def insert(self, x): | |
temp=node(x) | |
temp.next=self.head | |
self.head=temp | |
# to print data | |
def printData(self): | |
temp=self.head | |
while(temp!=None): | |
print temp.data | |
temp=temp.next | |
# to delete any number in linked list | |
def delete(self, d): | |
temp=self.head | |
found=0 | |
#first number is to deleted | |
if temp.data==d: | |
found=1 | |
self.head=self.head.next | |
# other than first number | |
else: | |
temp1=self.head | |
temp2=self.head | |
while temp!=None: | |
if temp.data==d: | |
found=1 | |
break | |
temp1=temp | |
temp=temp.next | |
if found==1: | |
temp1.next=temp.next | |
if found==0: | |
print 'number not found in linked List' | |
class node(object): | |
def __init__(self, data): | |
self.data=data | |
l=linkedList() | |
l.insert(12) | |
l.insert(1) | |
l.insert(34) | |
l.insert(100) | |
l.insert(102) | |
l.insert(2) | |
l.delete(2) | |
l.printData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment