SingleLinkList.py
#coding: utf-8
class Node(object):
"""节点"""
def __init__(self,elem):
self.elem=elem
self.next=None
class SingleLinkList(object):
"""单链表"""
def __init__(self,node=None):
self._head=node
def is_empty(self):
"""链表是否为空"""
return self._head is None
def length(self):
"""链表的长度"""
cur = self._head
count = 0
while cur != None:
cur = cur.next
count+=1
return count
def tarvel(self):
"""遍历链表"""
cur = self._head
while cur != None:
print(cur.elem, end=" ")
cur=cur.next
def add(self,item):
"""在链表的头部添加元素"""
node=Node(item)
node.next=self._head
self._head=node
def append(self,item):
"""在链表的尾部添加元素"""
node=Node(item)
cur=self._head
if self.is_empty():
self._head=node
else:
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:
node=Node(item)
cur= self._head
count=0
while count != pos-1:
cur=cur.next
count+=1
node.next=cur.next
cur.next=node
def remove(self,item):
"""删除节点"""
pre=None
cur=self._head
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 serach(self,item):
"""查询节点"""
cur=self._head
while cur != None:
if cur.elem == item:
return True
else:
cur=cur.next
return False
# if __name__ == "__main__":
# ll=SingleLinkList()
# #print(ll.is_empty())
#
# ll.append(10)
# ll.append(26)
# ll.add(88)
# ll.append(45)
# ll.insert(2,222)
# ll.insert(70,877)
# ll.add(90)
# ll.remove(26)
# ll.tarvel()
#
SingleLinkCycleList.py
# coding: utf-8
class Node(object):
"""节点"""
def __init__(self,elem):
self.elem=elem
self.next=None
class SingleLinkCycleList(object):
"""单向循环链表"""
def __init__(self,node=None):
self._head=node
if node:
node.next=node
def is_empty(self):
"""链表是否为空"""
return self._head is None
def length(self):
"""链表的长度"""
if self.is_empty():
return 0
else:
cur = self._head
count = 1
while cur.next != self._head:
cur = cur.next
count+=1
return count
def tarvel(self):
"""遍历链表"""
if self.is_empty():
return
else:
cur = self._head
while cur.next != self._head:
print(cur.elem, end=" ")
cur=cur.next
print(cur.elem)
def add(self,item):
"""在链表的头部添加元素"""
node=Node(item)
if self.is_empty():
self._head=node
node.next=node
else:
cur = self._head
while cur.next != self._head:
cur = cur.next
cur.next = node
node.next=self._head
self._head=node
def append(self,item):
"""在链表的尾部添加元素"""
node=Node(item)
if self.is_empty():
self._head=node
node.next=node
else:
cur=self._head
while cur.next != self._head:
cur=cur.next
cur.next=node
node.next=self._head
def insert(self,pos,item):
"""在链表中插入元素"""
if pos<0:
self.add(item)
elif pos>(self.length()-1):
self.append(item)
else:
node=Node(item)
cur= self._head
count=0
while count != pos-1:
cur=cur.next
count+=1
node.next=cur.next
cur.next=node
def remove(self,item):
"""删除节点"""
if self._head is None:
return
pre=None
cur=self._head
while cur.next != self._head:
if cur.elem == item:
if cur == self._head:
#头节点
rear=self._head
while rear.next != self._head:
rear=rear.next
rear.next=cur.next
self._head=cur.next
else:
pre.next=cur.next
#中间节点
return
else:
pre=cur
cur=cur.next
if cur.elem == item:
#尾节点
if cur == self._head:
self._head=None
else:
pre.next = cur.next
def serach(self,item):
"""查询节点"""
if self.is_empty():
return False
else:
cur=self._head
while cur.next != self._head:
if cur.elem == item:
return True
else:
cur=cur.next
if cur.elem == item:
return True
return False
if __name__ == "__main__":
ll=SingleLinkCycleList()
#print(ll.is_empty())
ll.append(10)
ll.append(26)
ll.add(88)
ll.append(45)
ll.insert(2,222)
ll.insert(70,877)
ll.add(90)
ll.remove(26)
ll.remove(90)
ll.tarvel()
DoubleLinkList.py
# coding: utf-8
from SingleLinkList import *
class Node(object):
'''创建节点'''
def __init__(self,item):
self.elem=item
self.prev=None
self.next=None
class DoubleLinkList(SingleLinkList):
'''双向链表'''
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
node=Node(item)
count=0
while count < pos:
count+=1
cur=cur.next
node.next=cur
node.prev=cur.prev
cur.prev.next=node
cur.prev=node
def remove(self,item):
cur=self._head
while cur != None:
if cur.elem == item:
if cur == self._head:
self._head=cur.next
if cur.next:
cur.next.prev=None
else:
cur.next.pre = cur.prev
if cur.next:
cur.prev.next = cur.next
break
cur=cur.next
if __name__ == "__main__":
ll=DoubleLinkList()
print(ll.is_empty())
ll.append(10)
ll.append(26)
ll.add(88)
ll.append(45)
ll.insert(2,222)
ll.insert(70,877)
ll.add(90)
ll.remove(26)
ll.tarvel()