From b93a7b87b55a42ca864325ae91328a5627885471 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 30 May 2020 20:35:05 -0400 Subject: Add remove index function for double ended queue. --- collections/double_ended_queue.c | 26 ++++++++++++++++++++++++++ collections/double_ended_queue.h | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index 7938307..0348cfa 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -184,6 +184,32 @@ deq *root; return root->base[root->end]; } +void deq_remove(root, index) +deq *root; +int index; +{ + if (!root || index > root->limit + || (index >= root->end && index < root->beg)) + return; + + index = (root->beg + index) % root->limit; + if (index >= root->end) + return NULL; + + root->base[index] = NULL; + + if (root->beg < root->end || index >= root->beg) { + root->beg = memmove(root->base + root->beg + 1, + root->base + root->beg, + (index - root->beg) * sizeof(void*)); + ++root->beg; + } else { + memmove(root->base + index, root->base + index + 1, + (--root->end - index) * sizeof(void*)); + } + +} + /* Note: Elements are not freed * deq_clear should be called before if they are no longer needed. */ diff --git a/collections/double_ended_queue.h b/collections/double_ended_queue.h index 7f11f66..bfc9d20 100644 --- a/collections/double_ended_queue.h +++ b/collections/double_ended_queue.h @@ -33,12 +33,13 @@ void deq_truncate(deq*, int); void* deq_front(deq*); void* deq_back(deq*); +void remove(deq*, int); + /* * resevee * push back * swap_rm_front/back * insert - * remove */ #endif -- cgit v1.1