ref: 1d710193b2b6d199929d61a3aa1547a8f8770cd8
parent: eb1032e367ec1aee3cf5e321c4112f97720f42ee
author: mkf <mkf@cloud9p.org>
date: Thu Jun 6 08:10:31 EDT 2024
replace qnext and qadd implmentions by a improved one
--- a/util.c
+++ b/util.c
@@ -127,12 +127,19 @@
if(last->next != nil)
return 0;
- temp = llnew();
+ /* first node? */
+ if(last->data == nil)
+ last->data = data;
+ else
+ {
+ dprint("lladd(): adding a new entry\n");
+ temp = llnew();
- last->next = temp;
- temp->data = data;
-
+ last->next = temp;
+ temp->data = data;
+ }
return 1;
+
}
void
@@ -143,7 +150,9 @@
dprint("llappend(%p, %p)\n", first, data);
- for(last = first ; last->next != nil ; last = last->next)
+ for(last = first;
+ last->next != nil && last != last->next;
+ last = last->next)
;
/* workaround for first entry */
@@ -184,6 +193,7 @@
}
/* quene */
+/*
void
qadd(Quene *q, void *data)
{
@@ -191,22 +201,61 @@
lladd(q->tail, data);
q->tail = q->tail->next;
}
+*/
void
+qadd(Quene *q, void *data)
+{
+ dprint("qadd(%p, %p)\n", q, data);
+
+ q->len++;
+ if(q->head == nil && q->tail == nil)
+ {
+ q->head = llnew();
+ q->head->data = data;
+ q->tail = q->head;
+ }
+ else
+ {
+ lladd(q->tail, data);
+ q->tail = q->tail->next;
+ }
+}
+
+/*
+void
qnext(Quene *q)
{
- List *oldfirst, *last;
- if(q->len == 0)
+ List *oldfirst;
+ if(q->len-- == 0)
sysfatal("qnext(): q->len == 0");
- q->len--;
-
oldfirst = q->head;
- q->head = q->head->next;
+ // what??
+ if(q->head == nil)
+ q->head = q->head->next;
+
if(q->tail == oldfirst)
- q->tail = nil;
+ q->tail->data = nil;
free(oldfirst);
+}
+*/
+
+void
+qnext(Quene *q)
+{
+ List *tmp;
+ if(q->head == nil)
+ return;
+ tmp = q->head;
+ q->head = q->head->next;
+
+ if(q->head == nil)
+ q->tail = nil;
+
+ q->len--;
+ free(tmp);
}