得到求最大值,最小值的功能栈
- 问题描述
利用栈求一组输入数据的最大和最小值
- 问题解析
准备两个栈,
- Stack_Date用于平常数据的压入和弹出数据
- Stack_Max/Stack_Min用于求最大值的功能栈
- 当对Stack_Date栈进行压栈时,如果Stack_Max/Stack_Min的栈是空栈,则直接压入栈中,当Stack_Max/Stack_Min的栈不是空栈时就要对Stack_Max/Stack_Min的栈顶元素进行比较,当大于/小于栈顶元素时,对功能栈进行压栈操作,到最后共嫩栈的栈顶元素即为所求。
代码实现
- 封装错误
class None_Stack_Error(Exception):
def __str__(self):
return "There are no elements available in the stack"
- 封装错误
当范围栈顶元素时/删除栈顶元素时栈不能为空栈
list实现栈功能
class Stack(object):
def __init__(self):
self.__list = []
@property
def Append_Stack_Number(self):
return self.__list
@Append_Stack_Number.setter
def Append_Stack_Number(self, Append_Stack_Number):
self.__list.append(Append_Stack_Number)
@property
def is_empty(self):
return self.__list == []
def push(self, item):
self.__list.append(item)
@property
def pop(self):
if self.is_empty:
raise None_Stack_Error
else:
return self.__list.pop()
@property
def top(self):
if self.is_empty:
raise None_Stack_Error
else:
return self.__list[-1]
def __str__(self):
return self.Append_Stack_Number
测试代码
if __name__ == '__main__':
new_Stack = Stack()
new_Stack.Append_Stack_Number = 2
new_Stack.Append_Stack_Number = 12
print(new_Stack.top)
print(new_Stack.is_empty)
# 12
# False
求最大值的栈功能实现
class Max_Stack:
def __init__(self):
self.__Stack_Date__ = Stack()
self.__Stack_Max__ = Stack()
self.__Stack_Append_Number__ = None
@property
def Stack_Append_Number(self):
if self.__Stack_Append_Number__ is not None:
self.__Stack_Append_Number__ = None
return
@property
def Stack_Date(self):
return self.__Stack_Date__.__str__()
@Stack_Append_Number.setter
def Stack_Append_Number(self, Stack_Append_Number):
self.__Stack_Append_Number__ = Stack_Append_Number
self.__append_Stack_Max__(__append_number__=self.__Stack_Append_Number__)
def __append_Stack_Max__(self, __append_number__):
self.__Stack_Date__.Append_Stack_Number = __append_number__
if self.__Stack_Max__.is_empty or self.__Stack_Max__.top < __append_number__:
self.__Stack_Max__.Append_Stack_Number = __append_number__
@property
def Stack_Max(self):
return self.__Stack_Max__.__str__()
def __str__(self):
return self.__Stack_Max__.top
测试代码
if __name__ == '__main__':
New_Max_Stack = Max_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)
"""
12
[12, 10, 3]
"""
求最小值的栈功能实现
class Min_Stack:
def __init__(self):
self.__Stack_Date__ = Stack()
self.__Stack_Min__ = Stack()
self.__Stack_Append_Number__ = None
@property
def Stack_Append_Number(self):
if self.__Stack_Append_Number__ is not None:
self.__Stack_Append_Number__ = None
return
@property
def Stack_Date(self):
return self.__Stack_Date__.__str__()
@Stack_Append_Number.setter
def Stack_Append_Number(self, Stack_Append_Number):
self.__Stack_Append_Number__ = Stack_Append_Number
self.__append_Stack_Min__(__append_number__=self.__Stack_Append_Number__)
def __append_Stack_Min__(self, __append_number__):
self.__Stack_Date__.Append_Stack_Number = __append_number__
if self.__Stack_Min__.is_empty or self.__Stack_Min__.top > __append_number__:
self.__Stack_Min__.Append_Stack_Number = __append_number__
@property
def Stack_Max(self):
return self.__Stack_Min__.__str__()
def __str__(self):
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] """