链表
不需要用连续的存储单元,区别于顺序表。链表中每一个元素称为结点。
链表与顺序表对比
操作 | 链表 | 顺序表 |
---|---|---|
访问 | O(n) | O(1) |
头插/删 | O(1) | O(n) |
尾插/删 | O(n) | O(1) |
中间插/删 | O(n) | O(n) |
单链表
1 头结点
2 item 数据
3 尾结点 next属性为空
代码实现
class singleNode(object):
def __init__(self, item):
self.item = item
self.next = None
class SingleLinkList(object):
def __init__(self):
self.__head = None
def is_empty(self):
return self.__head == None
def length(self):
c = 0
cur = self.__head
while cur != None:
cur = cur.next
c += 1
return c
def append(self,item):
node = singleNode(item)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
def travel(self):
cur = self.__head
while cur != None:
print(cur.item,end=' ')
cur = cur.next
print()
def add(self,item):
node = singleNode(item)
node.next,self.__head = self.__head,node
def insert(self,pop,item):
node = singleNode(item)
if pop <= 0:
self.add(item)
elif pop > self.length()-1:
self.append(item)
else:
c = 0
cur = self.__head
while c < (pop-1):
cur = cur.next
c += 1
node.next = cur.next
cur.next = node
def search(self,item):
cur = self.__head
while cur != None:
if cur.item == item:
return True
cur = cur.next
return False
def remove(self,item):#删除相应item的结点
pre = None
cur = self.__head
while cur != None:
if cur.item == item:
if cur == self.__head: #如果头结点是删除对象
self.__head = cur.next
# pre = cur
cur = cur.next
# return
else: # 否则把pre.next = cur.next
pre.next = cur.next
cur = cur.next
else: # 当前对象不是要删除的对象时的操作
pre = cur
cur = cur.next
单向循环链表
1 头结点
2 item 数据
3尾节点的next指向头结点
代码实现
class Node(object):
def __init__(self, item):
self.item = item
self.next = None
class SinCycLinkList(object):
def __init__(self, node=None):
self.__head = node
if node:
node.next = self.__head
def is_empty(self):
return self.__head == None
def length(self):
if self.is_empty():
return 0
cur = self.__head # 默认的self.__head是Node
count = 1
while cur.next != self.__head:
count += 1
cur = cur.next
return count
def travel(self):
if self.is_empty():
return None
cur = self.__head
while cur.next != self.__head:
print(cur.item, end=' ')
cur = cur.next
# 退出循环的时候,cur指向尾结点,但是尾结点并没有打印
print(cur.item)
def add(self, item):
"""链表头部添加元素"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = self.__head
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
node.next = self.__head
self.__head = node
# 尾结点指向新的节点
cur.next = node
def append(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
node.next = self.__head
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 = 1
while count < pos:
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def search(self, item):
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.item == item:
return True
cur = cur.next
# 循环退出 cur指向尾节点
if cur.item == item:
return True
return False
def remove(self, item):
if self.is_empty():
return
cur = self.__head
pre = None
# 头节点的元素就是要删除的元素
if cur.item == item:
# 链表中的节点不止一个
if cur.next != self.__head:
while cur.next != self.__head:
cur = cur.next
# 循环结束 cur 指向尾结点
cur.next = self.__head.next
self.__head = cur.next
else:
self.__head = None
# 第一个节点不是要删除的
else:
while cur.next != self.__head:
if cur.item == item:
# 删除
pre.next = cur.next
# return
break
else:
# 游标继续往下走
pre = cur
cur = cur.next
if cur.item == item:
pre.next = cur.next
def remove2(self, item):
if self.is_empty():
return
pre, cur = None, self.__head
while cur.next != self.__head:
if cur.item == item:
if cur == self.__head: #如果是头结点
self.__head = cur.next
cur = cur.next
else:
pre.next = cur.next
cur = cur.next
else:
pre = cur
cur = cur.next
if cur.item == item:
pre.next = self.__head
双向链表
1 pre指向上一个结点,头结点pre为None
2 item 数据
3 next指向下一个结点,尾结点Next为None
代码实现
class Node(object):
def __init__(self, item):
self.item = item
self.pre = None
self.next = None
class doubleLinkList(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
else:
cur = self.__head
c = 0
while cur is not None:
c += 1
cur = cur.next
return c
def travel(self):
cur = self.__head
while cur is not None:
print(cur.item, end=' ')
cur = cur.next
print()
def add(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
node.next = self.__head
self.__head.pre = node
self.__head = node
def append(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = node
node.pre = cur
def insert(self, pos, item):
node = Node(item)
c = 0
if pos <= c:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
cur = self.__head
while c < pos-1:
cur = cur.next
c += 1
node.pre = cur
node.next = cur.next
cur.next.pre = node
cur.next = node
def search(self,item):
cur = self.__head
while cur is not None:
if cur.item == item:
return True
cur = cur.next
return False
def remove(self, item):
cur = self.__head
if self.length() == 1:
if cur.item == item:
self.__head = None
return
while cur is not None:
if cur.item == item :
#删除头结点
if cur == self.__head:
self.__head = cur.next
cur = cur.next
#删除尾结点
elif cur.next is None:
cur.pre.next = None
return
#删除中间结点
else:
cur.pre.next = cur.next
cur.next.pre =cur.pre
cur = cur.next
else:
cur = cur.next