得到求最大值,最小值的功能栈

    • 问题描述

      利用栈求一组输入数据的最大和最小值

    • 问题解析
    • 准备两个栈,

      • Stack_Date用于平常数据的压入和弹出数据
      • Stack_Max/Stack_Min用于求最大值的功能栈
      • 当对Stack_Date栈进行压栈时,如果Stack_Max/Stack_Min的栈是空栈,则直接压入栈中,当Stack_Max/Stack_Min的栈不是空栈时就要对Stack_Max/Stack_Min的栈顶元素进行比较,当大于/小于栈顶元素时,对功能栈进行压栈操作,到最后共嫩栈的栈顶元素即为所求。
    • 代码实现

      • 封装错误
        1. class None_Stack_Error(Exception):
        2. def __str__(self):
        3. return "There are no elements available in the stack"

    当范围栈顶元素时/删除栈顶元素时栈不能为空栈

    • list实现栈功能

      1. class Stack(object):
      2. def __init__(self):
      3. self.__list = []
      4. @property
      5. def Append_Stack_Number(self):
      6. return self.__list
      7. @Append_Stack_Number.setter
      8. def Append_Stack_Number(self, Append_Stack_Number):
      9. self.__list.append(Append_Stack_Number)
      10. @property
      11. def is_empty(self):
      12. return self.__list == []
      13. def push(self, item):
      14. self.__list.append(item)
      15. @property
      16. def pop(self):
      17. if self.is_empty:
      18. raise None_Stack_Error
      19. else:
      20. return self.__list.pop()
      21. @property
      22. def top(self):
      23. if self.is_empty:
      24. raise None_Stack_Error
      25. else:
      26. return self.__list[-1]
      27. def __str__(self):
      28. return self.Append_Stack_Number

    测试代码

    1. if __name__ == '__main__':
    2. new_Stack = Stack()
    3. new_Stack.Append_Stack_Number = 2
    4. new_Stack.Append_Stack_Number = 12
    5. print(new_Stack.top)
    6. print(new_Stack.is_empty)
    7. # 12
    8. # False
    • 求最大值的栈功能实现

      1. class Max_Stack:
      2. def __init__(self):
      3. self.__Stack_Date__ = Stack()
      4. self.__Stack_Max__ = Stack()
      5. self.__Stack_Append_Number__ = None
      6. @property
      7. def Stack_Append_Number(self):
      8. if self.__Stack_Append_Number__ is not None:
      9. self.__Stack_Append_Number__ = None
      10. return
      11. @property
      12. def Stack_Date(self):
      13. return self.__Stack_Date__.__str__()
      14. @Stack_Append_Number.setter
      15. def Stack_Append_Number(self, Stack_Append_Number):
      16. self.__Stack_Append_Number__ = Stack_Append_Number
      17. self.__append_Stack_Max__(__append_number__=self.__Stack_Append_Number__)
      18. def __append_Stack_Max__(self, __append_number__):
      19. self.__Stack_Date__.Append_Stack_Number = __append_number__
      20. if self.__Stack_Max__.is_empty or self.__Stack_Max__.top < __append_number__:
      21. self.__Stack_Max__.Append_Stack_Number = __append_number__
      22. @property
      23. def Stack_Max(self):
      24. return self.__Stack_Max__.__str__()
      25. def __str__(self):
      26. return self.__Stack_Max__.top

    测试代码

    1. if __name__ == '__main__':
    2. New_Max_Stack = Max_Stack()
    3. New_Max_Stack.Stack_Append_Number = 12
    4. New_Max_Stack.Stack_Append_Number = 10
    5. New_Max_Stack.Stack_Append_Number = 3
    6. print(New_Max_Stack.__str__())
    7. print(New_Max_Stack.Stack_Date)
    8. """
    9. 12
    10. [12, 10, 3]
    11. """
    • 求最小值的栈功能实现

      1. class Min_Stack:
      2. def __init__(self):
      3. self.__Stack_Date__ = Stack()
      4. self.__Stack_Min__ = Stack()
      5. self.__Stack_Append_Number__ = None
      6. @property
      7. def Stack_Append_Number(self):
      8. if self.__Stack_Append_Number__ is not None:
      9. self.__Stack_Append_Number__ = None
      10. return
      11. @property
      12. def Stack_Date(self):
      13. return self.__Stack_Date__.__str__()
      14. @Stack_Append_Number.setter
      15. def Stack_Append_Number(self, Stack_Append_Number):
      16. self.__Stack_Append_Number__ = Stack_Append_Number
      17. self.__append_Stack_Min__(__append_number__=self.__Stack_Append_Number__)
      18. def __append_Stack_Min__(self, __append_number__):
      19. self.__Stack_Date__.Append_Stack_Number = __append_number__
      20. if self.__Stack_Min__.is_empty or self.__Stack_Min__.top > __append_number__:
      21. self.__Stack_Min__.Append_Stack_Number = __append_number__
      22. @property
      23. def Stack_Max(self):
      24. return self.__Stack_Min__.__str__()
      25. def __str__(self):
      26. return self.__Stack_Min__.top

    测试代码

    if __name__ == '__main__':
       New_Max_Stack = Min_Stack()
       New_Max_Stack.Stack_Append_Number = 12
       New_Max_Stack.Stack_Append_Number = 10
       New_Max_Stack.Stack_Append_Number = 3
       print(New_Max_Stack.__str__())
       print(New_Max_Stack.Stack_Date)
    """
    3
    [12, 10, 3]
    """