from time import clock import sys class CreateNode(object): __slots__ = ("Num","next") def __init__(this,Num): this.Num = Num this.next = None class NodeOperation: __slots__ = ("Head","Previous","Last") def __init__(this): this.Head=this.Last= None def WriteList(this): count=0 OutFile=open('out_11_9_1.txt','w') while this.Head.next is not None: OutFile.write(str(this.Head.Num)+",") count+=1 this.Head = this.Head.next OutFile.write(str(this.Head.Num)) count+=1 OutFile.close() return count def InsertNode(this,Num): if this.Head is not None: this.Last.next=CreateNode(Num) this.Last=this.Last.next else: this.Head=this.Last=CreateNode(Num) def InsertNode_at_Position(this,Num,Pos): this.Previous=this.Head if Pos==1 : this.Last=CreateNode(Num) this.Last.next=this.Previous this.Head=this.Last else: count=1 while count != Pos-1 : this.Previous=this.Previous.next count+=1 this.Last=CreateNode(Num) this.Last.next=this.Previous.next this.Previous.next=this.Last def InsertNum_at_Position_by_LinkList(): try: InFile=open('in_9_1.txt','r') start=clock() TotalShiftCount=0 n,m=InFile.readline().split(",") Anum=InFile.readline().split(",") Apos=InFile.readline().split(",") ## START OprationObj=NodeOperation() for Num in Anum: OprationObj.InsertNode(int(Num)) for i in range(1,int(m)+1): Pos=int(Apos[i-1]) OprationObj.InsertNode_at_Position(i,Pos) Asize=OprationObj.WriteList() ## END OutFile=open('out_11_9_1.txt','a') OutFile.write("\nTotal Excution Time : "+str(clock()-start)) OutFile.write("\nSize of Array is : "+str(Asize)) OutFile.write("\nTotal Shiftings are : "+str(TotalShiftCount)) memUsed = sys.getsizeof(InFile) + sys.getsizeof(OutFile)+ sys.getsizeof(start)+ sys.getsizeof(Anum)+ sys.getsizeof(Apos)+sys.getsizeof(Asize) + sys.getsizeof(TotalShiftCount)+ sys.getsizeof(m)+ sys.getsizeof(n)+sys.getsizeof(OprationObj) OutFile.write("\nMemory used : "+str(memUsed)+" Bytes") OutFile.write("\n\nThere are 4 main operatins:\n1. Create Link List of "+str(n)+" nodes" +"\n2. Travarese the list and Find the specified position and Insert new Node" +"\n3. Insert "+str(int(m))+" nodes at Given position" +"\n4. To insert one node 2 link changes required.") InFile.close() OutFile.close() print "Check Your Output File : out_11_9_1.txt is Generated" except ValueError: print "Check Your Output File Cant Not Generated",ValueError InFile.close() OutFile.close() InsertNum_at_Position_by_LinkList()