code:basicaly I am trying to make a linear linked list with pointers. In main I pass the constructor an array with the items say a,b,c, and d.class List
{
public:
List( /* in */ const char dataLine[] );
~List( );
void PrintList( ) const;
void ReversePrintList( ) const;
private:
struct Node
{
char item;
Node* next;
};
Node* front;
};
List::List( /* in */ const char dataLine[] )
{
int temp = strlen(dataLine);
for ( int counter = 0; counter < temp; counter++)
{
front = new Node;
front->item = dataLine[counter];
front->next = front;
cout << front->item << endl;
}
}
quote:Just like you told it to. Dude, it's staring you right in the face. Step through the code in your head like we've been telling you; it'll jump right out. By the way, do you have a sub for Poland or Greece tomorrow?
for some reason its making a circular linked list by pointing to itself
quote:This ties in with my suggestion to explicitly initialize your struct values/pointers (for this exercise, until you understand it). If you don't, your assumptions can be wrong.
Originally posted by MrSquicky:
It's important to keep in mind that a linked list should generally also end (i.e. have its last node pointing to nothing).
code:k with KoM guiding me and helping me understand this I have rewritten my code but it says when I run it that there is an unhandled exception at some place in memory access vilation reaing location some place in memory.List::List( /* in */ const char dataLine[] )
{
Node* current;
front = new Node;
front->item = dataLine[0];
int temp = strlen(dataLine);
for ( int counter = 1; counter < temp; counter++)
{
current = front;
Node* newPtr;
newPtr = new Node;
newPtr->item = dataLine[counter];
*while ( current->next != NULL )
{
// stuff
current = current->next;
}
current->next = newPtr;
cout << newPtr->item << endl;
}
}