Data structures in Python, Series 1.4: Linked Lists
Merge two sorted linked lists (Part 1 Non Recursive) :
# Firt of all make a node class for every future node to be shaped like .
class node():
def __init__(self,data,nextnode = None):
self.data = data
self.nextnode = nextnode
#now make a class with the all the operations that can be operated in a list
#remember do get a __init__ as this will be used to initiate the default values for the instance in main
class linkedlist():
def __init__(self):
self.head = None
self.data = 0
self.size = 0
def addnode(self,data):
self.data = data
new = node(data,self.head)
self.head = new
self.size +=1
def __print__(self):
cur = self.head
while cur:
print(str(cur.data),end = '>>')
cur = cur.nextnode
print('None')
def merger_two_sorted(lis1,lis2):
a = lis1.head
b = lis2.head
c = linkedlist()
if a == None:
return c
if b == None:
return c
while True:
if a == None:
i =0
else:
i = a.data
if b == None:
j = 0
else:
j = b.data
if i >=j :
c.addnode(int(i))
if a != None:
a = a.nextnode
if j>=i:
c.addnode(int(j))
if b !=None:
b = b.nextnode
if a == None and b == None :
break
return c
if __name__ == '__main__':
lis1 = linkedlist()
for i in range(int(input('enter the number of elements to be inserted'))):
lis1.addnode(int(input('enter the value')))
#lis.deletenodeposition(1)
lis1.__print__()
#print(lis.reccursive_length()
lis2 = linkedlist()
for i in range(int(input('enter the number of elements to be inserted'))):
lis2.addnode(int(input('enter the value')))
lis2.__print__()
c = merger_two_sorted(lis1,lis2)
c.__print__()
A function that takes two lists, each of which is sorted in increasing order,and merges the two together into one list which is in increasing order.
The function should return the new list. The new list should be made by splicing together the nodes of the first two lists.
For example if the first linked list a is 5->10->15 and the other linked list b is 2->3->20, then the function should return a pointer to the head node of the merged list 2->3->5->10->15->20.
There are many cases to deal with: either ‘a’ or ‘b’ may be empty, during processing either ‘a’ or ‘b’ may run out first,and finally there’s the problem of starting the result list empty, and building it up while going through ‘a’ and ‘b’.
# Firt of all make a node class for every future node to be shaped like .
class node():
def __init__(self,data,nextnode = None):
self.data = data
self.nextnode = nextnode
#now make a class with the all the operations that can be operated in a list
#remember do get a __init__ as this will be used to initiate the default values for the instance in main
class linkedlist():
def __init__(self):
self.head = None
self.data = 0
self.size = 0
def addnode(self,data):
self.data = data
new = node(data,self.head)
self.head = new
self.size +=1
def __print__(self):
cur = self.head
while cur:
print(str(cur.data),end = '>>')
cur = cur.nextnode
print('None')
def merger_two_sorted(lis1,lis2):
a = lis1.head
b = lis2.head
c = linkedlist()
if a == None:
return c
if b == None:
return c
while True:
if a == None:
i =0
else:
i = a.data
if b == None:
j = 0
else:
j = b.data
if i >=j :
c.addnode(int(i))
if a != None:
a = a.nextnode
if j>=i:
c.addnode(int(j))
if b !=None:
b = b.nextnode
if a == None and b == None :
break
return c
if __name__ == '__main__':
lis1 = linkedlist()
for i in range(int(input('enter the number of elements to be inserted'))):
lis1.addnode(int(input('enter the value')))
#lis.deletenodeposition(1)
lis1.__print__()
#print(lis.reccursive_length()
lis2 = linkedlist()
for i in range(int(input('enter the number of elements to be inserted'))):
lis2.addnode(int(input('enter the value')))
lis2.__print__()
c = merger_two_sorted(lis1,lis2)
c.__print__()
Comments
Post a Comment