summaryrefslogtreecommitdiff
path: root/list.c
diff options
context:
space:
mode:
Diffstat (limited to 'list.c')
-rw-r--r--list.c76
1 files changed, 71 insertions, 5 deletions
diff --git a/list.c b/list.c
index ab278a1..d6ecb1a 100644
--- a/list.c
+++ b/list.c
@@ -11,6 +11,9 @@
#define D(x)
#endif
+/*
+ * Move element in item to the head of list mainlist.
+ */
void movetohead(struct item **mainlist, struct item *item)
{
if (NULL == item || NULL == mainlist || NULL == *mainlist)
@@ -20,7 +23,7 @@ void movetohead(struct item **mainlist, struct item *item)
if (*mainlist == item)
{
- /* Already at head. Do nothing. */
+ /* item is NULL or we're already at head. Do nothing. */
return;
}
@@ -35,15 +38,23 @@ void movetohead(struct item **mainlist, struct item *item)
item->next->prev = item->prev;
}
- /* Now at head. */
+ /* Now we'at head, so no one before us. */
item->prev = NULL;
+
+ /* Old head is our next. */
item->next = *mainlist;
+ /* Old head needs to know about us. */
+ item->next->prev = item;
+
/* Remember the new head. */
*mainlist = item;
}
/*
+ * Create space for a new item and add it to the head of mainlist.
+ *
+ * Returns item or NULL if out of memory.
*/
struct item *additem(struct item **mainlist)
{
@@ -120,10 +131,14 @@ void listall(struct item *mainlist)
{
struct item *item;
int i;
+
+ printf("Listing all:\n");
for (item = mainlist, i = 1; item != NULL; item = item->next, i ++)
{
printf("%d at %p: %s.\n", i, (void *)item, (char *)item->data);
+ printf(" prev: %p\n", item->prev);
+ printf(" next: %p\n", item->next);
}
}
@@ -133,7 +148,10 @@ int main(void)
struct item *item1;
struct item *item2;
struct item *item3;
- struct item *item4;
+ struct item *item4;
+ struct item *item;
+ struct item *nextitem;
+ int i;
char *foo1 = "1";
char *foo2 = "2";
char *foo3 = "3";
@@ -179,12 +197,58 @@ int main(void)
printf("Current elements:\n");
listall(mainlist);
- movetohead(&mainlist, item2);
+ printf("----------------------------------------------------------------------\n");
+
+ printf("Moving item3 to be after item2\n");
+ movetonext(&mainlist, item2, item3);
+ printf("Current elements:\n");
+ listall(mainlist);
+
+ printf("----------------------------------------------------------------------\n");
+
+ printf("Moving head! item4 to be after item2\n");
+ movetonext(&mainlist, item2, item4);
printf("Current elements:\n");
listall(mainlist);
+
+ printf("----------------------------------------------------------------------\n");
+ printf("Moving tail! item1 to be after item2\n");
+ movetonext(&mainlist, item2, item1);
+ printf("Current elements:\n");
+ listall(mainlist);
+
printf("----------------------------------------------------------------------\n");
+ printf("Moving head to be after tail.\n");
+ movetonext(&mainlist, item3, item2);
+ printf("Current elements:\n");
+ listall(mainlist);
+
+ printf("Moving all the items after each other.\n");
+ /* item3 is tail. work backwards. */
+ for (item = mainlist, i = 1;
+ item != NULL;
+ item = item->next, i ++)
+ {
+ for (nextitem = item2; nextitem != NULL; nextitem = nextitem->prev)
+ {
+ movetonext(&mainlist, nextitem, item);
+ printf("Current elements:\n");
+ listall(mainlist);
+ }
+ }
+
+ printf("----------------------------------------------------------------------\n");
+
+#if 0
+ movetohead(&mainlist, item2);
+ printf("Current elements:\n");
+ listall(mainlist);
+
+ printf("----------------------------------------------------------------------\n");
+#endif
+
printf("Deleting item stored at %p\n", item3);
delitem(&mainlist, item3);
printf("Current elements:\n");
@@ -203,7 +267,9 @@ int main(void)
listall(mainlist);
puts("");
-
+
+ printf("----------------------------------------------------------------------\n");
+
exit(0);
}
#endif