SingleLinkList __init__()
#coding:utf-8
class Node(object):
'''节点'''
def __init__(self,elem):
self.elem=elem
self.next=None
class SingeLinkList(object):
'''单链表'''
def __int__(self,node=None):
self.__head=node
def is_empty(self):
'''链表是否为空'''
return self.__head==None
def length(self):
'''链表长度'''
#cur游标用来移动遍历节点
cur=self.__head
#count记录数量
count=0
while cur!=None:
count+=1
cur=cur.next
return count
def travel(self):
'''遍历整个链表'''
cur=self.__head
while cur!=None:
print(cur.elem)
cur=cur.next
def add(self,item):
'''链表头部添加元素'''
node=Node(item)
node.next=self.__head
self.__head=node
def append(self,item):
'''链表尾部添加元素'''
node=Node(item)
#判断当前链表是否为空
if self.is_empty():
self.__head=node
else:
cur=self.__head
while cur.next!=None:
cur=cur.next
cur.next=node
def insert(self,pos,item):
'''指定位置添加元素'''
if pos<=0:
self.add(item)
elif pos>self.length()-1
self.append(item)
else:
pre=self.__head
count=0
while count<(pos-1):
count+=1
pre=pre.next
# 当循环退出后,pre指向pos-1位置
node=Node(item)
node.next=pre.next
pre.next=node
def remove(self,item):
'''删除节点'''
cur=self.__head
pre=None
while cur!=None:
if cur.elem==item:
# 先判断此节点是否是头节点
if cur==self.__head:
self.__head=cur.next
else:
pre.next=cur.next
break
else:
pre=cur
cur=cur.next
def search(self,item):
'''查找节点是否存在'''
cur=self.__head
while cur!=None:
if cur.elem==item:
return True
else:
cur=cur.next
return False
# 创建链表对象方法
node=Node(100)
single_obj=SingleLinkList()
single_obj.travel()
if __name__=="__main__":
linklist=SingleList()
print(linklist.is_empty())
print(linklist.length())
linklist.append(1)
print(linklist.is_empty())
print(linklist.length())
linklist.append(3)
linklist.add(8)
linklist.append(2)
linklist.append(4)
linklist.insert(-1,9)
linklist.travel()
双向链表
每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。
操作
- is_empty() 链表是否为空
- length() 链表长度
- travel() 遍历整个链表
- add(item) 链表头部添加元素
- append(item) 链表尾部添加元素
- insert(pos,item) 指定位置添加元素
- remove(item) 删除节点
- search(item) 查找节点是否存在
实现
class Node(object):
"""节点"""
def __init__(self,item):
self.elem=item
self.next=None
self.prev=None
class DoubleLinkList(object):
"""双链表"""
def __int__(self,node=None):
self.__head=node
def is_empty(self):
'''链表是否为空'''
return self.__head==None
def length(self):
'''链表长度'''
#cur游标用来移动遍历节点
cur=self.__head
#count记录数量
count=0
while cur!=None:
count+=1
cur=cur.next
return count
def travel(self):
'''遍历整个链表'''
cur=self.__head
while cur!=None:
print(cur.elem)
cur=cur.next
def add(self,item):
'''链表头部添加元素'''
node=Node(item)
node.next=self.__head
self.__head=node
node.next.prev=node
def append(self,item):
'''链表尾部添加元素'''
node=Node(item)
#判断当前链表是否为空
if self.is_empty():
self.__head=node
else:
cur=self.__head
while cur.next!=None:
cur=cur.next
cur.next=node
node.prev=cur
def insert(self,pos,item):
'''指定位置添加元素'''
if pos<=0:
self.add(item)
elif pos>(self.length()-1):
self.append(item)
else:
cur=self.__head
count=0
while count<(pos-1):
count+=1
cur=cur.next
# 当循环退出后,cur指向pos位置
node=Node(item)
node.next=cur
node.prev=cur.prev
cur.prev.next=node
cur.prev=node
def remove(self,item):
'''删除节点'''
cur=self.__head
pre=None
while cur!=None:
if cur.elem==item:
# 先判断此节点是否是头节点
if cur==self.__head:
self.__head=cur.next
else:
pre.next=cur.next
break
else:
pre=cur
cur=cur.next
def search(self,item):
'''查找节点是否存在'''
cur=self.__head
while cur!=None:
if cur.elem==item:
return True
else:
cur=cur.next
return False
# 创建链表对象方法
node=Node(100)
single_obj=SingleLinkList()
single_obj.travel()
if __name__=="__main__":
linklist=SingleList()
print(linklist.is_empty())
print(linklist.length())
linklist.append(1)
print(linklist.is_empty())
print(linklist.length())
linklist.append(3)
linklist.add(8)
linklist.append(2)
linklist.append(4)
linklist.insert(-1,9)
linklist.travel()