def kthtoLast(l,k): ## If size if known (non recursive solution)
     n=l.size()
     if(k>n):
         print("invalid k")
         return -1
     
     nl=LinkedList()
     count=1
     current=l.head
     while(count<=n-k+1 and current !=None):
         print(current.getData())
         nl.addNode(current.getData())
         current=current.getNext()
         count=count+1
     
     return nl   
 def _treeLinkList(self,Node,l):
     
     if(len(l)==0):
         ll=LinkedList()
         if(Node.getLeftChild()):
             ll.addNode(Node.getLeftChild())
             lChild=True
         else:
             print("End of Tree:No more left Child")
             lChild=False
             return
         if(Node.getRightChild()):
             ll.addNode(Node.getRightChild())
             rChild=True
         else:    
             print("End of Tree:No more right Child")
             rChild=False
             return
         l.append(ll)
         if(lChild):
             Node=Node.getLeftChild()
             self._treeLinkList(Node, l)
     
     if(len(l)>0):
         ll=LinkedList()
         self.completeTreeLL(l,ll,Node)
         if(Node.getLeftChild()):
             Node=Node.getLeftChild()
             self._treeLinkList(Node, l)
예제 #3
0
def twoNo(l1,l2):
     
     if(l1==None or l1.size()==0):
         return l2
     
     if(l2==None or l2.size()==0):
         return l1
     
     n1=no(l1)
     print("n1 is",n1)
     
     n2=no(l2)
     print("n2 is",n2)
     n=n1+n2
     n=str(n) 
     print("n is",n)
     l=LinkedList()
     for item in n:
         l.addNode(item)
         
     l.show()    
     return l    
     n=l.size()
     if(k>n):
         print("invalid k")
         return -1
     
     nl=LinkedList()
     count=1
     current=l.head
     while(count<=n-k+1 and current !=None):
         print(current.getData())
         nl.addNode(current.getData())
         current=current.getNext()
         count=count+1
     
     return nl   
 
def kthtolastR(l,k): #Using recursion assuming size is unknown
     if(l==None):
         return 0
     i=kthtolastR(l.getNext(), k)+1
     if(i>=k):
         print(l.getData())
     return i    
l=LinkedList()       
#random.sample(range(30),10)
#[28, 9, 26, 19, 13, 1, 22, 29, 5]
for item in [1,2,3,4,5,6,7] :
     l.addNode(item)
l.show()
print("Using Recursion")
nl=kthtolastR(l.head, 2) 
예제 #5
0
from linkList import LinkedList

#Exerc 1
lista = LinkedList()
lista.append(7)
lista.append(8)
lista.append(9)
lista.append(10)
lista.append(11)
print(len(lista))

#Exerc 2
lista2 = LinkedList()
lista2.append(7)
lista2.append(8)
lista2.append(9)
lista2.append(10)
lista2.append(11)
print(lista.compara(lista2))

#Exerc3
print(lista)
print(lista.reverse())

#Exerc4
print(lista.maiorMedia())
예제 #6
0
     n=n1+n2
     n=str(n) 
     print("n is",n)
     l=LinkedList()
     for item in n:
         l.addNode(item)
         
     l.show()    
     return l    
 
def no(l):
     current=l.head
     s=""
     stack=[]
     while(current!=None):
         stack.append(current.getData())
         current=current.getNext()
     
     while(len(stack)!=0):
         s=s+str(stack.pop())    
     
     return int(s)    

l1=LinkedList()
l2=LinkedList()
for item in [6,1,7]:
     l1.addNode(item)
for item in [2,9,5]:
     l2.addNode(item)
     
twoNo(l1, l2)
Q:Implement a function to check if a list is palindrome?
-Size known or unknown?
-Use of stack allowed or not
-Doubly link list or singly
'''

from linkList import LinkedList
from linkList import  Node
def isPalindrome(l):
     fast=l.head
     slow=l.head
     stack=[]
     while(fast!=None and fast.getNext()!=None): #Even Elements
         stack.append(slow.getData())
         slow=slow.getNext()
         fast=fast.getNext().getNext()
    
     if(fast!=None): #odd Elements
         slow=slow.getNext()
     
     while(slow!=None):
         if(slow.getData()!=stack.pop()):
             return  False
         slow=slow.getNext()
     return True        
 
l=LinkedList()       
for item in "123321" :
     l.addNode(item)

print(isPalindrome(l))
from linkList import Node
from linkList import LinkedList

print 'Testing linkList.py'

print 'Initializing linkList1'
linkList1 = LinkedList()

print 'Inserting "Hello" at head'
linkList1.insertHead("Hello")

print 'Size of linkList1:'
print linkList1.size()

print 'Inserting "world!" at head'
linkList1.insertHead("world")

print 'Size of linkList1:'
print linkList1.size()

print 'Search for "Hello"'
try:
    linkList1.search("Hello")
    print 'Data in list'
except:
    print 'Data not in list'
    
print 'Search for "hello"'
try:
    linkList1.search("hello")
    print 'Data in list'