LiLRiceBoi
Golden Member
this program is supposed to parse a string of integers entered and make a linked list. the function that i'm having problems on is adding "atoms" to a list. ex. ( 1 2 3 ) has 3 atoms, 1, 2, and 3, while the list ( 1 ( 2 3 ) ) has only two elements, one atom, 1, and a sublist, ( 2 3 ).
here is the struct and the function that i'm having problems on
i commented so you can quickly see what's going on. visual studios says the problem is on the second to last line, the one with newlist->nextPtr = atom;. I would appreciate any help i can get on this. thanks.
here is the struct and the function that i'm having problems on
typedef struct atomStruct {
int value; /* let's just use ints for the atom's value */
struct atomStruct* sublistPtr; /* if the element is really a sublist */
struct atomStruct* nextPtr; /* the next element in the list (or NULL if end) */
} AtomType, *AtomTPtr;
/* AddAtomToList - Add an atom to the end of the list
* Inputs:
* list - the list (possibly NULL - empty)
* atom - the atom to add
* Outputs: the pointer to the head of the list
* Side Effects: The atom is added to the end of the list. If the list was empty,
* the atom will be the only member of the list.
*/
AtomTPtr AddAtomToList(AtomTPtr list, AtomTPtr atom)
{
AtomTPtr newlist; /*new list of same type*/
if (list == NULL) /*if the list is empty make it equal atom*/
list = atom;
else /*if it's not empty*/
{
newlist = list; /*make newlist so list still points to head*/
while (newlist != NULL) /*find end of list*/
{
newlist = newlist->nextPtr;
}
}
newlist->nextPtr = atom; /*set the end of newlist, which points to list, = atom*/
return list; /*return list, which still points to the head of list*/
}
i commented so you can quickly see what's going on. visual studios says the problem is on the second to last line, the one with newlist->nextPtr = atom;. I would appreciate any help i can get on this. thanks.