This youtube video describes a Stack:https://youtu.be/XSdXSmwb550 Key video points:
Last in first out (LIFO) order of data access. Standard stack operations include: Push, Pop, Top, isEmpty, Size.
Examples of stacks: Pez dispenser, Cafeteria Tray dispenser
This youtube video describes a Queue:https://youtu.be/PjQdvpWfCmE Key video points:
First in first out (FIFO) order of data access. Standard queue operations include: Enqueue, Dequeue, Front, Back, isEmpty, Size.
Examples of queues: Waiting in a queue for an amusement park ride
This youtube video describes a Priority Queue:https://youtu.be/wptevk0bshY Key video points:
Examples of priority queues: VIP Waiting in a queue for an amusement park ride goes to the front, Triage in hospital emergency.
Another method of creating a priority queue object can be found at the website www.geeksforgeeks https://www.geeksforgeeks.org/priority-queue-in-python/ an implementation of this method of priority queue standard operations is defined in Trinket here: https://trinket.io/python3/8a97ea58d2
See the examples of alternate methods of implementing a priority queue in Python3 below.
This youtube video describes a dictionary (associative array): https://youtu.be/5qXvj56dmmw Key video points:
Key, value pairings
An example of a dictionary is the contacts on mobile phone which has key=name, value=person details.Introductory online MathPlanet Python Course to set up data structures Data types (Programming, To remember) – Mathplanet
Example 1: Python3 method for implementing a Priority Queue using a dictionary implemented in Python3/Trinket
# Python3/Trinket
# Implementing Priority Queue in Python3 using dictionary - copy and paste this example into Python/Trinket
from operator import itemgetter
#itemgetter helper function allows values in dictionary to be used for ranking
# Implementing a priority Queue by
# using a dictionary with key=name, value=rank
# rank in this example Nations statistics of % Urban Population
NationRankDict = { 'India': 36, 'China': 65, 'US': 83, 'Indonesia': 59,
'Pakistan': 35, 'Nigeria': 54} # use a dictionary as a PQ
newNation={'Brazil':88} # add a new Nation with own % Urban Pop priority
NationRankDict.update(newNation)
# Minimum Priority Queue by urban population %
minPQ = sorted(NationRankDict.items(), key=itemgetter(1)) # sort by value 1 in dictionary
print('=======minPQ is ==> ', minPQ)
print()
# Maximum Priority Queue by urban population %
maxPQ = sorted(NationRankDict.items(), key=itemgetter(1), reverse = True) # sort by reverse value 1 in dictionary
print('=======maxPQ is ==> ', maxPQ)
Example 2: Python3 method for implementing a Priority Queue in Python3/Trinket using the queue library of helper functions can be imported - from queue import PriorityQueue
# Python3/Trinket
# Implementing Priority Queue in Python3 using - from queue import PriorityQueue copy and paste this example into Python/Trinket
from queue import PriorityQueue
nationPQ = PriorityQueue() # create the priority queue
# enqueue items with ranking and details
nationPQ.put((36, "India"))
nationPQ.put((65, "China"))
nationPQ.put((54, "Nigeria"))
nationPQ.put((88, "Brazil"))
# minimum priority queue is dequeud in the default order
for i in range(nationPQ.qsize()):
print(nationPQ.get())
# maximum priority queue not supported an implementation needs more work
# NOTE: can multiply rankings by -1 to reverse the order or other method