Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Model Question Paper (as circulated)Part B · Q11b4 marksCO2Module 1asked in 2 papers

Question 11b

Write algorithms to insert and delete elements from a double ended queue.

Topics: Double-ended queues

Watch this exact question run

Loading visualizer…

Solution

Assumptions:
  • Circular array; front = rear = −1 when empty; full when (rear + 1) mod MAX = front.
algorithm
InsertFront(x): if full → overflow; if empty front = rear = 0 else front = (front − 1 + MAX) mod MAX; dq[front] = x
InsertRear(x):  if full → overflow; if empty front = rear = 0 else rear = (rear + 1) mod MAX;      dq[rear] = x
DeleteFront():  if empty → underflow; item = dq[front]; if front = rear → front = rear = −1 else front = (front + 1) mod MAX
DeleteRear():   if empty → underflow; item = dq[rear];  if front = rear → front = rear = −1 else rear = (rear − 1 + MAX) mod MAX
Example: MAX = 5checked by code
OperationResultfront, rearArray (index 0 … 4)Front → rear
insertRear(10)ok0, 0[10 _ _ _ _]10
insertRear(20)ok0, 1[10 20 _ _ _]10 20
insertFront(5)ok4, 1[10 20 _ _ 5]5 10 20
insertFront(1)ok3, 1[10 20 _ 1 5]1 5 10 20
insertRear(30)ok3, 2[10 20 30 1 5]1 5 10 20 30
insertFront(0)OVERFLOW3, 2[10 20 30 1 5]1 5 10 20 30
deleteFrontremoved 14, 2[10 20 30 _ 5]5 10 20 30
deleteRearremoved 304, 1[10 20 _ _ 5]5 10 20
deleteRearremoved 204, 0[10 _ _ _ 5]5 10
deleteFrontremoved 50, 0[10 _ _ _ _]10

MAX = 5. “_” marks a slot that holds no live element.

How to write it in the exam

Write the four procedures above, each with its overflow or underflow check. Mention that the index arithmetic wraps around with mod MAX, so every operation is O(1).

How this answer was verified
  • tests/linear-expression.test.ts
  • deque_array.c matches the engine on 200 random cases

Similar questions