1. 队列
操作系统使用队列控制计算机进程—调度机制基于队列算法—目的是尽可能快地执行程序,同时服务尽可能多的用户;
队列是元素的有序集合,添加操作—尾部,移除操作—头部
假设队列尾部在列表的位置0(使用insert函数)----pop移除队列头部的元素(列表最后一个元素)—添加操作复杂度O(n)----移除操作复杂度O(1)
2. python实现队列
class Queue:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def enqueue(self,item):#入队列,队尾在下标0的位置
self.items.insert(0,item)
def dequeue(self):#出队列
return self.items.pop()
def size(self):
return len(self.items)
if __name__=='__main__':
q=Queue()
print(q.isEmpty())
q.enqueue('python')
q.enqueue('C++')
q.enqueue('C')
print(q.size())
print(q.dequeue())
print(q.isEmpty())
3. 传土豆
class Queue:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def enqueue(self,item):#入队列,队尾在下标0的位置
self.items.insert(0,item)
def dequeue(self):#出队列
return self.items.pop()
def size(self):
return len(self.items)
#传入参数---名字列表and计数常量
def potatoes(name,num):
q=Queue()
#首先入队列
for i in range(len(name)):
q.enqueue(name[i])
n=0#用于计数
#循环直到队列剩一个
while q.size()>1:
temp=q.dequeue()#出队
q.enqueue(temp)#入队
n+=1
if n%num==0:
q.dequeue()
return q.dequeue()
if __name__=='__main__':
name=["Bill","David","Susan","Jane","Kent","Brad"]
print(potatoes(name,7))
4. 打印任务
import random
class Queue:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def enqueue(self,item):#入队列,队尾在下标0的位置
self.items.insert(0,item)
def dequeue(self):#出队列
return self.items.pop()
def size(self):
return len(self.items)
#打印机类---Printer
class Printer:
def __init__(self,ppm):
self.pagerate=ppm#初始化打印速度---每分钟打印多少页
self.currentTask=None#当前打印机是否空闲
self.timeRemaining=0
#减量计时---并在执行完任务后设置为空闲状态
def tick(self):
if self.currentTask!=None:
self.timeRemaining=self.timeRemaining-1
if self.timeRemaining<=0:
self.currentTask=None
# 检查当前是否有待完成的任务
def busy(self):
if self.currentTask!=None:
return True
else:
return False
def startNext(self,newtask):
self.currentTask=newtask
self.timeRemaining=newtask.getPages()*60/self.pagerate#60的原因是进行一秒的打印
#打印任务类---Task
class Task:
def __init__(self,time):
self.timestamp=time#时间戳---用于计算等待时间---时间戳代表任务被创建并放入打印任务队列的时间
self.pages=random.randrange(1,21)#任务被创建时,随机数生成器会随机提供页数[1,20]
def getStamp(self):
return self.timestamp
def getPages(self):
return self.pages
def waitTime(self,currenttime):
return currenttime-self.timestamp#用currenttime减去该任务的时间戳--表示等待时间
def simulation(numSeconds,pagesPerMinute):#模拟总秒数---每分钟打印页数
labprinter=Printer(pagesPerMinute)
printQueue=Queue()
waitingtimes=[]
for currentSecond in range(numSeconds):
if newPrintTask():
task=Task(currentSecond)
printQueue.enqueue(task)
if (not labprinter.busy()) and (not printQueue.isEmpty()):
nexttask=printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labprinter.startNext(nexttask)
labprinter.tick()
averageWait=sum(waitingtimes)/len(waitingtimes)
print("Average Wait %6.2f secs %3d tasks remaining."%(averageWait,printQueue.size()))
#布尔辅助函数newPrintTask判断是否有新创建的打印任务
def newPrintTask():
num=random.randrange(1,181)
if num==180:
return True
else:
return False
if __name__=='__main__':
for i in range(10):
simulation(3600,5)
5. python实现双端队列
#列表0---后端 n--前端
class Deque:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def addFront(self,item):#将元素添加到双端队列前端
self.items.append(item)
def addRear(self,item):#添加到双端队列后端
self.items.insert(0,item)
def removeFront(self):#移除前端一个元素
return self.items.pop()
def removeRear(self):#移除后端一个元素
return self.items.pop(0)
def size(self):
return len(self.items)
if __name__=='__main__':
deq=Deque()
print(deq.isEmpty())
deq.addFront('python')
deq.addFront('php')
deq.addRear('c++')
deq.addRear('c')
print(deq.size())
print(deq.removeFront())
print(deq.removeRear())
print(deq.isEmpty())
6. 回文检测器
#双端队列存放待判断的字符串,分别从前后端移除元素进行比较
class Deque:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def addFront(self,item):#将元素添加到双端队列前端
self.items.append(item)
def addRear(self,item):#添加到双端队列后端
self.items.insert(0,item)
def removeFront(self):#移除前端一个元素
return self.items.pop()
def removeRear(self):#移除后端一个元素
return self.items.pop(0)
def size(self):
return len(self.items)
def pal_checker(string):
deq=Deque()
#首先将字符串进行入双端队列
for ch in string:
deq.addRear(ch)
label=True#标记字符---记录中途是否有不满足的情形
while deq.size()>1 and label:
fro=deq.removeFront()
rea=deq.removeRear()
if fro!=rea:
label=False
return label
if __name__=='__main__':
s=input('please input Palindrome string:')
if pal_checker(s):
print(s+"是回文!")
else:
print(s+"不是回文!")