@@ -123,3 +123,78 @@ int32_t staticQueueClear(staticQueue_t* queue)
123123 queue -> tail = queue -> first_item ;
124124 return STATIC_QUEUE_SUCCESS ;
125125}
126+
127+ int32_t staticQueueErase (staticQueue_t * queue , staticQueueItem_t * item )
128+ {
129+ // Check if the item is active
130+ if (!item -> active ) {
131+ return STATIC_QUEUE_EMPTY ;
132+ }
133+
134+ // Mark the item as inactive
135+ item -> active = false;
136+
137+ // Special case: if this was the only item in the queue
138+ if (queue -> tail == queue -> head -> last ) {
139+ // Queue is now empty, reset pointers
140+ queue -> head = queue -> first_item ;
141+ queue -> tail = queue -> first_item ;
142+ return STATIC_QUEUE_SUCCESS ;
143+ }
144+
145+ // If erasing the tail item (oldest item), just move tail to next
146+ if (item == queue -> tail ) {
147+ queue -> tail = queue -> tail -> next ;
148+
149+ // Skip over any remaining inactive items at tail
150+ while (queue -> tail != queue -> head && !queue -> tail -> active ) {
151+ queue -> tail = queue -> tail -> next ;
152+ }
153+
154+ // Check if tail caught up to head with exactly one active item
155+ if (queue -> tail == queue -> head && queue -> tail -> active ) {
156+ // Move head forward to maintain tail != head invariant for single item
157+ queue -> head = queue -> head -> next ;
158+ }
159+
160+ return STATIC_QUEUE_SUCCESS ;
161+ }
162+
163+ // If erasing the item just before head (newest item), move head backward
164+ if (item -> next == queue -> head ) {
165+ queue -> head = item ;
166+
167+ // Check if head caught up to tail with exactly one active item
168+ if (queue -> tail == queue -> head && queue -> tail -> active ) {
169+ // Move head forward to maintain tail != head invariant for single item
170+ queue -> head = queue -> head -> next ;
171+ }
172+
173+ return STATIC_QUEUE_SUCCESS ;
174+ }
175+
176+ // For items in the middle: remove from current position and relink immediately before tail
177+
178+ // Step 1: Remove item from its current position in the list
179+ staticQueueItem_t * prev_item = item -> last ;
180+ staticQueueItem_t * next_item = item -> next ;
181+
182+ prev_item -> next = next_item ;
183+ next_item -> last = prev_item ;
184+
185+ // Step 2: Reinsert the item immediately before tail
186+ staticQueueItem_t * before_tail = queue -> tail -> last ;
187+
188+ // Insert: before_tail <-> item <-> tail
189+ before_tail -> next = item ;
190+ item -> last = before_tail ;
191+ item -> next = queue -> tail ;
192+ queue -> tail -> last = item ;
193+
194+ // Step 3: If queue was full, move head to point to the erased item (now inactive)
195+ if (queue -> head == queue -> tail && queue -> head -> active ) {
196+ queue -> head = item ;
197+ }
198+
199+ return STATIC_QUEUE_SUCCESS ;
200+ }
0 commit comments