#NEEDS TESTING THOROUGHLY! #implementation of a list as a dynamic data structure using Python list data type #List stores jobs and priorities in a priority queue #add a new element to the correct place in jobs queue #behind other jobs of the same priority def add(newJob): print("\nnewJob",newJob) if len(jobs)==0: jobs.insert(0,newJob) elif newJob[1] == "Low": jobs.insert(0,newJob) elif newJob[1] == "High": p = len(jobs)-1 exitLoop = False while ((jobs[p][1] == "High") and exitLoop == False): print("p in High loop =",p, exitLoop, "jobs[p][1] = ",jobs[p][1]) if p > 0: p = p - 1 else: exitLoop = True #endwhile if p == 0 and jobs[p][1] == "High": p = p-1 jobs.insert(p+1,newJob) else: #priority "Normal" p = len(jobs)-1 exitLoop = False while (jobs[p][1] == "High" or jobs[p][1] == "Normal") and exitLoop == False: if p > 0: p = p - 1 else: exitLoop = True #endwhile if p == 0 and jobs[p][1] == "Normal": p = p-1 jobs.insert(p+1,newJob) #endif #end add #main #jobs = [["D","Low"],["SS","Normal"],["WP","Normal"],["G","High"]] jobs = [] # add new job "D", priority High newJob = ["Job5","High"] print(jobs) add(newJob) print (jobs) newJob = ["Job6", "Low"] add(newJob) print (jobs) newJob = ["Job7","High"] add(newJob) print(jobs) newJob = ["Job8","Normal"] add(newJob) print(jobs) print() #dequeue all jobs p = len(jobs) print("Now dequeue") for i in range(p): jobs.pop() print(jobs)