Exemplo n.º 1
0
class MyQueue(object):
    def __init__(self):

        self.start = Stack()
        self.end = Stack()

    def push(self, data):
        #push data so that first in is first out from the stacks
        self.start.push(data)

    def reverseStack(self):
        node = self.start.top
        while node:
            data = node.data
            self.end.push(data)
            node = node.next

    def create_Queue(self):
        self.end = Stack()
        self.reverseStack()

    def dump_queue(self):
        node = self.end.top
        while node:
            print(node)
            node = node.next

    def dump_stack(self):
        node = self.start.top
        while node:
            print(node)
            node = node.next
Exemplo n.º 2
0
def test2():
    astack = Stack()
    for i in range(10):
        ranX = randint(0, 100)
        astack.push(ranX)
    #astack.dump()
    newstack = sortStack(astack)
    newstack.dump()
    print(newstack.top)
Exemplo n.º 3
0
 def push(self, data):
     current = self.stack[self.currentStack]
     length = current.size
     if length < self.max:
         current.push(data)
     else:
         newStack = Stack()
         newStack.push(data)
         self.stack.append(newStack)
         self.currentStack += 1
Exemplo n.º 4
0
def sortStack(stack):

    newStack = Stack()

    while (not stack.isEmpty()):
        # remove the last item place it in tmp
        tmp = stack.pop()
        less_than = True  #greatear_than if you flip the gator jaws
        while (not newStack.isEmpty() and less_than):
            x = newStack.peek()
            if x < tmp:  # flipping the gator jaws will sort the list in the reverse order.
                lesser = newStack.pop()
                stack.push(lesser)
            else:
                #stop here, the next value is greater_than the tmp value
                #we want the tmp value pushed to the newStack at this point
                less_than = False
        # The values that were pushed back into the stack are in order in comparison to the newly appended tmp.
        # The outer loop will quickly loop through and append them to the newStack, as they are, in order, less_than the next popped() value
        # Once it gets to the tmp value that is greater than the last item in the newStack, it will remove all of them again,
        # pushing them onto the original stack, in order, and then appending the larger tmp value to our newStack, and the process loops.
        newStack.push(tmp)

    return newStack
Exemplo n.º 5
0
 def _populate(self):
     newarray = []
     for i in range(self.numstacks):
         newstack = Stack()
         newarray.append(newstack)
     return newarray
Exemplo n.º 6
0
 def __init__(self):
     self.stack = [Stack()]
     self.max = 10
     self.currentStack = 0
Exemplo n.º 7
0
    def __init__(self):

        self.start = Stack()
        self.end = Stack()
Exemplo n.º 8
0
 def create_Queue(self):
     self.end = Stack()
     self.reverseStack()