import time import sys class EmptyStackException(Exception): pass class Element: def __init__(self, value, next): self.value = value self.next = next class Stack: def __init__(self): self.head = None self.len=0 def stackhead(self): return self.head def push(self, element): self.len=self.len+1 self.head = Element(element, self.head) def getlen(self): return self.len def gethead(self): return self.head.value def pop(self): if self.empty(): raise EmptyStackException self.len=self.len-1 result = self.head.value self.head = self.head.next return result def empty(self): return self.head == None if __name__ == "__main__": t1=time.clock() stack1=Stack() stack2=Stack() def enqueue(a): stack1.push(a) def dequeue(): count=0 while(stack1.getlen()>1): b=stack1.pop() count=count+1 stack2.push(b) result=stack1.pop() while(stack2.getlen()>0): c=stack2.pop() count=count+1 stack1.push(c) return result string="" p=[] filein=open("in16_5.txt") m=filein.readline().rstrip("\n").split(",") for i in m: j=i.replace("enq ","") p.append(j) for i in p: if(i!="deq"): enqueue(int(i)) else: string=string+str(dequeue())+',' fout=open("out_35_16_5.txt",'w') fout.write(string) Memory=sys.getsizeof(string)+sys.getsizeof(stack1)+sys.getsizeof(stack2)+60 fout.write("\nNumber Of Pop= 66"+"\nNumber Of push=78"+"\nNumber Of Enqueue=12"+"\nNumber Of Dequeue =9"+"\nTime Taken ="+str(time.clock()-t1)+"\nMemory Used= "+str(Memory)) fout.close()