List Problem

This is a discussion on List Problem within the C++ Programming forums, part of the General Programming Boards category; Ok, so I'm trying to tackle an algorithm from a book and need to create a simple list of structures. ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    9

    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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    9

    Post

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21