Skip to content
Snippets Groups Projects
Commit 63176d77 authored by Toby Murray's avatar Toby Murray
Browse files

list_pop

parent 531eb385
No related branches found
No related tags found
No related merge requests found
...@@ -192,12 +192,14 @@ char inst[INSTRUCTION_LENGTH]; ...@@ -192,12 +192,14 @@ char inst[INSTRUCTION_LENGTH];
/* password mapping for each url: initially empty */ /* password mapping for each url: initially empty */
node_t * map = NULL; node_t * map = NULL;
/* a list of node pointers -- the idea is to use these to implement a node /* a doubly-linked list of node pointers
stack that we can then use to traverse the tree iteratively, avoiding is used to implement stacks/queues of nodes so we can implement various
recursion. We need tree traversal e.g. when saving the passbook to a file */ tree traversal algorithms without using recursion (to avoid stack overflow
for very large trees). */
typedef struct nodeptr_list { typedef struct nodeptr_list {
const node_t *p; const node_t *p;
struct nodeptr_list *next; struct nodeptr_list *next;
struct nodeptr_list *prev;
} nodeptr_list_t; } nodeptr_list_t;
...@@ -206,26 +208,47 @@ nodeptr_list_t * list_push(nodeptr_list_t *lst, const node_t *p){ ...@@ -206,26 +208,47 @@ nodeptr_list_t * list_push(nodeptr_list_t *lst, const node_t *p){
assert(n != NULL && "push: malloc failed"); assert(n != NULL && "push: malloc failed");
n->p = p; n->p = p;
n->next = lst; n->next = lst;
if (lst != NULL){
lst->prev = n;
}
n->prev = NULL;
return n; return n;
} }
/* when out is non-NULL we place a pointer to the first node into it */
nodeptr_list_t * list_pop(nodeptr_list_t *lst, const node_t **out){
if (lst != NULL){
if (out != NULL){
*out = lst->p;
}
nodeptr_list_t *ret = lst->next;
free(lst);
return ret;
}else{
if (out != NULL){
*out = (const node_t *)NULL;
}
return lst;
}
}
void list_destroy(nodeptr_list_t *lst){ void list_destroy(nodeptr_list_t *lst){
while (lst != NULL){ while (lst != NULL){
nodeptr_list_t *next = lst->next; lst = list_pop(lst,NULL);
free(lst);
lst = next;
} }
} }
void list_print(nodeptr_list_t *lst){ void list_print(nodeptr_list_t *lst){
printf("\nBegin list print:\n"); printf("\nBegin list print:\n");
while (lst != NULL){ while (lst != NULL){
node_print(lst->p); const node_t *p;
lst = lst->next; lst = list_pop(lst,&p);
node_print(p);
} }
printf("End list print.\n\n"); printf("End list print.\n\n");
} }
void print_all(const node_t *p){ void print_all(const node_t *p){
nodeptr_list_t *lst = NULL; nodeptr_list_t *lst = NULL;
if (p != NULL){ if (p != NULL){
...@@ -239,10 +262,8 @@ void print_all(const node_t *p){ ...@@ -239,10 +262,8 @@ void print_all(const node_t *p){
} }
// pop from the stack to simulate the return // pop from the stack to simulate the return
const node_t *q = lst->p; const node_t *q;
nodeptr_list_t *temp = lst; lst = list_pop(lst,&q);
lst = lst->next;
free(temp);
// print the node following the return // print the node following the return
node_print(q); node_print(q);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment