diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-02-23 13:34:38 -0500 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-02-23 15:00:17 -0500 |
commit | c38089c8246d25f64557de6cf2542b48f91ace4c (patch) | |
tree | 3369408dbb6b242b6e4230304c038084fe8ddcf2 | |
parent | 4165ace67ae82def6850f589310a3aa82a755c60 (diff) |
Add swap to double ended queue
-rw-r--r-- | collections/double_ended_queue.c | 22 | ||||
-rw-r--r-- | collections/double_ended_queue.h | 2 |
2 files changed, 23 insertions, 1 deletions
diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index ee93196..6a3a810 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -159,3 +159,25 @@ deq *root; copy->beg = copy->base; copy->end = copy->base + deq_size(root); } + +void deq_swap(root, i, j) +deq *root; +int i, j; +{ + int len; + void *tmp; + + if (!root) { + return; + } + + len = deq_size(root); + + if (i >= len || j >= len) { + return; + } + + tmp = root->end[i]; + root->end[j] = root->end[i]; + root->end[i] = tmp; +} diff --git a/collections/double_ended_queue.h b/collections/double_ended_queue.h index a5645e2..d1d687a 100644 --- a/collections/double_ended_queue.h +++ b/collections/double_ended_queue.h @@ -19,10 +19,10 @@ void* deq_pop_front(deq*); void* deq_index(deq*, int); void* deq_pop_back(deq*); +void deq_swap(deq*, int, int); /* - * swap * resevee * truncate * front |