Skip to content

Commit

Permalink
Add function queue_push_begin
Browse files Browse the repository at this point in the history
  • Loading branch information
elyorbek010 committed Jan 27, 2023
1 parent a54a1be commit 3140f68
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
27 changes: 26 additions & 1 deletion queueNew/cqueue.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#include "cqueue.h"

//wrap fprintf messages into MACROS
static inline size_t queue_next_index(const size_t index, const size_t capacity) {
return (index + 1) % (capacity + 1);
}

static inline size_t queue_prev_index(const size_t index, const size_t capacity) {
if (index == 0) {
return capacity;
}
else {
return index - 1;
}
}

struct cqueue_s
{
size_t capacity;
Expand Down Expand Up @@ -51,6 +60,22 @@ cqueue_ret_t queue_push_end(cqueue_t* cqueue, int element)
return CQUEUE_SUCCESS;
}

cqueue_ret_t queue_push_begin(cqueue_t* cqueue, int element) {
if (cqueue == NULL) {
fprintf(stderr, "Queue does not exist");
return CQUEUE_FAILURE;
}

cqueue->begin = queue_prev_index(cqueue->begin, cqueue->capacity);
cqueue->element[cqueue->begin] = element;

if (cqueue->begin == cqueue->end) {
cqueue->end = queue_prev_index(cqueue->end, cqueue->capacity);
return CQUEUE_OVERFLOW;
}
return CQUEUE_SUCCESS;
}

cqueue_ret_t queue_pop_begin(cqueue_t* cqueue, int* p_element) {
if (cqueue == NULL || p_element == NULL) {
fprintf(stderr, "argument is null");
Expand Down
14 changes: 11 additions & 3 deletions queueNew/cqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ void queue_destroy(cqueue_t* cqueue);
* Adds an element to the cqueue.
*
* If the cqueue is full, CQUEUE_OVERFLOW is returned
* and `element` is written in place of the last element.
* and `element` is written in place of the first element.
*/
cqueue_ret_t queue_push_end(cqueue_t* cqueue, int element);

/**
* Adds an element to the cqueue.
*
* If the cqueue is full, CQUEUE_OVERFLOW is returned
* and `element` is written in place of the last element.
*/
cqueue_ret_t queue_push_begin(cqueue_t* cqueue, int element);

/**
* Removes an element from the cqueue.
*/
Expand All @@ -48,12 +56,12 @@ cqueue_ret_t queue_pop_begin(cqueue_t* cqueue, int* p_element);
cqueue_ret_t queue_peek(const cqueue_t* cqueue, int* p_element);

/**
* Returns true if queue is full, false otherwise
* Returns CQUEUE_FULL if queue is full, CQUEUE_NOT_FULL otherwise
*/
cqueue_ret_t queue_is_full(const cqueue_t* cqueue);

/**
* Returns true if queue is empty, false otherwise
* Returns CQUEUE_EMPTY if queue is empty, CQUEUE_NOT_EMPTY otherwise
*/
cqueue_ret_t queue_is_empty(const cqueue_t* cqueue);

Expand Down
17 changes: 10 additions & 7 deletions testNew/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ TEST(isEmpty, FullQueue) {

queue_destroy(queue);
}

/*
TEST(EmptyQueue, PopEnd) {
cqueue_t* queue = NULL;
Expand Down Expand Up @@ -409,6 +409,7 @@ TEST(OverflowQueue, PopEnd) {
queue_destroy(queue);
}
*/

TEST(EmptyQueue, pushBegin) {
cqueue_t* queue = NULL;
Expand All @@ -417,7 +418,7 @@ TEST(EmptyQueue, pushBegin) {
queue = queue_create(3);
EXPECT_EQ(queue_push_begin(queue, 101), CQUEUE_SUCCESS);
EXPECT_EQ(queue_peek(queue, &val), CQUEUE_SUCCESS);
EXPECT_EQ(queue_pop_end(queue, &val), CQUEUE_SUCCESS);
EXPECT_EQ(queue_pop_begin(queue, &val), CQUEUE_SUCCESS);

queue_destroy(queue);
}
Expand All @@ -431,12 +432,14 @@ TEST(FullQueue, pushBegin) {
ASSERT_EQ(queue_push_end(queue, 202), CQUEUE_SUCCESS);
ASSERT_EQ(queue_push_end(queue, 303), CQUEUE_SUCCESS);
EXPECT_EQ(queue_push_begin(queue, 404), CQUEUE_OVERFLOW);
EXPECT_EQ(queue_pop_end(queue, &val), 202);
EXPECT_EQ(queue_pop_begin(queue, &val), CQUEUE_SUCCESS);
EXPECT_EQ(val, 404);

queue_destroy(queue);
}

TEST(EmptyQueue, popBegin) {
/*
TEST(EmptyQueue, popEnd) {
cqueue_t* queue = NULL;
queue = queue_create(5);
Expand All @@ -446,7 +449,7 @@ TEST(EmptyQueue, popBegin) {
queue_destroy(queue);
}
TEST(FullQueue, popBegin) {
TEST(FullQueue, popEnd) {
cqueue_t* queue = NULL;
int val;
Expand All @@ -455,7 +458,7 @@ TEST(FullQueue, popBegin) {
ASSERT_EQ(queue_push_end(queue, 202), CQUEUE_SUCCESS);
ASSERT_EQ(queue_push_end(queue, 303), CQUEUE_SUCCESS);
EXPECT_EQ(queue_push_begin(queue, 404), CQUEUE_OVERFLOW);
EXPECT_EQ(queue_pop_begin(queue, &val), 404);
EXPECT_EQ(queue_pop_end(queue, &val), 202);
queue_destroy(queue);
}
}*/

0 comments on commit 3140f68

Please sign in to comment.