Implement queue_push for a fixed-capacity circular queue. Return 0 when the queue is full or invalid; otherwise store the value, advance tail with wraparound, and increase size.
Requirements
Never overwrite when full
Require size <= capacity
Handle capacity 0 safely
Example
Input
capacity 3, tail 2, size 2
Expected output
writes at 2 and wraps tail to 0
Show a hint
Write down the input contract and the failure cases before coding.
local runtime
RUNTIME OUTPUT
Use your C runtime locally, then compare with solution.c.
Use your local runtime, then review the reference solution
LEARN FROM THE SOLUTION
Why the solution works.
The size field distinguishes a full queue from an empty queue even when head and tail share an index. Writing before advancing tail keeps the invariant easy to inspect, and modulo capacity wraps the next insertion back to the first slot.
What this exercise teaches
Struct invariants
Modulo arithmetic
Bounded storage
A PRACTICAL PLAN
Work through Push into a bounded ring queue with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Push into a bounded ring queue” teach?
This advanced C exercise focuses on Struct invariants, Modulo arithmetic, Bounded storage. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Write and compile the starter in your local C environment, exercise the example and edge cases, then compare your approach with solution.c.
When should I open the reference solution?
Attempt Push into a bounded ring queue first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.