【python】棧

一端操作,先進後出,後進先出

用列表實現棧:

代碼示例:

#!/usr/bin/env python
# encoding: utf-8

# TODO: 棧, 一端操作,後進先出,先進後出
class Stack(object):
    def __init__(self):
        self.stack = []

    def __repr__(self):
        return repr(self.stack)

    def __len__(self):
        return len(self.stack)

    def put(self, item):
        self.stack.append(item)

    def pop(self):
        return self.stack.pop()

    def clear(self):
        self.stack = []

    def top(self):
        return self.stack[-1]

    def isEmpty(self):
        return bool(self.stack)


if __name__ == '__main__':
    s = Stack()
    print(s.isEmpty())
    s.put(0)
    s.put(1)
    s.put(2)
    print(s)
    print(len(s))
    s.pop()
    print(s)
    s.pop()
    print(s)
    print(s.isEmpty())
輸出結果爲:
False
[0, 1, 2]
3
[0, 1]
[0]
True

用鏈表實現棧:

代碼示例:

#!/usr/bin/env python
# encoding: utf-8


class Node(object):
    def __init__(self, item):
        self.item = item
        self.next = None


class Stack(object):
    def __init__(self):
        '''
        :param head: 頭部,進出端
        :param tail: 尾部,進入的第一個元素
        :param length: 長度
        :return: 
        '''
        self.head = None
        self.tail = None
        self.length = 0

    def __repr__(self):
        stack = []
        if not self.isEmpty():
            curNode = self.head
            while curNode:
                stack.append(curNode.item)
                curNode = curNode.next
        return repr(stack)

    def __len__(self):
        return self.length

    def put(self, item):
        newNode = Node(item)
        if self.isEmpty():
            self.head = newNode
            self.tail = newNode
        else:
            newNode.next = self.head
            self.head = newNode
        self.length += 1

    def pop(self):
        if self.isEmpty():
            raise IndexError("Stack is empty!")
        item = self.head.item
        self.head = self.head.next
        self.length -= 1
        if self.isEmpty():
            self.tail = None
        return item

    def clear(self):
        self.head = None
        self.tail = None

    def top(self):
        return self.head

    def isEmpty(self):
        return bool(self.head is None)


if __name__ == '__main__':
    s = Stack()
    print(s.isEmpty())
    s.put(1)
    s.put(2)
    s.put(3)
    s.put(4)
    print(s)
    print('len=', len(s))
    print('head=', s.head.item)
    print('tail=', s.tail.item)
    print(s.pop())
    print(s)
    print('len=', len(s))
    print('head=', s.head.item)
    print('tail=', s.tail.item)
    print(s.pop())
    print(s)
    print('len=', len(s))
    print('head=', s.head.item)
    print('tail=', s.tail.item)
    s.put(4)
    print(s)
    print('len=', len(s))
    print('head=', s.head.item)
    print('tail=', s.tail.item)

    print(s.isEmpty())
    # print(s.pop())

輸出結果爲:
True
[4, 3, 2, 1]
len= 4
head= 4
tail= 1
4
[3, 2, 1]
len= 3
head= 3
tail= 1
3
[2, 1]
len= 2
head= 2
tail= 1
[4, 2, 1]
len= 3
head= 4
tail= 1
False
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章