python数据结构(2)

队列

队列代码实现

class queue():
    def __init__(self):
        print('队列初始化')
        self.items=[]
    def isempty(self):
        return self.items==[]
    def enqueue(self,item):
        print('入队列:',item)
        self.items.insert(0,item)
    def dequeue(self):
        print('出队列:')
        res=self.items.pop()
        return res
    def size(self):
        return len(self.items)

q=queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.dequeue())

应用(击鼓传花)

实现

class queue():
    def __init__(self):
        #print('队列初始化')
        self.items=[]
    def isempty(self):
        return self.items==[]
    def enqueue(self,item):
        #print('入队列:',item)
        self.items.insert(0,item)
    def dequeue(self):
        #print('出队列:')
        res=self.items.pop()
        return res
    def size(self):
        return len(self.items)


def hotpotato(Name,num):
    simqueue=queue()
    t=0
    for name in Name:
        simqueue.enqueue(name)
    while simqueue.size()>1:
        for i in range(1,num):
            simqueue.enqueue(simqueue.dequeue())
        result=simqueue.dequeue()
        t+=1
        print('第%s局'%t,result)
    return simqueue.dequeue()

result=hotpotato(['1','2','3','4','5','6'],4)
print(result)

应用(模拟打印机打印任务)

打印任务建模

模拟流程

代码实现

代码具体实现

class queue():
    def __init__(self):
        #print('队列初始化')
        self.items=[]
    def isempty(self):
        return self.items==[]
    def enqueue(self,item):
        #print('入队列:',item)
        self.items.insert(0,item)
    def dequeue(self):
        #print('出队列:')
        res=self.items.pop()
        return res
    def size(self):
        return len(self.items)

import random
#定义打印机类
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

#定义任务类
class Task:
    def __init__(self,time):
        #生成时间
        self.timestamp=time
        # 生成打印页数
        self.pages=random.randrange(1,21)

    def getstamp(self):
        return self.timestamp

    def getpages(self):
        return self.pages
    def waittime(self,currenttime):
        #计算等待时间
        return currenttime-self.timestamp

def newprinttask():
    #生成作业
    num=random.randrange(1,181)
    if num==180:
        return  True
    else:
        return  False

#numseconds表示模拟打印时间设定,pagesperminute表示打印速度
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()
    averagewaite=sum(waitingtimes)/len(waitingtimes)
    print('打印平均时间:%s,等待打印数:%s'% (averagewaite,printqueue.size()))

#模拟10次
for i in range(10):
    simulation(3600,5)

  

 

posted on 2020-03-20 23:22  hcy12  阅读(119)  评论(0编辑  收藏  举报

导航