单链表
# 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 == None
def length(self):
"""链表长度"""
# cur游标, 用来移动遍历节点
cur = self.__head
# count
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 != None:
cur = cur.next
cur.next = node
def insert(self, pos, item):
"""链表中指定位置添加元素
:param pos : 从pos位置开始插入
"""
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
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
双链表
# coding:utf-8
class Node(object):
""""双链表节点"""
def __init__(self, elem):
self.elem = elem
self.next = None
self.prev = None
class SingleLinkList(object):
"""双链表"""
def __init__(self, node=None):
self.__head == node
def is_empty(self):
"""判断链表是否为空"""
return self.__head == None
def length(self):
"""链表长度"""
# cur游标, 用来移动遍历节点
cur = self.__head
# count
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 != None:
cur = cur.next
cur.next = node
node.prev = cur
def insert(self, pos, item):
"""链表中指定位置添加元素
:param pos : 从pos位置开始插入
"""
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
cur = cur.next
node = Node(item)
node.next = pre.next
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.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
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
单向循环链表的实现
# 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
if node:
node.next = node
def is_empty(self):
"""判断链表是否为空"""
return self.__head == None
def length(self):
"""链表长度"""
if self.is_empty():
return 0
# cur游标, 用来移动遍历节点
cur = self.__head
# count
count = 1
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
if self.is_empty():
return
cur = self.__head
while cur.next != self.__head:
print(cur.elem)
cur = cur.next
# 当退出循环的时候, cur指向尾节点, 但尾节点并未打印出来
print(cur.elem)
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
node.next = node
else:
cur == self.__head
while cur.next != self.__head:
cur = cur.next
node.next = self.__head
self.__head = node
cur.next = self.__head
def insert(self, pos, item):
"""链表中指定位置添加元素
:param pos : 从pos位置开始插入
"""
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
node = Node(item)
node.next = pre.next
pre.next = node
def remove(self, item):
"""删除节点"""
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 = self.__head
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