#自定义异常
class NullLinkedError(BaseException):
def __init__(self):
super().__init__("null linked error")
#定义一个节点类
class node():
def __init__(self,data,pre=None,next=None):
self.__pre = pre
self.__data = data
self.__next = next
def get_data(self):
return self.__data
def set_data(self,data):
self.__data = data
def get_next(self):
return self.__next
def set_next(self,next):
self.__next = next
def get_pre(self):
return self.__pre
def set_pre(self,pre):
self.__pre = pre
#定义一个双向链表
class double_linked():
def __init__(self,len=0,first=None,last=None):
self.__len = 0
self.__first = first
self.__last = last
#增
def insert_first(self,data):
#首先判断是否是空链表
n = node(data=data)
if self.__len==0:
self.__first = n
self.__last = n
else:
n.set_next(self.__first)
self.__first.set_pre(n)
self.__first = n
self.__len += 1
def insert_last(self,data):
n = node(data=data)# 关键字参数
if self.__len == 0:
self.__first = n
self.__last = n
else:
n.set_pre(self.__last)
self.__last.set_next(n)
self.__last = n
self.__len += 1
def insert(self,data0,data):
n = node(data=data)
if self.__len == 0:
raise NullLinkedError()
else:
currentnode = self.__first
flag = False
while currentnode!=None:
if currentnode.get_data()==data0:
if currentnode.get_next()!=None:
n.set_next(currentnode.get_next())
n.set_pre(currentnode)
currentnode.set_next(n)
currentnode.get_next().set_pre(n)
self.__len += 1
else:
self.insert_last(data)
flag = True
break
else:
currentnode = currentnode.get_next()
if flag:
print("插入成功")
else:
print("插入失败")
#删
#删除头结点,若空抛出异常,否则删除头元素
def delete_first(self):
if self.__len == 0:
raise NullLinkedError()
else:
if self.__len == 1:
self.__first = None
self.__last = None
else:
self.__first = self.__first.get_next()
self.__first.set_pre(None)
self.__len -= 1
# 删除尾结点,若空抛出异常,否则删除尾元素
def delete_last(self):
if self.__len == 0:
raise NullLinkedError()
else:
if self.__len == 1:
self.__first = None
self.__last = None
else:
self.__last = self.__last.get_pre()
self.__last.set_next(None)
self.__len -= 1
def delete(self,data):
if self.__len == 0:
raise NullLinkedError()
elif self.__len == 1 :
if self.__first.get_data()==data:
self.__first = None
self.__last = None
self.__len -= 1
else:
currentnode = self.__first
while currentnode!=None:
if currentnode.get_data()==data:
if currentnode.get_pre()==None:
self.delete_first()
elif currentnode.get_next()==None:
self.delete_last()
else:
currentnode.get_pre().set_next(currentnode.get_next())
currentnode.get_next().set_pre(currentnode.get_pre())
self.__len -= 1
break
else:
currentnode = currentnode.get_next()
#改
def update_first(self,data):
if self.__len == 0:
raise NullLinkedError()
else:
self.__first.set_data(data)
def update_last(self,data):
if self.__len == 0:
raise NullLinkedError()
else:
self.__last.set_data(data)
def update(self,data0,data):
if self.__len == 0:
raise NullLinkedError()
else:
currentnode = self.__first
flag = False
while currentnode!=None:
if currentnode.get_data()==data0:
currentnode.set_data(data)
flag = True
break
else:
currentnode = currentnode.get_next()
if flag:
print("修改成功")
else:
print("修改失败")
#查
def show_info(self):
if self.__len == 0:
print("空链表")
else:
currentnode = self.__first
while currentnode!=None:
print(currentnode.get_data())
currentnode = currentnode.get_next()
def length(self):
return self.__len
#查看队头元素
def getFirst(self):
return self.__first
---------------------------------------------------------------------------------------
栈的应用 后缀表达式计算
前缀表达式(从右往左查第一个运算符,与后缀相反)
中缀表达式:
后缀表达式:从左往右第一个遇到的计算符,前面去两位计算)
a+bc-d abc+d- -d+*bca
(a+b)c ab+c c+ab
(a+b)(c-d)/e ab+cd-e/ /e-cd+ab