import sys from time import clock class createNode(): #This class will create a new node. def __init__(self, val): self.val = val self.next = None class Node: def __init__(self): self.first = self.last = None def add(self, val): if self.first is None: self.first = self.last = createNode(val) else: self.last.next = createNode(val) self.last = self.last.next def show(self): f1=open("out_34_sec9_1.txt","w") write=f1.write data = self.first write("%d" %data.val) while data.next is not None: data = data.next write(",%d" %data.val) f1.close() def insert(self,n,k): if n==1: x=createNode(k) x.next=self.first self.first=x else: data=self.first c=1 while data is not None: if c==n-1: # n-1 because previous node's pointer is required. x=createNode(k) x.next=data.next data.next=x break else: data=data.next c+=1 def main(): start=clock() try: f=open("9_1.txt","r") temp=[] pos=[] #This will contain the data. a=[] #This will contain the positions for insertion s=[] countins=0 #It will count the insertions required to fill the list with default values. countinsa=0 #It will count the operations performed on the list. appendtemp= temp.append #In python, local variables are easier to access than global ones. If temp.append() is invoked multiple times, it will call the append function from the library everytime. #Therefore, using local variable, append() will be call from library just once and later using the local variable.""" appends= s.append appenda= a.append appendpos= pos.append for x in f.readlines(): appendtemp(x) f.close() for u in temp[0].split(','): u=u.strip() appends(u) for u in temp[1].split(','): u=u.strip() appenda(u) for u in temp[2].split(','): u=u.strip() appendpos(u) m=int(s[0]) n=int(s[1]) list1 = Node() for i in a: list1.add(int(i)) countins+=1 j=1 for k in pos: list1.insert(int(k),j) countinsa+=1 j+=1 list1.show() end=clock() f1=open("out_34_sec9_1.txt","a") write=f1.write write("\nThe number of operations required to fill the list with initial values are : %d" %countins) write("\nThe insertion operations performed on the list are: %d" %countinsa) write("\nThe size of the list will be : %d " %(m+n)) write("\nTime taken by the program is : %f seconds" %(end-start)) mem=sys.getsizeof(f)+sys.getsizeof(f1)+sys.getsizeof(temp)+sys.getsizeof(pos)+sys.getsizeof(write)+sys.getsizeof(a)+sys.getsizeof(s)+sys.getsizeof(appendtemp)+sys.getsizeof(appends)+sys.getsizeof(appendpos)+sys.getsizeof(appenda)+sys.getsizeof(x)+sys.getsizeof(u)+sys.getsizeof(m)+sys.getsizeof(n)+sys.getsizeof(i)+sys.getsizeof(j)+sys.getsizeof(list1)+sys.getsizeof(k) write("\nMemory used by the program is : %d bytes." %mem) f1.close() except IOError: print "Input file not found in the directory." main()