class PseudoQueue:
    """
    Requires a stack of nodes as its only parameter.
    Creates an instance of a "PseudoQueue".
    Initialized with two stacks.
    """
    def __init__(self, stack_a):
        self.stack_a = stack_a
        self.stack_b = Stack()

    def enqueue(self, value):
        """
        Takes a value as an arguement.
        Creates an instance of a node with its value being the value given as an arguement.
        Adds the new node to the "end" of the PseudoQueue. Last in line. Top of stack_a.
        """
        new_node = Node(value)
        new_node.next = self.stack_a.top
        self.stack_a.top = new_node

    def dequeue(self):
        """
        Has no parameters.
        Removes the node from the "front" of the PseudoQueue. First in line. The bottom of stack_a.
        """
        while self.stack_a.top:
            value = self.stack_a.pop()
            # new_node = Node(value)
            self.stack_b.push(value)
        result = self.stack_b.pop()
        while self.stack_b.top:
            value = self.stack_b.pop()
            # new_node = Node(value)
            self.stack_a.push(value)
        return result
示例#2
0
class PseudoQueue:
    def __init__(self, stack_a):
        self.stack_a = stack_a
        self.stack_b = Stack()

    def enqueue(self, value):
        new_node = Node(value)
        new_node.next = self.stack_a.top
        self.stack_a.top = new_node

    def dequeue(self):
        while self.stack_a.top:
            value = self.stack_a.pop()
            # new_node = Node(value)
            self.stack_b.push(value)
        result = self.stack_b.pop()
        while self.stack_b.top:
            value = self.stack_b.pop()
            # new_node = Node(value)
            self.stack_a.push(value)
        return result
def stack_of_four_nodes():
    new_stack = Stack()
    new_stack.push(1)
    new_stack.push(2)
    new_stack.push(3)
    new_stack.push(4)
    return new_stack
def empty_stack():
    new_stack = Stack()
    return new_stack
def stack_of_one():
    new_stack = Stack()
    new_stack.push('one')
    return new_stack
示例#6
0
 def __init__(self, stack_a):
     self.stack_a = stack_a
     self.stack_b = Stack()
示例#7
0
    def __init__(self, stack_a):
        self.stack_a = stack_a
        self.stack_b = Stack()

    def enqueue(self, value):
        new_node = Node(value)
        new_node.next = self.stack_a.top
        self.stack_a.top = new_node

    def dequeue(self):
        while self.stack_a.top:
            value = self.stack_a.pop()
            # new_node = Node(value)
            self.stack_b.push(value)
        result = self.stack_b.pop()
        while self.stack_b.top:
            value = self.stack_b.pop()
            # new_node = Node(value)
            self.stack_a.push(value)
        return result


if __name__ == "__main__":
    stack_a = Stack()
    stack_a.push(20)
    stack_a.push(15)
    stack_a.push(10)
    queue = PsudoQueue(stack_a)
    a = queue.dequeue()
    print(a.value)