Data Structures · Abstract data types, representation, and invariants
A ring buffer (circular array) of fixed capacity C≥ 1 stores a sequence of at most C…
Problem
A ring buffer (circular array) of fixed capacity \(C\ge 1\) stores a sequence of at most \(C\) elements using an underlying array \(B[0..C-1]\) together with two integers `head` and `size`, where \(0\le\text{head}<C\) and \(0\le\text{size}\le C\). The sequence in logical order is \[ B[\text{head}],\ B[(\text{head}+1)\bmod C],\ \ldots,\ B[(\text{head}+\text{size}-1)\bmod C]. \] Design procedures `ENQUEUE-BACK(x)`, `DEQUEUE-FRONT()`, and `PEEK(i)` (\(0\le i<\text{size}\)) for this representation. (a) Write pseudocode for the three procedures. `ENQUEUE-BACK` must fail (and leave the structure unchanged) if \(\text{size}=C\). `DEQUEUE-FRONT` must fail if \(\text{size}=0\). `PEEK` must return the element at logical index \(i\) without mutating the buffer. (b) State the representation invariant relating \(B\), `head`, `size`, and \(C\). (c) For each procedure, give a worst-case \(\Theta\) bound in terms of \(C\) and `size`, and argue that the bound does not hide a scan of the buffer.
Hint
The live data occupy a wrap-around interval of length `size` starting at `head`; the back of that interval is determined by those two integers.
Check your work
Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.
Create a free account to check your answer and see the solution. Create a free account.
More Data Structures practice problems
- Let V be a dynamic array of n elements stored in a buffer of capacity c, with the…Abstract data types, representation, and invariants
- Let L be the doubly linked list with values [10, 20, 30, 40, 50], and let I be a…Arrays, linked structures, stacks, queues, and amortization
- Consider BST deletion of a node zSearch trees and balanced trees
- Let T be a BST whose nodes contain only key, left, and right fields (no parent…Search trees and balanced trees
- Consider a hash table T of size m = 11 that resolves collisions by chainingHash tables and probabilistic performance
- Throw n balls independently and uniformly into n binsHash tables and probabilistic performance
- `Bubble-Down`(A,i,n) is specified as follows on a 1-based array that is a complete…Heaps, priority queues, and disjoint sets
- The fat-node method of partial persistence stores, in each field of each node, a…Graph representations and traversals
- A duplicate minimum must sit on the min-stack, or one pop forgets the otherArrays, linked structures, stacks, queues, and amortization