diff --git a/src/structures/queue.h b/src/structures/queue.h index cda8b082f41474bdad17cc7e5bb13f21b0e2a117..e1c4cda153202de7fce93047e1018fd116436706 100644 --- a/src/structures/queue.h +++ b/src/structures/queue.h @@ -28,6 +28,9 @@ void cry_queue_free(struct CryQueue* queue, cry_free_funptr handler) int cry_queue_size(struct CryQueue* queue); pointer cry_queue_front(struct CryQueue* queue); +pointer cry_queue_rear(struct CryQueue* queue); +pointer cry_queue_nth(struct CryQueue* queue, size_t index); + pointer cry_queue_dequeue(struct CryQueue* queue); void cry_queue_enqueue(struct CryQueue* queue, pointer data); diff --git a/src/structures/single_linked_list.c b/src/structures/single_linked_list.c index cd4f1d0e50e5287ae56d02b7279d858b75350cd5..b1a7ed35274d700a3affe80d3827630fd5ebad5d 100644 --- a/src/structures/single_linked_list.c +++ b/src/structures/single_linked_list.c @@ -9,7 +9,7 @@ * Crystal is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. + * Lesser GNU General Public License for more derears. * * You should have received a copy of the Lesser GNU General Public License * along with this program. If not, see . @@ -31,19 +31,19 @@ struct Stack { struct Node* begin; }; -struct Queue { +struct CryQueue { struct Node* front; - struct Node* tail; + struct Node* rear; }; struct CryQueue* cry_queue_new(void) { - struct Queue* list = cry_malloc(struct Queue); + struct CryQueue* list = cry_malloc(struct CryQueue); list->front = 0; - list->tail = 0; - return cry_cast(struct CryQueue*, list); + list->rear = 0; + return list; } @@ -52,7 +52,7 @@ cry_queue_free(struct CryQueue* queue, cry_free_funptr handler) { assert(queue != 0); - struct Node* node = cry_cast(struct Queue*, queue)->front; + struct Node* node = queue->front; struct Node* tmp = 0; while(node != 0) { @@ -76,7 +76,7 @@ cry_queue_size(struct CryQueue* queue) assert(queue != 0); size_t length = 0; - struct Node* node = cry_cast(struct Queue*, queue)->front; + struct Node* node = queue->front; while(node != 0) { node = node->next; @@ -92,10 +92,36 @@ cry_queue_front(struct CryQueue* queue) { assert(queue != 0); - if(cry_cast(struct Queue*, queue)->front == 0) + if(queue->front == 0) return 0; else - return cry_cast(struct Queue*, queue)->front->data; + return queue->front->data; +} + + +pointer +cry_queue_rear(struct CryQueue* queue) +{ + assert(queue != 0); + + if(queue->rear == 0) + return 0; + else + return queue->rear->data; +} + + +pointer +cry_queue_nth(struct CryQueue* queue, size_t index) +{ + assert(queue != 0); + + struct Node* node = queue->front; + + while(node != 0 && index-- > 0) + node = node->next; + + return node; } @@ -104,8 +130,7 @@ cry_queue_dequeue(struct CryQueue* queue) { assert(queue != 0); - struct Queue* list = cry_cast(struct Queue*, queue); - struct Node* front = list->front; + struct Node* front = queue->front; pointer data = 0; if(front == 0) @@ -113,10 +138,10 @@ cry_queue_dequeue(struct CryQueue* queue) data = front->data; - if(list->front == list->tail) - list->front = list->tail = 0; + if(queue->front == queue->rear) + queue->front = queue->rear = 0; else - list->front = front->next; + queue->front = front->next; cry_free(front); @@ -129,18 +154,17 @@ cry_queue_enqueue(struct CryQueue* queue, pointer data) { assert(queue != 0); - struct Queue* list = cry_cast(struct Queue*, queue); struct Node* node = cry_malloc(struct Node); node->next = 0; node->data = data; - if(list->front == 0) { - list->front = node; - list->tail = node; + if(queue->front == 0) { + queue->front = node; + queue->rear = node; } else { - list->tail->next = node; - list->tail = node; + queue->rear->next = node; + queue->rear = node; } }