Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commit 110468f7 authored by rswindell's avatar rswindell

Add listReverse() - popular interview question, no need for use right now

Add listVerify() - confirm the list is sane
parent 1d44d9cb
......@@ -853,6 +853,84 @@ BOOL DLLCALL listSwapNodes(list_node_t* node1, list_node_t* node2)
return(TRUE);
}
static void list_update_prev(link_list_t* list)
{
if(list == NULL)
return;
list_node_t* node = list->first;
list_node_t* prev = NULL;
while(node != NULL) {
node->prev = prev;
prev = node;
node = node->next;
}
}
void DLLCALL listReverse(link_list_t* list)
{
if(list == NULL)
return;
list_node_t* node = list->first;
if(node == NULL)
return;
listLock(list);
list->last = list->first;
list_node_t* prev = NULL;
while(node != NULL) {
list_node_t* next = node->next;
node->next = prev;
prev = node;
node = next;
}
list->first = prev;
list_update_prev(list);
listUnlock(list);
}
long DLLCALL listVerify(link_list_t* list)
{
long result = __COUNTER__;
if(list == NULL)
return -__COUNTER__;
listLock(list);
list_node_t* node = list->first;
list_node_t* prev = NULL;
while(node != NULL) {
if(node->list != list) {
result = -__COUNTER__;
break;
}
if(node->prev != prev) {
result = -__COUNTER__;
break;
}
prev = node;
node = node->next;
result++;
}
if(result >= 0 && list->last != prev)
result = -__COUNTER__;
if(result >= 0 && result != list->count)
result = -__COUNTER__;
listUnlock(list);
return result;
}
#if 0
#include <stdio.h> /* printf, sprintf */
......@@ -860,22 +938,40 @@ BOOL DLLCALL listSwapNodes(list_node_t* node1, list_node_t* node2)
int main(int arg, char** argv)
{
int i;
long result;
char* p;
char str[32];
link_list_t list;
listInit(&list,0);
if((result = listVerify(&list)) < 0) {
fprintf(stderr, "line %d: listVerify() returned %ld\n", __LINE__, result);
return EXIT_FAILURE;
}
for(i=0; i<100; i++) {
sprintf(str,"%u",i);
listPushNodeString(&list,str);
}
if((result = listVerify(&list)) < 0) {
fprintf(stderr, "line %d: listVerify() returned %ld\n", __LINE__, result);
return EXIT_FAILURE;
}
listReverse(&list);
if((result = listVerify(&list)) < 0) {
fprintf(stderr, "line %d: listVerify() returned %ld\n", __LINE__, result);
return EXIT_FAILURE;
}
while((p=listShiftNode(&list))!=NULL)
printf("%d %s\n",listCountNodes(&list),p), free(p);
if((result = listVerify(&list)) < 0) {
fprintf(stderr, "line %d: listVerify() returned %ld\n", __LINE__, result);
return EXIT_FAILURE;
}
/* Yes, this test code leaks heap memory. :-) */
gets(str);
return 0;
return EXIT_SUCCESS;
}
#endif
......@@ -205,6 +205,12 @@ DLLEXPORT void* DLLCALL listRemoveTaggedNode(link_list_t*, list_node_tag_t, BOOL
/* Remove multiple nodes from list, returning the number of nodes removed */
DLLEXPORT long DLLCALL listRemoveNodes(link_list_t*, list_node_t* /* NULL=first */, long count, BOOL free_data);
/* Reverse the nodes in a list */
DLLEXPORT void DLLCALL listReverse(link_list_t*);
/* Return >= 0 (count of nodes) if list is valid, negative otherwise */
DLLEXPORT long DLLCALL listVerify(link_list_t*);
#if defined(__cplusplus)
}
#endif
......
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