简体   繁体   English

我只能将两个值推送到堆栈数组上?

[英]I can only push two values onto the stack array?

I can only push two values onto the stack array that I have created when I use the myPush() function.当我使用 myPush() 函数时,我只能将两个值推送到我创建的堆栈数组中。 The reason I need to do it this way is because I am not allowed to use the built-in stack functions.我需要这样做的原因是因为我不允许使用内置的堆栈函数。 Plus I have to use an array not a lit.另外我必须使用一个不点亮的阵列。 Thank you.谢谢你。

I can only push two values onto the stack array that I have created when I use the myPush() function.当我使用 myPush() 函数时,我只能将两个值推送到我创建的堆栈数组中。 The reason I need to do it this way is because I am not allowed to use the built-in stack functions.我需要这样做的原因是因为我不允许使用内置的堆栈函数。 Plus I have to use an array not a lit.另外我必须使用一个不点亮的阵列。 Thank you.谢谢你。

class Stack:

    def __init__(self, data):
        if data > 0:
            self.stack = [0] * data
            self.top = -1
            self.stack[self.top] = self.stack[-1]
        elif data <= 0:
            self.stack = [0] * 10
            self.top = -1

    def showStack(self):
        for i in self.stack:
            print(i, end=" ")
        return 

    def isEmpty(self):
        return self.stack == []

    def myEmpty(self): #complete this function
        return # just a placeholder

    def push(self, data):
        self.stack.append(data)

    def myPush(self, data):
        if data == 0:
            print("You did not enter a value.")
        elif self.sizeStack() <= 10:
            current = self.stack[stack.top]
            self.stack[stack.top] = data
            self.stack[stack.top - 1] = current


    def pop(self):
        data = self.stack[-1]
        del self.stack[-1]
        return data

    def myPop(self): #complete this function
        return # just a placeholder


    def myPeek(self): 
        temp = self.top
        return temp

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

    userVal = int(input("Enter the size of the stack: "))
    stack = Stack(userVal)
    while True:
    print('\n1 display the stack')
    print('2 add a value to the stack')
    print('3 check the value at the top of the stack')
    print('4 remove the value at the top of the stack')
    print('5 check if the stack is empty')
    print('99 quit')

    option = int(input("Enter your choice: "))

    if option == 1:
        stack.showStack()
    elif option == 2:
        temp = int(input("Enter a number to add to the stack: "))
        stack.myPush(temp)
    elif option == 3:
        print(stack.peek())


    elif option == 99:
        break
    else:
        print('Wrong option')
        print

try defining a some customized functions that map to your list.尝试定义一些映射到您的列表的自定义函数。 Read up on customization here在此处阅读自定义

class stack:
    def __init__(self, data):
        self.stack = [0] * data
        self.top = -1
        # whatever you want to do to init the list

    def __str__(self): # replaces your showstack function now just use print(stack)
        return self.stack
    def __iter__(self): # creates an iterator you can use
        return iter(self.stack)
    def __len__(self): # replaces your sizestack function
        return len(self.stack) 

basically just adjust the methods you need.基本上只是调整你需要的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将numpy数组堆叠到对角线上 - Stack numpy array onto diagonal 为什么我不能在 wxPython 中绘制两个不同的位图? - Why can't I draw onto two different bitmaps in wxPython? 在Python中,如何将值映射到其他刻度上? - In Python, how can I map values onto other another scale? 使用pandas,如何比较两个数据帧中2列之间的值并将它们推送到新的数据帧? - Using pandas, how can I compare the values between 2 columns from two dataframes and push them to a new dataframe? 如何在一本字典中比较两个不同的值(数组)? - How can I compare two different values(array) in one dictionary? 我怎样才能只保留一些值数组,列表? - How can I keep only somes values in array, list? Python:考虑一个仅包含两个可能值的多次出现的列表。 如何向左“推/压缩”所有一个值 - Python: Considering a list comprising of multiple occurrences of only two possible values. How do I "push/compress" all of one value to the left 如何在不使用堆栈或队列的情况下编写自己的等效于Python的推入和弹出函数的Python? - How can I write my own Python equivalent of stack push and pop functions without using a stack or queue? 如何将numpy二维数组存储为磁盘上的一种二进制格式,可以用C ++读取 - How to store numpy two dimensional array onto disk as a kind of binary format which can be read with C++ 在 python 中,如何找到两个特定值之间的数组中的所有值 - In python how can I find all the values in an array that are in between two specific values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM