#节点
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
res = 0
while cur != None:
res += 1
cur = cur.next
return res
#遍历整个链表
def travel(self):
cur = self.__head
while cur != None:
print(cur.elem)
cur = cur.next
print ("")
#链表头部添加元素
def add(self, item):
node = Node(item)
node.next = self.__head
self.__head = node
#链表尾部添加元素
def append(self, item):
cur = self.__head
node = Node(item)
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)
return
elif pos >= self.length():
self.append(item)
return
#pos 从0开始
per = self.__head
node = Node(item)
i = 0
while i < pos - 1:
per = per.next
i += 1
node.next = per.next
per.next = node
#删除节点
def remove(self, item):
#通过双指针的运用,比较方便的删除节点,代码也好理解。
cur = self.__head
per = None
if cur == None:
return
while cur != None:
if cur.elem == item:
if cur == self.__head:
self.__head = cur.next
else:
per.next = cur.next
break
else:
per = cur
cur = cur.next
per = self.__head
if per == None:
return
elif per.elem == item:
per = per.next
return
while per.next != None:
if per.next.elem == item:
per.next.next = per.next
return
per = per.next
#查找节点是否存在
def search(self, item):
cur = self.__head
count = 0
while count < self.length():
count += 1
if cur.elem == item:
return True
return False
# 查找节点是否存在
def search1(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("length:", ll.length())
ll.append(3)
print ("length:",ll.length())
ll.append(2)
ll.append(1)
ll.travel()
ll.add(4)
ll.travel()
ll.insert(-1, 6)
ll.travel()
ll.insert(1, 5)
ll.travel()
ll.insert(10, 0)
ll.travel()
ll.remove(6)
ll.travel()
ll.remove(4)
ll.travel()
ll.remove(0)
ll.travel()
ll.remove(100)
ll.travel()
for i in range(0, 2000):
ll.append(i)
import time
a = time.time()
ll.search(10000)
print("length " + str(time.time() - a))
b = time.time()
ll.search1(10000)
print("no length " + str(time.time() - b))
?
?
#节点
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
self.prev = None
#单向链表
class DoubleLinkList(object):
#初始化的时候要传节点,不然就默认空,头指向空
def __init__(self, node = None):
self.__head = node
#链表是否为空
def is_empty(self):
return self.__head == None
#链表长度
def length(self):
#游标
cur = self.__head
res = 0
while cur != None:
res += 1
cur = cur.next
return res
#遍历整个链表
def travel(self):
cur = self.__head
while cur != None:
print(cur.elem)
cur = cur.next
print ("")
#链表头部添加元素
def add(self, item):
node = Node(item)
node.next = self.__head
self.__head = node
node.next.prev = node
#链表尾部添加元素
def append(self, item):
cur = self.__head
node = Node(item)
if self.is_empty():
self.__head = node
else:
while cur.next != None:
cur = cur.next
cur.next = node
node.prev = cur
#指定位置添加元素
def insert(self, pos, item):
if pos <= 0:
self.add(item)
return
elif pos >= self.length():
self.append(item)
return
#pos 从0开始
cur = self.__head
node = Node(item)
i = 0
while i < pos:
cur = cur.next
i += 1
node.next = cur
node.prev = cur.prev
cur.prev.next = node
cur.prev = node
#删除节点
def remove(self, item):
#通过双指针的运用,比较方便的删除节点,代码也好理解。
cur = self.__head
if cur == None:
return
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
count = 0
while count < self.length():
count += 1
if cur.elem == item:
return True
return False
# 查找节点是否存在
def search1(self, item):
cur = self.__head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
if __name__ == "__main__":
ll = DoubleLinkList()
print("length:", ll.length())
ll.append(3)
print ("length:",ll.length())
ll.append(2)
ll.append(1)
ll.travel()
ll.add(4)
ll.travel()
ll.insert(-1, 6)
ll.travel()
ll.insert(1, 5)
ll.travel()
ll.insert(10, 0)
ll.travel()
ll.remove(6)
ll.travel()
ll.remove(4)
ll.travel()
ll.remove(0)
ll.travel()
ll.remove(100)
ll.travel()
for i in range(0, 2000):
ll.append(i)
import time
a = time.time()
ll.search(10000)
print("length " + str(time.time() - a))
b = time.time()
ll.search1(10000)
print("no length " + str(time.time() - b))
|