linked list not traversing

closed account (98qGz8AR)
hi everyone.
been working on a program for uni and this has been driving me crazy. i've got a class and several nested structs inside the class, and i'm trying to get two linked lists running, one of data, and another list of pointers to that data, but it's not working properly. it only stores one node, and nothing else.

on the bright side, everything else works just fine!

here are the two functions that are not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
void dataClass::display()
{
    //check if headPoint is NULL
    if(headPoint == NULL)
    cout << endl << endl << "No entries detected!";
    
    pointerNode * current;
    
    current = headPoint->nextPoint;
    while(current->nextPoint != NULL)
    {
          cout << *current->point.numPoint << endl;
          cout << *current->point.namePoint << endl;
          current = current->nextPoint;
    }
    cout << *current->point.numPoint << endl;
    cout << *current->point.namePoint << endl;
}
 
//function gets in a struct of data to be added, returns a 0 if it all went off without a hitch
int dataClass::addNode(data & data_to_add)
{
    if(headData == NULL)
    {
           headData = new dataNode;
           headPoint = new pointerNode;
           headData->nextData = NULL;
           headPoint->nextPoint = NULL;
           
           /*
           dataNode * currentData; //creates a new data node for traversal
           currentData = headData; //sets it equal to head data node
    
           pointerNode * currentPointer; //creates a new pointer node for traversal
           currentPointer = headPoint; //sets it equal to the head pointer node
           */
    }
    
           dataNode * currentData; //creates a new data node for traversal
           currentData = headData; //sets it equal to head data node
    
           pointerNode * currentPointer; //creates a new pointer node for traversal
           currentPointer = headPoint; //sets it equal to the head pointer node
    
           if((currentData->nextData) && (currentPointer->nextPoint))
           {
                     while(currentData->nextData) //loops & traverses while there are more nodes
                     currentData = currentData->nextData;
    
                     while(currentPointer->nextPoint) //loops & traverses pointer list while(more nodes)
                     currentPointer = currentPointer->nextPoint;
            }
    
            else
            {
                     currentData->nextData = new dataNode; //creates a new node at the end of the list
                     currentData = currentData->nextData; //actually goes there
                     currentData->nextData = NULL; //sets the next pointer to be NULL
    
                     currentPointer->nextPoint = new pointerNode; //creates a new pointer node at end of list
                     currentPointer = currentPointer->nextPoint; //travels there
                     currentPointer->nextPoint = NULL; //sets next pointer to NULL
            }
    //sets each pointer member to point to the appropriate data member
    currentPointer->point.namePoint = &(currentData->dataEntry.name);
    currentPointer->point.numPoint = &(currentData->dataEntry.num);
    
    //performs a memberwise copy of each new data element
    currentData->dataEntry.name = new char [strlen(data_to_add.name)];
    strcpy(currentData->dataEntry.name, data_to_add.name);
    currentData->dataEntry.num = data_to_add.num;
    
    return 0;
}


any input at all would be greatly appreciated, i've traced this damn thing out at least three times to no avail.
Post the class declarations.
closed account (98qGz8AR)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//data type for information to be stored in primary list
struct data
{
       char * name;
       int num;
};

//data type for pointers to primary info list, for display purposes
struct pointers
{
       char ** namePoint;
       int * numPoint;
};

//declares the behavior of a list of data
struct dataNode
{
       data dataEntry;
       dataNode * nextData;
};

//declares the behavior of a list of pointers
struct pointerNode
{
       pointers point;
       pointerNode * nextPoint;
};

//class that determines how the data list can be used by the client
class dataClass
{
      private:
      dataNode * headData; //the head node of the data list, accessible only by the add function
      pointerNode * headPoint; //the head node of the pointer list, accessible only by the display function
      
      public:
      dataClass();
      ~dataClass();
      void display();
      int addNode(data & data_to_add);
};
Wait, let me get this straight. You have a list of nodes that contain data, and a list of nodes that contain pointers to the data inside the nodes of the other list? Does this second list have a point? Because it seems like it would only make things more complicated for no good reason.
closed account (98qGz8AR)
it's a mandatory part of the assignment to have two lists, and the teacher keeps stressing the importance of low memory overhead. i thought it would cut down on memory to have a list of pointers being the one traversed instead of the actual data list.

this may not actually be the case, but the professor says it is and that he knows what he is talking about. >:|

it does make things a little more complicated, but it made more natural sense to me than other alternatives (having a list of lists, etc).

i just can't figure out why the function addNode isn't actually adding any nodes, even though the display function outputs the values pointed to just fine.
i thought it would cut down on memory to have a list of pointers being the one traversed instead of the actual data list.
I can't even begin to imagine how you came to this conclusion, but no. This not only doesn't save memory, it uses more memory than the straightforward approach.
Traversing a list uses practically no memory (just one pointer to hold the current node), so there's little point in attempting to optimize that operation. In case, if one did want to optimize that, it would be better to go for a less, not more, complex data structure. The simpler the component parts of the structure, the less things you need to keep track of to traverse the structure. In this sense, an array and a list are more or less equally simple, a tree is less simple than a list, and a graph is less simple still.

it's a mandatory part of the assignment to have two lists
In that case, I think you interpreted that bit in the wrong direction. You were probably meant to have one list of strings and another list of integers, possibly using templates. I can't say that's the case for sure, but I do think it makes more sense than adding a pointless list that serves no purpose.
closed account (98qGz8AR)
eh, the assignment said that there were to be two lists, one of a data type, and another of categories. the user is supposed to be able to display by the second list (categories), and be able to add new data.

the list of pointers made sense for the categories list, instead of having a category list that was comprised of copies of bits and pieces of actual data.
I could keep guessing what a good solution would be based on the bits and pieces of the assignment you keep giving me, but I think it'll be faster if you just post the problem statement.
Topic archived. No new replies allowed.