ISRO Computer Science (CSE) 2014 - Question 36

All ISRO Computer Science (CSE) papers | ISRO Computer Science (CSE) 2014

Consider a standard Circular queue implementation (which has the same condition for queue full and queue empty) whose size is 11 and the elements of the queue are $q[0], q[1], ..., q[10]$. The front and rear pointers are initialized to point at $q[2]$. In which position will the ninth element be added?

  • A. $q[0]$
  • B. $q[1]$
  • C. $q[9]$
  • D. $q[10]$

Answer: D. $q[10]$

Explanation: Size 11. Initial F=R=2.\nAdd 1st: R=3.\nAdd 2nd: R=4.\n...\nAdd 9th: R = (2 + 9) % 11? \nInitial state is empty? If F=R=2, usually empty.\n1st elem at q[2]? Or q[3]? Usually Rear points to last inserted or next free. If next free: \n1st at 2, R becomes 3.\n9th at index $(2 + 8) = 10$? \nSequence of indices: 2, 3, 4, 5, 6, 7, 8, 9, 10. \n9th element is at index 10.\nWait, if R points to occupied, increment first. \nLet's assume R points to last element. Start empty F=R=2. \nInsert 1: R=3, q[3]. (1st element)\nInsert 9: q[3+8] = q[11]? Wrap.\nLet's use R points to next free. \n1st -> q[2], R=3.\n9th -> q[2+8] = q[10], R=11->0.\nSo q[10].

| | All questions of ISRO Computer Science (CSE) 2014