python创建链表
class SingleNode(object): # 默认是object,下面的类也同理,可使用class SingleNode:
def __init__(self,value):
self.val=value
self.next=None
# 创建单链表
class LinkedList(object):
def __init__(self):
self.head=None
def __len__(self):
p = self.head
count = 0
while p is not None:
p=p.next
count += 1
return count
def is_emperty(self):
return self.head==None
def addAtHead(self,value): # 头部添加元素
node = SingleNode(value) # 首先创建一个value值得结点
node.next=self.head # 将新节点的next指向头节点,即head指向的位置
self.head=node # 将链表的头head指向新节点
def addAtTail(self,value): # 尾部添加元素
node = SingleNode(value)
if self.is_emperty():
self.head=node
else:
cur = self.head
while cur.next:
cur = cur.next
cur.next=node
def addAtIndex(self,index,value):
if index<=0: # 如果指定位置为第一个元素之前,则执行头部插入
self.addAtHead(value)
elif index>(self.length-1): # 如果指定位置超过链表尾部,则执行尾部插入
self.addAtTail(value)
else: # 在链表中间
node = SingleNode(value)
count = 0
pre = self.head
while count< (index-1):
count +=1
pre=pre.next
node.next=pre.next
pre.next=node
def deleteAtIndex(self,index):
if index<0 or index>=self.length: # 如果删除的结点在第一个元素之前或者超过链表尾部,那么则找不到要删除的结点
return -1
else:
cur = self.head
pre =None
count = 0
while count<index:
count+=1
pre = cur
cur=cur.next
pre.next = cur.next
def get(self,index): # 返回index下标的结点值
if index < 0 or index>=self.length: # 如果索要的结点超出查找的范围
return -1
else:
cur = self.head
count = 0
while cur:
if count = index:
return cur.value
else:
count+=1
cur =cur.next
def search(self,item): # 查找是否存在,存在返回True,不存在返回False
cur =self.head
while cur != None:
if cur.value==item:
return True
cur = cur.next
return False
def travel(self): # 遍历链表
cur=self.head
while cur!=None:
print(cur.val,end=' ')
cur = cur.next
print('')
输入一串数字构建链表,所以只是创建的话,只涉及到两个函数,一个初始化,一个添加元素。
a=list(map(int,input().strip().split()))
mlist=MyLinkedList()
for i in a:
mlist.addAtTail(i)
mlist.travel() # 打印显示出来
# 例如输入是 2 3 4 5 6 7 8
# 那么输出的链表就是2->3->4->5->6->7->8
python创建二叉树
"""定义一个二叉树的节点类,括号也可以直接object省略,则默认是继承object类,同理下面的类也可以直接省略,即
class Node:
pass
"""
class Node(object):
def __init__(self,value):
self.number=number
self.left = None
self.right = None
"""
建立一个二叉树类,节点复制方法的赋值逻辑是先用一个列表依次左右孩子未满的结点(列表L),然后依次将传入的数值赋给该节点作为左右孩子的同时,依次将该节点的左右孩子放进列表里面。然后因为该节点左右孩子已满,将它从列表中删除。
"""
class Tree(object):
L=[] # 该列表,依次存放左右孩子未满的节点
def __init__(self):
self.root = None
def add(self,number): # 只添加一个节点
node = Node(number)
if self.root ==None: # 或者if self.root is None
self.root=node
Tree.L.append(self.root) # 存放起来,以防以后还要添加
return # 添加节点完成就退出
else:
while True:
point = Tree.L[0] # 依次对左右孩子未满得节点分配孩子
if point.left == None:
point.left = node
Tree.L.append(point.left) # 该节点后面作为父节点也是未满的,也要添加进去
return
elif point.right == None:
point.right = node
Tree.L.append(point.right)
Tree.L.pop(0) # 表示该节点已经拥有了左右孩子,那么从L列表中删除
return
输入一串数字构建二叉树
a=input().strip().split()
a=[int(i) if i.lstrip('-').isdigit() else None for i in a]
t=Tree()
for i in a:
t.add(i)
# 方法二:使用递归实现列表创建二叉树
def listcreattree(root,a,i):
if i<len(a):
if a[i] is None: # 当节点是None,即null的时候
return None
else:
root=Node(a[i]) # 将列表值变为树节点
# 往左回溯,从根开始一直到最左,直至为空
root.left = listcreattree(root.left,a,2*i+1)
# 往右回溯,
root.right = listcreattree(root.right,a,2*i+2)
return root
return root
t=listcreattree(None,a,0)
注意string.isdigit()函数:如果 string 只包含数字则返回 True 否则返回 False.但是检测负数的时候,由于负号不是数字所以会返回False,那么关于负数的isdigit()判断的方法有以下几种:
num = '-10'
# 方法一 对负数进行判断,并且使用逻辑表达式的运算结果,该知识点在下面
if (num.startwith('-') and num[1:] or num).isdigit():
print("num 是整数:",int(num))
else:
print("num不是整数")
# 方法二,正则表达式法:
import re
if re.match(r'^-?(\.\d+|\d+(\.\d+)?)',num):
print("num 是整数:",int(num))
else:
print("num不是整数")
# 方法三,更pythonic的方法:
if num.lstrip('-').isdigit():
print("num 是整数:",int(num))
else:
print("num不是整数")
python逻辑运算结果的类型
需要注意的是:and和or的运算结果不一定是布尔类型!具体类型是由返回的x或y决定的。只有not操作返回的才一定是布尔类型!
Python中,数字,字符串,列表等都能参与逻辑运算。0,空字符串,空列表当作False;而非空值当作True。
在and和or运算中,python使用了短路计算。即如果x的值已经决定了结果,将不执行y。x和y可以是变量或者表达式。