7-10 有关堆栈操作 Python

请实现一个Stack类,实现堆栈初始化,进栈,出栈,显示堆栈数据,堆栈长度等.

实现进栈方法 push(int x); 实现出栈方法 pop(); 实现求堆栈长度方法 size();实现显示堆栈数据方法:show()。

输入格式:
输入包含多个测试用例,每个测试用例均为数字,第一个数 表示对应的操作。

是1 时,表示初始化一个堆栈对象,最大长度为10,1后面可带上用空格分隔的0-n个数,这些数据为初始化堆栈的数据(如果数据个数n大于10 ,则最大长度为n),其后 也可不带数据,表示初始化的是一个空堆栈。

是2,表示进栈操作,即从堆栈顶部(尾部)将数据添加到堆栈,所以2 后面还要带一个数,两数之间用空格分隔,当没有第2个数时,显示 “data error”,当进栈操作没有建立堆栈对象时显示 “stack is not exist”.当进堆栈操作时,堆栈数据达到最大长度时,则显示”The stack is full”

是3 : 表示出栈操作,即从堆栈顶部(尾部)将数据弹出并显示。当出栈操作没有建立堆栈对象时显示 “stack is not exist”.当出堆栈操作时,堆栈没有数据,则显示”The stack is Empty”

是4:表示显示堆栈中所有元素,元素之间用空格分隔,最后元素后面没有空格。同样 当显示操作没有建立堆栈对象时显示 “stack is not exist”。

是5 :表示显示堆栈的长度。同样 当显示长度操作没有建立堆栈对象时显示 “stack is not exist”。
是其它数字,则退出

输出格式:
参考输入格式中说明。对应不同的功能输出不同。

输入样例:
在这里给出一组输入。例如:

3
2
1 2 3 4 5
5
4
3
3
2 56
4
0
输出样例:
在这里给出相应的输出。例如:

stack is not exist
data error
4
2 3 4 5
5
4
2 3 56

class Stack:
    def __init__(self, size = 10):

        self._content = []

        self._size = size

        self._current = 0

    def push(self, v):
        if self._current < self._size:
            for i in v:
                self._content.append(i)
            self._current = self._current+1
        else:
            print('Stack Full!')

    def pop(self):
        if self._content:
            self._current = self._current-1
            return self._content.pop(-1)
        else:
            print('Stack is empty!')

    def empty(self):
        self._content = []
        self._current = 0

    def isEmpty(self):
        return not self._content

    def maxlen(self):
        if sta.Size() < 10:
            self._size = 10
        else:
            self._size = sta.Size()
        return self._size

    def Size(self):
        return len(self._content)

    def show(self):
        for i in range(len(self._content)):
            if i != len(self._content) - 1:
                print(self._content[i], end=' ')
            else:
                print(self._content[i])

if __name__ == '__main__':
    flag = 0
    while flag == 0:
        num = input()
        if ' ' in str(num):
            lostr = num.split()
            if lostr[0] == '1':
                sta = Stack()
                sta.push(lostr[1:])
                flag = 1
            elif lostr[0] == '2':
                print("data error")
            else:
                break
        else:
            num = int(num)
            if num == 1:
                Stack.empty()
                flag = 1
            elif num == 3:
                print("stack is not exist")
            elif num == 2:
                print("data error")
            elif num == 4:
                print("stack is not exist")
            elif num == 5:
                print("stack is not exist")
            else:
                break

    while flag == 1:
        num = input()
        if ' ' in num:
            lostr = num.split()
            if lostr[0] == '2':
                if sta.Size() < sta.maxlen():
                    sta.push(lostr[1:])
                else:
                    print("The stack is full")
            else:
                break
        else:
            if num == '2':
                print("data error")
            elif num == '3':
                print(sta.pop())
            elif num == '4':
                if sta.isEmpty():
                    print("The stack is Empty")
                else:
                    sta.show()
            elif num == '5':
                print(sta.Size())
            else:
                break

Original: https://blog.csdn.net/m0_57723028/article/details/127815480
Author: 米兰的小铁匠2333
Title: 7-10 有关堆栈操作 Python

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/653263/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球