1.单链表
"""
单链表的时间复杂度不会变,分散的空间可以利用,但是占用空间大
顺序表的空间必须是连续的
"""
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 == None
def length(self):
cur = self.__head #游标
count = 0 # 长度
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历"""
cur = self.__head
while cur != None:
print(cur.elem,end=' ')
cur = cur.next
print('\n')
def add(self,item):
"""头部添加,item:具体数据,头插法"""
node = Node(item)
node.next = self.__head # 新节点的next指向原有头结点 这样可以使得原有链表没有丢掉
self.__head = node # 然后再将__head指向新结点
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):
"""
指定位置添加 pre prior:指定位置前一位
:param pos:从0开始
:param item:
:return:
"""
if pos <= 0:
self.add(item) #小于0 默认头部添加
elif pos >(self.length()-1):
self.append(item) # 大于最后一个,默认尾部添加
else:
pre = self.__head # pre从头部开始
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 search(self,item):
"""查找o(n)"""
cur = self.__head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
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
if __name__ == '__main__':
ll = SingleLinkList()
print(ll.is_empty())
print(ll.length())
ll.append(1)
print(ll.is_empty())
print(ll.length())
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.add(8)
ll.travel()
ll.insert(-1,9)
ll.travel()
ll.insert(3,100)
ll.insert(10,200)
ll.travel()
ll.remove(100)
ll.travel()
2.单向循环链表
class Node(object):
def __init__(self,elem):
self.elem = elem
self.next = None
class SingCycleLinkList(object):
def __init__(self,node = None):
self.__head= node
# 链表头 私有属性
def is_empty(self):
return self.__head == None
def length(self):
if self.is_empty():
return 0 # 特殊情况如果是空返回0
cur = self.__head #游标
count = 1 # 长度count从1开始
while cur.next != self.__head: #循环链表和头结点比较
count += 1
cur = cur.next
return count
def travel(self):
"""遍历"""
if self.is_empty():
return 0
cur = self.__head
while cur.next != self.__head:
print(cur.elem,end=' ')
cur = cur.next
# 退出循环,cur指向尾结点,但尾结点元素没打印
print(cur.elem)
print('\n')
def add(self,item):
"""头部添加,item:具体数据,头插法"""
node = Node(item)
cur = self.__head
if self.is_empty():
self.__head = node
node.next = node # 指向自身
else:
while cur.next != self.__head:
cur = cur.next
node.next = self.__head
self.__head = node
# cur.next = node
cur.next = self.__head
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
# node.next = cur.next
node.next = self.__head
cur.next = node
def insert(self,pos,item):
"""
指定位置添加 pre prior:指定位置前一位
:param pos:从0开始
:param item:
:return:
"""
if pos <= 0:
self.add(item) #小于0 默认头部添加
elif pos >(self.length()-1):
self.append(item) # 大于最后一个,默认尾部添加
else:
# 与单链表相同 不涉及尾部
pre = self.__head # pre从头部开始
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 search(self,item):
"""查找"""
if self.is_empty():
return False
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
def remove(self,item):
"""删除具体节点内容"""
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next!= self.__head:
if cur.elem == item:
# 头结点
if cur == self.__head:
# 找尾结点
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
else:
# 中间结点
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 退出循环,cur指向尾结点
if cur.elem == item:
# 只有一个头结点的时候
if cur == self.__head:
self.__head = None
else:
pre.next = cur.next
if __name__ == '__main__':
ll = SingCycleLinkList()
print(ll.is_empty())
print(ll.length())
ll.append(1)
print(ll.is_empty())
print(ll.length())
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.add(8)
ll.travel()
ll.insert(-1,9)
ll.travel()
ll.insert(3,100)
ll.insert(10,200)
ll.travel()
ll.remove(100)
ll.travel()
ll.remove(9)
ll.travel()