顺序表
from timeit import Timer
def append_test():
li = []
for i in range(10000):
li.append(i)
def insert_test():
li = []
for i in range(10000):
li.insert(0, i)
append_timer = Timer('append_test()', 'from __main__ import append_test')
print('append插入执行时间:', append_timer.timeit(1000))
insert_timer = Timer('insert_test()', 'from __main__ import insert_test')
print('insert插入执行时间:', insert_timer.timeit(1000))
单向链表
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList:
def __init__(self, node=None):
if node != None:
headNode = Node(node)
self.__head = headNode
else:
self.__head = node
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:
curNode = self.__head
while curNode.next != None:
curNode = curNode.next
curNode.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)
count = 0
preNode = self.__head
while count < (pos - 1):
count += 1
preNode = preNode.next
node.next = preNode.next
preNode.next = node
def remove(self, item):
curNode = self.__head
preNode = None
while curNode != None:
if curNode.elem == item:
if preNode == None:
self.__head = curNode.next
else:
preNode.next = curNode.next
break
else:
preNode = curNode
curNode = curNode.next
def search(self, item):
curNode = self.__head
while curNode != None:
if curNode.elem == item:
return True
curNode = curNode.next
return False
def is_empty(self):
return self.__head == None
def length(self):
count = 0
curNode = self.__head
while curNode != None:
count += 1
curNode = curNode.next
return count
def travel(self):
curNode = self.__head
while curNode != None:
print(curNode.elem, end=' ')
curNode = curNode.next
print("")
if __name__ == '__main__':
singleLinkList = SingleLinkList()
print('是否是空链表:', singleLinkList.is_empty())
print('链表的长度:', singleLinkList.length())
print('----------遍历单链表----------')
singleLinkList.travel()
print('--------查找---------')
print(singleLinkList.search(20))
print(singleLinkList.search(30))
print('------头部插入-----------')
singleLinkList.add(1)
singleLinkList.add(2)
singleLinkList.add(3)
singleLinkList.travel()
print('------尾部追加-----------')
singleLinkList.append(10)
singleLinkList.append(20)
singleLinkList.append(30)
singleLinkList.travel()
print('链表的长度:', singleLinkList.length())
print('----------指定位置插入----------')
singleLinkList.insert(2, 100)
singleLinkList.travel()
singleLinkList.insert(-1, 200)
singleLinkList.travel()
singleLinkList.insert(100, 300)
singleLinkList.travel()
print('---------删除节点--------')
singleLinkList.remove(100)
singleLinkList.travel()
singleLinkList.remove(200)
singleLinkList.travel()
singleLinkList.remove(300)
singleLinkList.travel()
双向链表
class Node:
def __init__(self, elem):
self.elem = elem
self.next = None
self.prev = None
class DoubleLinkList:
def __init__(self, node=None):
if node != None:
headNode = Node(node)
self.__head = headNode
else:
self.__head = node
def add(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
node.next = self.__head
self.__head.prev = node
self.__head = node
def append(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
curNode = self.__head
while curNode.next != None:
curNode = curNode.next
curNode.next = node
node.prev = curNode
def insert(self, pos, item):
if pos <= 0:
self.add(item)
elif pos > (self.length() - 1):
self.append(item)
else:
node = Node(item)
count = 0
curNode = self.__head
while count < (pos - 1):
count += 1
curNode = curNode.next
node.prev = curNode
node.next = curNode.next
curNode.next.prev = node
curNode.next = node
def remove(self, item):
curNode = self.__head
while curNode != None:
if curNode.elem == item:
if curNode == self.__head:
self.__head = curNode.next
if curNode.next:
curNode.next.prev = None
else:
curNode.prev.next = curNode.next
if curNode.next:
curNode.next.prev = curNode.prev
break
else:
curNode = curNode.next
def search(self, item):
curNode = self.__head
while curNode != None:
if curNode.elem == item:
return True
curNode = curNode.next
return False
def is_empty(self):
return self.__head == None
def length(self):
count = 0
curNode = self.__head
while curNode != None:
count += 1
curNode = curNode.next
return count
def travel(self):
curNode = self.__head
while curNode != None:
print(curNode.elem, end=' ')
curNode = curNode.next
print("")
if __name__ == '__main__':
doubleLinkList = DoubleLinkList()
doubleLinkList.add(11)
doubleLinkList.add(22)
doubleLinkList.add(33)
doubleLinkList.travel()
print('-----------追加-----------')
doubleLinkList.append(100)
doubleLinkList.append(200)
doubleLinkList.append(300)
doubleLinkList.travel()
print('指定位置插入')
doubleLinkList.insert(-1, 44)
doubleLinkList.travel()
doubleLinkList.insert(100, 400)
doubleLinkList.travel()
doubleLinkList.insert(2, 1000)
doubleLinkList.travel()
print('------删除节点--------')
doubleLinkList.remove(44)
doubleLinkList.travel()
doubleLinkList.remove(1000)
doubleLinkList.travel()
doubleLinkList.remove(400)
doubleLinkList.travel()
print('链表的长度:', doubleLinkList.length())
print('查找节点11', doubleLinkList.search(11))
print('查找节点111', doubleLinkList.search(111))
栈
class Stack(object):
def __init__(self):
self.__list = []
def push(self, item):
self.__list.append(item)
def pop(self):
return self.__list.pop()
def peek(self):
return self.__list[len(self.__list) - 1]
def is_empty(self):
return self.__list == []
def size(self):
return len(self.__list)
if __name__ == '__main__':
stack = Stack()
print('是否空栈吗', stack.is_empty())
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
print('是否空栈吗', stack.is_empty())
print('栈的长度:', stack.size())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
队列
class Queue:
def __init__(self):
self.__list = []
def enqueue(self, item):
self.__list.insert(0, item)
def dequeue(self):
return self.__list.pop()
def is_empty(self):
return self.__list == []
def size(self):
return len(self.__list)
if __name__ == '__main__':
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
print(queue.is_empty())
print('队列大小', queue.size())
print('-----出队---------')
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
二叉树
class Node:
def __init__(self, elem):
self.elem = elem
self.lchild = None
self.rchild = None
class Tree:
def __init__(self):
self.root = None
def add(self, elem):
node = Node(elem)
if self.root == None:
self.root = node
else:
queue = []
queue.append(self.root)
while queue:
curNode = queue.pop(0)
if curNode.lchild == None:
curNode.lchild = node
return
else:
queue.append(curNode.lchild)
if curNode.rchild == None:
curNode.rchild = node
return
else:
queue.append(curNode.rchild)
def travel(self):
queue = []
if self.root is None:
return
else:
queue.append(self.root)
while queue:
curNode = queue.pop(0)
print(curNode.elem, end=' ')
if curNode.lchild is not None:
queue.append(curNode.lchild)
if curNode.rchild is not None:
queue.append(curNode.rchild)
print()
def preOrder(self, root):
if root is None:
return
else:
print(root.elem, end=' ')
self.preOrder(root.lchild)
self.preOrder(root.rchild)
def inOrder(self, root):
if root is None:
return
else:
self.inOrder(root.lchild)
print(root.elem, end=' ')
self.inOrder(root.rchild)
def lastOrder(self, root):
if root is None:
return
else:
self.lastOrder(root.lchild)
self.lastOrder(root.rchild)
print(root.elem, end=' ')
if __name__ == '__main__':
tree = Tree()
tree.add(0)
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)
tree.add(7)
tree.add(8)
tree.add(9)
print('广度优先遍历')
tree.travel()
print('先序遍历')
tree.preOrder(tree.root)
print()
print('中序遍历')
tree.inOrder(tree.root)
print()
print('后序遍历')
tree.lastOrder(tree.root)