diff --git a/recursion.py b/recursion.py new file mode 100644 index 0000000..bf6129b --- /dev/null +++ b/recursion.py @@ -0,0 +1,131 @@ +##single list +class Node: + def __init__(self,value=None): + self.value=value + self.next=None + + +class SSLinked: + def __init__(self): + self.head=None + self.tail=None + + def __iter__(self): + node=self.head + while node: + yield node + node=node.next + + def insertssl(self,value,location): + newnode=Node(value) + if self.head is None: + self.head=newnode + self.tail=newnode + + else: + #Fist we put the new node reference value as initial head value by using + #newnode.next=self.head + #now we self.head reference to newnode + # + if location==0: + newnode.next=self.head + self.head=newnode +#In the below sentences refer the algorithm as it says 1)-1 means we end up on the last node +#First delete the reference of the last node by newnode.next=None +#now update inittially existing tail reference to the newnode +#now make the new node as the tail + + elif location ==-1: + newnode.next=None + self.tail.next=newnode + self.tail=newnode + + else: + value=0 + temp=self.head + while value