Beispiel #1
0
 def test_pop(self):
     s = ArrayStack()
     s.push(1)
     self.assertEqual(s.pop(), 1)
     s.push(2)
     s.push(3)
     s.pop()
     self.assertEqual(s.top(), 2)
def test_transfer():
    S = ArrayStack()
    S.push(3)
    S.push(2)
    S.push(1)
    print(S)
    T = ArrayStack()
    transfer(S, T)
    print(T)
def test_stack_clear():
    S = ArrayStack()
    S.push(3)
    S.push(2)
    S.push(1)
    print(S)
    stack_clear(S)
    print(S)
def test_reverse_stack():
    S = ArrayStack()
    S.push(3)
    S.push(2)
    S.push(1)
    print(S)
    reverse_stack(S)
    print(S)
def reverse_list(items):
    s = ArrayStack()
    for item in items:
        s.push(item)
    for i in range(len(items)):
        items[i] = s.pop()
def stack_clear(s: ArrayStack) -> None:
    if s.is_empty():
        return
    s.pop()
    return stack_clear(s)
def test_three_stack():
    R = ArrayStack()
    R.push(1)
    R.push(2)
    R.push(3)

    S = ArrayStack()
    S.push(4)
    S.push(5)

    T = ArrayStack()
    T.push(6)
    T.push(7)
    T.push(8)
    T.push(9)

    three_stack(R, S, T)
    print("R:")
    print(R)
    print("S:")
    print(S)
def three_stack(R: ArrayStack, S: ArrayStack, T: ArrayStack):
    R_len = len(R)
    while not S.is_empty():
        R.push(S.pop())
    while not T.is_empty():
        R.push(T.pop())
    while len(R) > R_len:
        S.push(R.pop())
def transfer(S: ArrayStack, T: ArrayStack) -> None:
    while not S.is_empty():
        T.push(S.pop())
def reverse_stack(S: ArrayStack):
    s1 = ArrayStack()
    s2 = ArrayStack()
    transfer(S, s1)
    transfer(s1, s2)
    transfer(s2, S)