## Answers

import random

import time

class Node:

def __init__(self, data):

self.data = data

self.next = None

class Queue:

def __init__(self):

self.head = None

self.last = None

def enqueue(self, data):

if self.last is None:

self.head = Node(data)

self.last = self.head

else:

self.last.next = Node(data)

self.last = self.last.next

def dequeue(self):

if self.head is None:

return None

else:

to_return = self.head.data

self.head = self.head.next

return to_return

def getdata(self):

temp = self.head

Data = list()

while temp is not None:

Data.append(temp.data)

# print(temp.data)

temp = temp.next

return Data

def sizequeue(self):

temp = self.head

count = 0

while temp is not None:

count = count + 1

temp = temp.next

return count

def top(self):

return self.head.data

American = Queue()

Delta = Queue()

Southwest = Queue()

Runway = Queue()

American.enqueue(["American",127,"DCA",10])

American.enqueue(["American",322,"BUF",11])

American.enqueue(["American",233,"FLL",12])

American.enqueue(["American",742,"LAX",13])

American.enqueue(["American",112,"CAE",14])

American.enqueue(["American",437,"LGA",15])

Delta.enqueue(["Delta",221,"SFO",20])

Delta.enqueue(["Delta",348,"DET",21])

Delta.enqueue(["Delta",765,"CVG",22])

Delta.enqueue(["Delta",612,"SAN",23])

Delta.enqueue(["Delta",148,"FLL",24])

Southwest.enqueue(["Southwest",345,"LGA",40])

Southwest.enqueue(["Southwest",657,"PHL",41])

Southwest.enqueue(["Southwest",211,"BOS",42])

Southwest.enqueue(["Southwest",324,"SFO",43])

Southwest.enqueue(["Southwest",367,"SAN",44])

Southwest.enqueue(["Southwest",311,"LAX",45])

Southwest.enqueue(["Southwest",375,"FLL",46])

def Every2sec():

x = random.random()

Asize = American.sizequeue()

Dsize = Delta.sizequeue()

Ssize = Southwest.sizequeue()

if Asize == 0 and Dsize == 0 and Ssize == 0:

return False

if Asize and Dsize and Ssize:

if x >= 0 and x <= .33:

a = American.dequeue()

elif x > .33 and x <= .67:

a = Delta.dequeue()

else:

a = Southwest.dequeue()

else:

if Asize and Dsize:

if x >= 0 and x <= .5:

a = American.dequeue()

else:

a = Delta.dequeue()

elif Dsize and Ssize:

if x >= 0 and x <= .5:

a = Delta.dequeue()

else:

a = Southwest.dequeue()

elif Asize and Ssize:

if x >= 0 and x <= .5:

a = American.dequeue()

else:

a = Southwest.dequeue()

else:

if Asize:

a = American.dequeue()

elif Dsize:

a = Delta.dequeue()

else:

a = Southwest.dequeue()

print("Flight " + str(a[1]) + " of " + str(a[0]) + " airlines is added to Runway Queue ")

Runway.enqueue(a)

def Every4sec_takeoff():

y = random.random()

if y >= 0 and y <= .5:

a = Runway.dequeue()

print("Flight " + str(a[1]) + " of " + str(a[0]) + " airlines took off")

else:

a = Runway.top()

print("Flight " + str(a[1]) + " of " + str(a[0]) + " airlines is waiting")

if Runway.sizequeue() == 0:

print("All flights have taken off")

return True

Data = Runway.getdata()

print()

print("List of all flight currently in Runway queue")

print("###################################")

print("Airlines","Flight#","Destination","Gate#")

for i in Data:

print(str(i[0]) + " " + str(i[1]) + " " + str(i[2])+ " " +str(i[3]))

print("-----------------------------------")

return False

def Every4sec_cancel():

z = random.randint(1,100) / 100

a = None

if z >= 0 and z <= .1:

if American.sizequeue() == 0:

print("American airline queue Empty, no need to cancel a flight")

else:

a = American.dequeue()

if z >= .50 and z <= .60:

if Delta.sizequeue() == 0:

print("Delta airline queue Empty, no need to cancel a flight")

else:

a = Delta.dequeue()

if z >= .80 and z <= .90:

if Southwest.sizequeue() == 0:

print("Southwest airline queue Empty, no need to cancel a flight")

else:

a = Southwest.dequeue()

if a is not None:

print(str(a[0]) + " airlines flight no " + str(a[1]) + " travelling to " + str(a[2]) + " is cancelled")

while True:

time.sleep(2)

Every2sec()

time.sleep(2)

Every2sec()

if Every4sec_takeoff() == True:

break;

Every4sec_cancel()