# List Problem

• 03-09-2008
Pantheon
List Problem
Ok, so I'm trying to tackle an algorithm from a book and need to create a simple list of structures. This is my code, it's pretty self-explanitory. Some empty functions that need to be filled in, a function to initialize the list, initialize nodes and add a node to a list.

Code:

```#include <iostream> #define max_x 50 #define max_y 50 #define mincost 1 #define max_list 50 using namespace std; /*GLOBAL VARIABLES*/ int world[max_x][max_y]; int goalx; int goaly; /*CLASSES*/ class node { public: int x; int y; double f; double g; double h; }; class list { public: int num_elems; node *elems[max_list]; }; /*LIST FUNCTIONS*/ void list_init(list *ilist) { ilist = new list; ilist->num_elems = 0; for(int i = 0; i<max_list; i++)         {         ilist->elems[i] = (node *)NULL;         } } void list_add(list *addlist, node *addnode) { for(int i= 0; i<max_list; i++)         {         if(addlist->elems[i] == (node *)NULL)                 {                 addlist->elems[i] = addnode;                 break;                 }         } addlist->num_elems++; } void list_remove() { } node *list_get() { } node *list_best() { } int list_empty() { } /*OTHER SUPPORT FUNCTIONS*/ void init_node(node *nnode, int x, int y) { nnode = new node; nnode->x = x; nnode->y = y; } /*FORMULAS*/ int calc_g() { } int calc_h() { } int cacl_f() { } /*MAIN PROGRAM*/ int main() { int a; list *new_list; node *node1; node *node2; node *node3; init_node(node1 ,2 ,3); init_node(node2 ,2 ,4); init_node(node3 ,5 ,7); list_init(new_list); a = new_list->num_elems; cout << a << "\n"; /*list_add(new_list, node1); list_add(new_list, node2); list_add(new_list, node3);*/ }```
The output says that the number of elements on the list is 24! I've gone over the code serveral times and can't see anything that would cause this. All help is appreciated.
• 03-09-2008
vart
list_init(new_list);

you passing pointer by value - so any changes made to the pointer inside function will be not visible outside the function

PS. It is very strange idea to implement list using static array - kills the idea of the list...
• 03-09-2008
Pantheon
I actually had started to rewrite the program not to use such a complicated list, but I know that I need to practice using pointers. Thanks for the help.