diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-05-30 20:11:45 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-05-30 20:11:45 -0400 |
commit | 10cd9eadf3f14d4b25ef85ea563938a784fcf20c (patch) | |
tree | b43791b9c1b3917340242ce30fc55991041d927c | |
parent | 4e4704b0251bb2b03d0fa573437b77b15567441c (diff) |
Add deq_clear function for double ended queue.
Frees all elements in double ended queue, does not free the queue struct
itself (deq_free is used for this).
-rw-r--r-- | collections/double_ended_queue.c | 14 | ||||
-rw-r--r-- | collections/double_ended_queue.h | 8 |
2 files changed, 22 insertions, 0 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index 956e362..7938307 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -184,6 +184,9 @@ deq *root; return root->base[root->end]; } +/* Note: Elements are not freed + * deq_clear should be called before if they are no longer needed. + */ void deq_free(root) deq *root; { @@ -193,6 +196,17 @@ deq *root; free(root); } +void deq_clear(root) +deq *root; +{ + int i, size; + + size = deq_size(root); + for (i = 0; i < size; i++) { + free(deq_index(root, i)); + } +} + void deq_print(root, to_string) deq *root; char* to_string(void*); diff --git a/collections/double_ended_queue.h b/collections/double_ended_queue.h index 226918d..7f11f66 100644 --- a/collections/double_ended_queue.h +++ b/collections/double_ended_queue.h @@ -11,8 +11,14 @@ deq* deq_with_capacity(int); int deq_size(deq*); int deq_capacity(deq*); deq* deq_cp(deq*); + +/* Note: Elements are not freed + * deq_clear should be called before if they are no longer needed.*/ void deq_free(deq*); +/*Free all elements within queue*/ +void deq_clear() + /*data*/ void deq_push_back(deq*, void*); void* deq_pop_front(deq*); @@ -20,6 +26,8 @@ void* deq_index(deq*, int); void* deq_pop_back(deq*); void deq_swap(deq*, int, int); + +/*Note: Does not currently reduce memory footprint*/ void deq_truncate(deq*, int); void* deq_front(deq*); |