Lab3
Lab3
Definition
A Stack follows the Last In, First Out (LIFO) principle.
Operations:
# Create a Stack
def create_stack():
stack = []
return stack
# Pop Operation
def pop(stack):
if check_empty(stack):
return "stack is empty"
return stack.pop()
Output:
pushed item: 1
pushed item: 2
pushed item: 3
pushed item: 4
----------------------
popped item: 4
stack after popping an element: ['1', '2', '3']
Explanation
1. create_stack() :
2. check_empty(stack) :
3. push(stack, item) :
Adds the item to the top of the stack using the append() method.
4. pop(stack) :
import queue
Output:
popped item: 9
popped item: 1
popped item: 18
popped item: 5
popped item: 10
Explanation
2. put(item) :
3. get() :
Definition
A Queue follows the First In, First Out (FIFO) principle.
Operations:
# Create a Queue
def create_queue():
queue = []
return queue
# Enqueue Operation
def push(queue, item):
queue.append(item)
print("pushed item: " + item)
Output:
pushed item: 1
pushed item: 2
pushed item: 3
pushed item: 4
----------------------
popped item: 1
queue after popping an element: ['2', '3', '4']
Explanation
1. create_queue() :
2. check_empty(queue) :
3. push(queue, item) :
Adds the item to the end of the queue using the append() method.
import queue
# Enqueue items
queue2.put(10)
queue2.put(5)
queue2.put(18)
queue2.put(1)
queue2.put(9)
# Dequeue items
while not queue2.empty():
print("popped item: ", queue2.get())
Explanation
1. Queue :
2. put(item) :
3. get() :
Priority Queue
class PriorityQueue(object):
def __init__(self):
self.queue = []
def isEmpty(self):
return len(self.queue) == 0
def delete(self):
try:
max = 0
for i in range(len(self.queue)):
if self.queue[i] > self.queue[max]:
max = i
item = self.queue[max]
del self.queue[max]
return item
except IndexError:
print()
exit()
Output:
popped item: 14
popped item: 12
popped item: 7
popped item: 1
Explanation
1. insert(data) :
2. delete() :
import queue
# Dequeue items
while not pQueue2.empty():
print("popped item: ", pQueue2.get())
Output:
popped item: (1, 10)
popped item: (2, 18)
Explanation
1. PriorityQueue :
2. put((priority, value)) :
3. get() :
Removes the item with the highest priority (lowest priority value).
Lab Solution
Data Structures Exercises
Objective:
Analyze the problem.
Problem Description:
1. Read 5 names from the user.
2. Store the names in a data structure to facilitate printing them in reverse order.
3. Requirements:
def create_stack():
stack=[]
return stack
def check_empty(stack):
return len(stack)==0
def push(stack,item):
stack.append(item)
print(f'pushed item: {item}')
def pop(stack):
if check_empty(stack):
return 'stack is empty'
return stack.pop()
stack=create_stack()
for i in range(1,6):
name=input(f'enter name {i}: ')
push(stack,name)
print(pop(stack))
print(pop(stack))
print(pop(stack))
print(pop(stack))
print(pop(stack))
Objective:
Analyze the problem.
Problem Description:
1. Read 5 names from the user.
3. Requirements:
import queue
Pq=queue.PriorityQueue()
Objective:
Analyze the problem.
Problem Description:
Develop a class called DeliveryOrders with the following attributes and methods:
Methods:
1. scheduleDelivery() :
Adds the delivery to the schedule and its type to the list.
2. completeDelivery() :
Removes the oldest delivery and its type from the schedule.
3. report() :
class DeliveryOrders:
def __init__(self):
self._scheduled_orders = queue.Queue()
self._delivery_types = []
self._completed_orders_count = 0
def report(self):
print("Report:")
print(f"A total of {self._scheduled_orders.qsize()} are
print(f"A total of {self._completed_orders_count} are co
print("-" * 50)
orders = DeliveryOrders()
orders.report()
orders.completeDelivery()
orders.completeDelivery()
orders.report()