Ejemplo n.º 1
0
    def sort(self):
        r = Stack()
        while not self.isEmpty():
            # insert each element in s in sorted order into r
            tmp = self.pop()
            while not r.isEmpty() and r.peek() > tmp:
                self.push(r.pop())
            r.push(tmp)

        # copy the elements from r back into s
        while not r.isEmpty():
            self.push(r.pop())
Ejemplo n.º 2
0
 def __init__(self):
     self.stack_newest = Stack()
     self.stack_oldest = Stack()
Ejemplo n.º 3
0
class MyQueue:
    def __init__(self):
        self.stack_newest = Stack()
        self.stack_oldest = Stack()
        
    def size(self):
        return self.stack_newest.size() + self.stack_oldest.size()
    
    def add(self, value):
        #push onto stack_newest which always has newest elements on top
        self.stack_newest.push(value)
        
    #move elements from stack_newest into stack_oldest. This is usually done so that
    #we can do operations on stack_oldest
    def shift_stacks(self):
        if(self.stack_oldest.isEmpty()):
            while(not self.stack_newest.isEmpty()):
                self.stack_oldest.push(self.stack_newest.pop())
                
    def peek(self):
        self.shift_stacks() #ensure stack_oldest has the current elements
        return self.stack_oldest.peek() #retrieve the oldest elements
    
    def remove(self):
        self.shift_stacks()#ensure stack_oldest has the current elements
        return self.stack_oldest.pop()  #pop the oldest elements
    
    def __str__(self):  
        out ="Newer: " + str(self.stack_newest.items ) + " "  + "Older: " + str( self.stack_oldest.items )
        
        return out