Commit f8525c13 authored by Chris Müller's avatar Chris Müller

lang: struct CryQueue supports lookups for nth and rear elements in list.

parent e9d96ef4
......@@ -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);
......
......@@ -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 <http://www.gnu.org/licenses/>.
......@@ -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;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment