PRIP 10.2 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 10.2 Sumita Arora Solutions for class 12 Computer Science. To view chapter 10 conceptual videos, visit here.

To view Sumita Arora solutions for all chapters, visit here.

Q.1: After the following operations have all been completed: draw the picture of the resulting queue.

(a) Q.Enqueue(20)

(b) Q.Enqueue(51)

(c) Q.Enqueue(31)
Answer: [ 20 | 51 | 31 ]

(d) Q.Enqueue(71)
Answer: [ 20 | 51 | 31 | 71 ]

(e) Q.Dequeue()
Answer: [ 51 | 31 | 71 ]

(f) Q.Enqueue(43)
Answer: [ 51 | 31 | 71 | 43]

(g) Q.Dequeue()
Answer: [ 31 | 71 | 43]

Q.2: Determine whether each of the following characteristics apply to a stack ( S), a queue(Q) both (B), or neither (N):

(a) An element is inserted at a special place called the top.

(b) An element is inserted at a special place called the rear.

(c) An element is deleted at the front.

(d) The ith position may be deleted.

(e) An element is deleted at the top.

(f)The structure is a LIFO structure.

(g) The structure is a FIFO structure.

(h) The structure is a restricted access structure.

Q.3: Write a program to implement a queue of order numbers in a restaurant. Display a menn as shown below:
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit

• When option 1 is chosen, generate an order number of three digits, randomly and add it to your queue
• For option 2, display the orders that are in queue.
• For option 3, dequeue an order number from the queue and flash it like:
Order number <orderno> is ready e.g.,

Code:
import random

def generate_order(order_queue):
order_no = random.randint(100,999)
order_queue.append(order_no)    # enqueue operation using listas queue

def waiting_orders(order_queue):
print("orders in waiting list:")
for i in order_queue:
print(i,end=", ")

def dequeue_order(order_queue):
front = order_queue.pop(0) # deque operation using list as queue

#__main__
order_queue = []  #create empty stack
choice = -1   # innitially set choice to -1
while(choice!=0):
print()
print("---------------")
print("YUMMY PROGRAM")
print("1. Order a meal")
print("2. Waiting Queue")
print("4. Exit")
print("---------------")

choice = input("Enter your choice: ")
if(choice == "1"):
generate_order(order_queue)
if(choice == "2"):
waiting_orders(order_queue)
if(choice == "3"):
dequeue_order(order_queue)
if(choice == "4"):
print("program exiting...")
choice = 0
print("program ended")

Output:
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------
orders in waiting list:
956, 111, 530,
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------
orders in waiting list:
111, 530, 541,
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
4. Exit
---------------
program exiting...
program ended
4. A stack S stores some integer values. Write a program that displays the integers from the stack S in sorted order with the help of a dequeue.
Use output restricted deque D to accomplish this task as per following algorithm:
pop from stack, call it anum
if deque empty add to deque
if deque not empty
if anum < front-most of deque
add enum in beginning of deque
otherwise
add anum in the end of deque
repeat above steps for all elements of stack
finally display the deque

• Algorithm in question is not perfect
• Instead of one, two dequeues are used
Code:
from collections import deque  # for this program we are using deque from collections module

def isEmpty(temp):
if len(temp)==0:
return True
return False

def sortUsingQueue(stack):
deq = deque()
while(not isEmpty(stack)):
anum = stack.pop()
if(isEmpty(deq)):
deq.append(anum)  # append element at rear
else:
if anum < deq:  # compare with front element
deq.appendleft(anum)  # enque at the front
else:
if(deq[-1] < anum):
deq.append(anum) # enque at the rear
else:
temp = deque()  # create a temporary deque
while(deq[-1] > anum):
temp.appendleft(deq.pop()) # add every greater element from left of deque
deq.append(anum)               # when while loop ends append anum
deq.extend(temp)               # extend temp deque to original deque

print(deq)       # print deq status with every iterations
return deq

#__main__

stack = [10, 20, 54, 5, 63, 40, 37, 88]
deq = sortUsingQueue(stack)
print("Sorted list : ")

for i in deq:
print(i,end = " ")

Output:
deque()
deque([37, 88])
deque([37, 40, 88])
deque([37, 40, 63, 88])
deque([5, 37, 40, 63, 88])
deque([5, 37, 40, 54, 63, 88])
deque([5, 20, 37, 40, 54, 63, 88])
deque([5, 10, 20, 37, 40, 54, 63, 88])
Sorted list :
5 10 20 37 40 54 63 88

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of 