Example #1
0
 def printTower(self, towerId):
     tempStack = Stack()
     while self.tower[towerId].length > 0:
         topNode = self.tower[towerId].pop()
         print(f"{topNode}")
         tempStack.push(topNode)
     while tempStack.length > 0:
         self.tower[towerId].push(tempStack.pop())
Example #2
0
 def test_pop(self):
     stack = Stack()
     to_push = [-5.0, 0, "five", True]
     for item in to_push:
         stack.push(item)
     self.assertEqual(len(stack), len(to_push))
     for i in range(len(to_push)):
         self.assertEqual(stack.pop(), to_push[len(to_push) - 1 - i])
     self.assertEqual(len(stack), 0)
Example #3
0
 def test_push(self):
     stack = Stack()
     to_push = 33
     stack.push(to_push)
     self.assertEqual(len(stack), 1)
     self.assertEqual(stack.peek(), to_push)
     to_push = "thirty three"
     stack.push(to_push)
     self.assertEqual(len(stack), 2)
     self.assertEqual(stack.peek(), to_push)
Example #4
0
 def by_stack(self, header):
     """
     首先肯定要遍历链表,此时我们肯定要遍历链表,但是输出确实逆序的,因而我们可利用栈来实现这一功能。
     """
     s = Stack()
     while header is not None:
         s.push(header.val)
         header = header.next
     while not s.is_empty():
         print(s.pop())
Example #5
0
 def test_init(self):
     stack = Stack()
     self.assertEqual(len(stack), 0)
Example #6
0
def sort_stack(s):
    sort = Stack()
    while not s.is_empty():
        top_node = s.pop()
        if sort.is_empty() or sort.peek().val >= top_node.val:
            sort.push(top_node.val)

        else:
            while (not sort.is_empty()) and sort.peek().val < top_node.val:
                n = sort.pop()
                s.push(n.val)
            sort.push(top_node.val)
    s.head = sort.head
    s.tail = sort.tail
Example #7
0
def sortStack(stackSize=10):
    s = Stack()

    print("Before sort")
    for _ in range(stackSize):
        data = random.randint(1, stackSize * 10)
        print(data, end=', ')
        s.push(data)

    r = Stack()
    while not s.isEmpty():
        data = s.pop()
        while (not r.isEmpty() and data > r.peek()):
            s.push(r.pop())
        r.push(data)

    print("\nPost sort")
    while not r.isEmpty():
        print(r.pop(), end=', ')
Example #8
0
 def __init__(self):
     self.s1 = Stack()
     self.s2 = Stack()
Example #9
0
class MyQueue(object):
    def __init__(self):
        self.s1 = Stack()
        self.s2 = Stack()

    def enqueue(self, data):
        self.s1.push(data)

    def dequeue(self):
        if self.s2.length > 0:
            return self.s2.pop()
        while self.s1.length > 0:
            self.s2.push(self.s1.pop())
        return self.s2.pop()

    def peek(self):
        if self.s2.peek() is None:
            while self.s1.length > 0:
                self.s2.push(self.s1.pop())
        return self.s2.peek()

    def getLength(self):
        return self.s1.length + self.s2.length
Example #10
0
 def __init__(self, discCount=3):
     self.discs = discCount
     self.tower = [Stack(), Stack(), Stack()]
     for i in range(self.discs, 0, -1):
         self.tower[0].push(i)
Example #11
0
def towerOfHanoi(discCount=2):
    a = Stack()
    b = Stack()
    c = Stack()

    for i in range(discCount, 0, -1):
        a.push(i)

    print("Tower A at start")
    for i in range(1, discCount + 1):
        print(i)

    minMoves = 2**discCount - 1
    for i in range(minMoves):
        if i % 3 == 0:
            if c.length > 0 and a.length > 0:
                if a.peek() < c.peek():
                    c.push(a.pop())
                else:
                    a.push(c.pop())
            elif c.length > 0:
                a.push(c.pop())
            elif a.length > 0:
                c.push(a.pop())
        elif i % 3 == 1:
            if a.length > 0 and b.length > 0:
                if a.peek() < b.peek():
                    b.push(a.pop())
                else:
                    a.push(b.pop())
            elif a.length > 0:
                b.push(a.pop())
            elif b.length > 0:
                a.push(b.pop())
        elif i % 3 == 2:
            if b.length > 0 and c.length > 0:
                if b.peek() < c.peek():
                    c.push(b.pop())
                else:
                    b.push(c.pop())
            elif b.length > 0:
                c.push(b.pop())
            elif c.length > 0:
                b.push(c.pop())

    if discCount % 2 == 0:
        b, c = c, b

    if a.length > 0:
        print("Tower A at end")
        while a.length > 0:
            print(a.pop())

    if b.length > 0:
        print("Tower B at end")
        while b.length > 0:
            print(b.pop())

    if c.length > 0:
        print("Tower C at end")
        while c.length > 0:
            print(c.pop())
    return