使用python实现链表,并提供基本的增删改查,只有大致思路,细节未处理。只是一次练习,作为学习笔记。
class Node():
def __init__(self,item):
self.__item=item
self.__next=None #指针部分默认指向None
def getItem(self):
return self.__item
def getNext(self):
return self.__next
def setItem(self,newItem):
self.__item=newItem
def setNext(self,newNext):
self.__next=newNext
class SingleLinkList():
def __init__(self):
"""初始化链表"""
self.__head=None
def isEmpty(self):
"""判断链表是否为空表"""
return self.__head==None
def getSize(self):
"""获取链表长度大小"""
currentSite=self.__head
count=0
while currentSite !=None:
count+=1
currentSite=currentSite.getNext()
return count
def findAll(self):
"""遍历链表"""
currentSite=self.__head
nodeList=[]
while currentSite!=None:
nodeList.append(currentSite.getItem())
currentSite=currentSite.getNext()
return nodeList
def add(self,item):
"""在链表前添加一个数据"""
temp=Node(item)
temp.setNext(self.__head)
self.__head=temp
def append(self,item):
"""在链表后追加一个结点"""
temp=Node(item)
# 空表,将需要append的Node作为第一个节点
if self.isEmpty():
self.__head=temp
# 如果不是空表,则遍历这个链表,并添加到最后
else:
currentSite=self.__head
while currentSite.getNext()!=None:
currentSite=currentSite.getNext()
currentSite.setNext(temp)
def searchByItem(self,item):
"""判断输入的结点数据是否存在于该链表中"""
currentSite=self.__head
foundItemFlag=False
while currentSite!=None:
if(currentSite.getItem()==item):
foundItemFlag=True
break
currentSite=currentSite.getNext()
return foundItemFlag
def getIndexByItem(self,item):
"""判断输入的结点数据存在于该链表中索引位置,如果不存在则返回-1"""
currentSite=self.__head
count=0
foundFalg=False
while currentSite!=None:
if currentSite.getItem()==item:
foundFalg=True
break
else:
count+=1
currentSite=currentSite.getNext()
if foundFalg:
return count
else:
return -1
def remove(self,item):
"""刪除一个结点"""
currentSite=self.__head
# 匹配的结点的前置结点引用
pre=None
while currentSite!=None:
if currentSite.getItem()==item:
if not pre:
self.__head=currentSite.getNext()
else:
pre.setNext(currentSite.getNext())
return item
else:
pre=currentSite
currentSite=currentSite.getNext()
raise Exception("删除不存在的结点")
def insert(self,pos,item):
if self.getSize()==0:
if pos ==0:
self.append(item)
else:
raise Exception("插入位置出错")
else:
if pos<0 or pos>self.getSize():
raise Exception("插入位置出错")
else:
currentSite = self.__head
count=0
pre=None
while currentSite!=None or (currentSite ==None and pre!=None):
if count==pos:
if not pre:
print("pre")
temp=Node(item)
temp.setNext(currentSite)
self.__head =temp
break
else:
temp=Node(item)
temp.setNext(currentSite)
pre.setNext(temp)
break
else:
pre=currentSite
currentSite=currentSite.getNext()
count+=1
def __str__(self):
tempList=self.findAll()
string=''
for i in tempList:
string+=str(i)+'\n'
return string
if __name__ == '__main__':
class People():
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def doSomething(self):
print("people can do something")
def saySomething(self):
print("people can say something")
def __str__(self):
return "you'r name is {0} and age is {1} sex is{2}".format(self.name,self.age,self.sex)
from Data.People import *
class Teacher(People):
def __init__(self,name,sex,age,salary):
People.__init__(self,name=name,age=age,sex=sex)
self.salary=salary
def doSomething(self):
print("{0} you are a teacher and you can teach students and your salary is {1}".format(self.name,self.salary))
def saySomething(self):
print("{0} you are a teacher and you can say something".format(self.name))
def __str__(self):
return "you'r name is {0} and age is {1} sex is{2} salary is {3}".format(self.name,self.age,self.sex,self.salary)
from Data.People import *
class Student(People):
def __init__(self,name,sex,age,gpa):
People.__init__(name=name,age=age,sex=sex)
self.GPA=gpa
def doSomething(self):
print("{0} you are a student and you can study from your teacher and your GPA is {1}".format(self.name,self.GPA))
def saySomething(self):
print("{0} you are a student and you can say something".format(self.name))
def __str__(self):
return "you'r name is {0} and age is {1} sex is{2} GPA is {3}".format(self.name,self.age,self.sex,self.GPA)
from Data.People import *
from Data.Student import *
from Data.Teacher import *
from LinkList.LinkList import *
if __name__ == '__main__':
teacher1=Teacher('王老师','男',35,10000)
teacher2=Teacher('李老师','女',20,9000)
myLinkList=SingleLinkList()
myLinkList.append(teacher1)
myLinkList.append(teacher2)
print(myLinkList)
teacher3 = Teacher('张老师', '女', 25, 8000)
myLinkList.add(teacher3)
print("myLinkList is ",myLinkList.getSize())
print("张老师存在",myLinkList.searchByItem(teacher3))
print('吴老师',myLinkList.searchByItem(Teacher('吴老师', '女', 25, 8000)))
print(myLinkList)
print("结点为王老师的索引为",myLinkList.getIndexByItem(teacher1))
wang=myLinkList.remove(teacher1)
print("=====================")
wang.doSomething()
print("=====================")
print('删除王老师',wang)
print(myLinkList)
print(type(wang))
print(id(wang))
print(id(teacher1))
# 47230800
# 47230800
# SingleLinkList类方法中的匹配都是使用了== 所以必须两个属于同一个对象,实际项目中应该使用(所有或者部分属性)等值比较
print(wang==teacher1)
# 由于python是动态数据类型,没有其他高级语言如java的泛型,所以学生链表同理,
# 但是如果需要保持同一个链表存储数据的数据类型一致,可以在存入时判断对象的类型是否属于某个类型,再判断是否存入链表中
print(Teacher)
# 考虑等类型,不考虑继承
print(type(teacher1) ==Teacher)
print(type(teacher1) ==People)
# 考虑等类型,也考虑继承
print(isinstance(teacher1,Teacher))
print(isinstance(teacher1,People))
# 以下省略了学生链表的实现,实现与老师同理