1.抽象数据类型(ADT):拥有属性和方法,属性可以存储数据,方法操作属性
2.线性表
3.链表
属性:data-存储数据 next-下一个节点的地址
方法:增 删 改 查
next: 后继
pre: 前驱
4.链表的操作
class myList():
def init(self,element): #存储列表数据
self.__element = element #属性赋值
def delete(self):
del self.__element #删除元素
def set_element(self,element):
self.__element = element
def get_element(self):
return self.__element
ml = myList([1,4,5,6])
#ml.delete()
print(ml.get_element())
#----------------------------------------------------------------------------------------------------------------------#
#链表内每个元素占一块内存空间(节点),节点内部一部分储存数据,一部分储存下一个元素所在节点的地址
单向链表
class node(): #定义节点
def init(self,data=None,next=None):
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 delete_data(self):
del self.__data
class lianbiao(): #定义链表
def init(self,first):
self.__first = first
def delete(self,data):
if self.__first == None:
print("链表为空,无法删除")
else:
currentNode = self.__first
flag = False
while currentNode.get_next() != None:
if currentNode.get_data == data:
flag =True
break
else:
currentNode = currentNode.get_next()
if flag:
currentNode.delete_data(data)
def append(self,data): #链表追加元素
n = node(data)
if self.__first == None: #列表为空时,将增加的元素赋值作为第一个元素
self.__first = n
else: #列表不为空时
currentNode = self.__first
while currentNode.get_next() != None: #循环调出链表内的元素,直到最后一个(None)
currentNode = currentNode.get_next()
currentNode.set_next(n) #赋值
def insert(self,data1,data2): #data1:插入位置的前一个元素 data2:要插入的元素
if self.__first == None: #链表为空时,显示无法插入
print("空链表,没办法插入")
else: #列表不为空时
currentNode = self.__first
flag = False
while currentNode.get_next() != None:#循环调出链表元素到最后一个元素之前
if currentNode.get_data()==data1: #当循环到插入位置前一个元素时,break跳出循环
flag = True
break
else: #如果不是,继续循环
currentNode = currentNode.get_next()
if flag: #在标记处(data1)的下一位,赋值data2
n = node(data2, currentNode.get_next())
currentNode.set_next(n)
else: #如果当前节点正好是data1处,在下一位赋值data2
if currentNode.get_data()==data1:
currentNode.set_next(node(data2))
def showInfo(self): #查看链表
if self.__first == None: #如果链表为空
print("该链表为空")
else:
currentNode = self.__first #从第一个节点开始
while currentNode.get_next() != None: #循环直到最后一个节点(即下一个节点为None)
print(currentNode.get_data()) #打印当前节点数据
currentNode = currentNode.get_next() #循环赋值,打印
print(currentNode.get_data()) #打印最后一个节点
def update(self,data1,data2): #改
if self.__first == None:
print("该链表为空")
else:
currentNode = self.__first
flag = False
while currentNode.get_next() != None: #循环到最后一个元素
if currentNode.get_data()==data1: #循环直到改动处节点,break跳出循环
flag = True
break
else:
currentNode = currentNode.get_next() #不是data1处,继续循环
if flag:
currentNode.set_data(data2) #在当前节点(data1)处,修改数据为data2
else:
if currentNode.get_data()==data1: #如果改动的是第一个节点,直接赋值
currentNode.set_data(data2)
l = lianbiao(None)
l.append(“增加内容”)
l.insert(“增加内容”,“插入内容”)
l.update(“增加内容”,“改后内容”)
l.showInfo()
总结:
1.单向链表在追加、查看元素时:只需外侧循环到最后一位(检出下一处地址为空),且追加时只需要data一个参数,用于在末尾追加
处赋值。
2.单向链表在插入、删除、改动时:外层循环到最后一位节点的前提下,内层循环到标记处(需要删改的节点,或者插入点的前一位节点)
用break跳出整个循环。然后在标记处进行赋值,重写或者删除。插入和改动需要两个参数,data1作为插入点前一位数据或需要删
改处节点的数据,data2则作为插入节点数据或重写后的数据。删除需要删除处的参数
#----------------------------------------------------------------------------------------------------------------------#
双链表
class first_linked_last(): #定义基类
def init(self,first,last):
self.__first = first
self.__last = last
def insert(self,data0,data):
n = node(data)
if self.__first == None:
self.__first = n
self.__last = n
else:
currentnode = self.__first
flag = False
while currentnode.get_data() != None:
if currentnode.get_data() == data0:
flag = True
break
else:
currentnode = currentnode.get_next()
if flag:
n.set_next(currentnode.get_next())
currentnode.set_next(n)
else:
if currentnode.get_data() == data0:
self.insert_last(data)
def insert_first(self,data):
n = node(data)
if self.__first == None:
self.__first = n
self.__last = n
else:
n.set_next(self.__first)
self.__first = n
def insert_last(self,data):
n = node(data)
if self.__first == None:
self.__first = n
self.__last = n
else:
self.__last.set_next(n)
self.__last = n
def delete_first(self):
if self.__first == None:
print("空链表,不能删除")
else:
self.__first = self.__first.set_next()
def delete(self,data):
if self.__first == None:
print("空链表,不能删除")
else:
currentnode = self.__first
precurrentnode = self.__first
flag = False
while currentnode.get_next != None:
if currentnode.get_data() == data:
flag = True
break
else:
precurrentnode = currentnode
currentnode = currentnode.get_next()
if flag:
if currentnode == precurrentnode:
self.delete_first()
else:
precurrentnode.set_next(currentnode.get_next())
else:
if currentnode.get_data() == data:
precurrentnode.set_next(None)
self.__last = precurrentnode
def update_first(self,data):
if self.__first == None:
print("空链表,不能修改")
else:
self.__first.set_data(data)
def update_last(self,data0,data):
if self.__first == None:
print("空链表,不能修改")
else:
currentnode = self.__first
flag = False
while currentnode.get_data() == data0:
if currentnode.get_data() == data0:
flag = True
break
else:
currentnode = currentnode.get_next()
if flag:
currentnode.set.data(data)
else:
self.update_last(data)
def show(self):
if self.__first == None:
print("该链表为空")
else:
currentnode = self.__first
while currentnode != None:
print(currentnode.get_data())
currentnode = currentnode.get_next()
#----------------------------------------------------------------------------------------------------------------------#
双链表
class NullLinkedError(BaseException):#创建自定义异常,当链表为空时放出
def init(self):
super().init(“null linked error”)
class node():
#content:创建节点,表示链表中元素,有三个属性,
#pre-前驱结点地址
#data-数据
#next-后继结点地址
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
d = double_linked()
d.insert_first(“lhy”)
d.insert_first(“cy”)
d.insert_last(“hjx”)
d.insert_last(“wyx”)
print(d.length())
d.show_info()
d.delete_first()
d.delete_last()
print(d.length())
d.show_info()
d.insert_first(“lhy”)
d.insert_last(“cy”)
d.insert_last(“hh”)
d.delete(“cy”)
d.show_info()
print(d.length())