Re: Linked List & Dynamic Memory Allocation
- From: Dan Bloomquist <public21@xxxxxxxxxxx>
- Date: Mon, 27 Aug 2007 20:21:30 GMT
one-trick-pony wrote:
Sorry for confusion. I deliberately left code out because it was
irrelevant to the question I am asking. I have program working-I have
a linked list and it prints all the records fine. The only problem is
connecting/attaching a new dynamically allocated records to the linked
list.
Will this work if user enters 5 or 6 or even 100 or x number of
records? Your code worked for adding on only 1 more record. It keeps
the 3 hardcoded records then added one more with the help of following
lines of code:
m_nMode[MAX-1].next = pNewRec;
pNewRec->next = NULL;
However, if user enters more than 1 the only one attached to the list
is the very last entry that user made before choosing to stop. Still
need help. Thanks
Is this purely an exercise? If not, there are tools in the STL to make your life easier.
But if it is...
First, I wouldn't call a node, 'nodes'. Your starting array is a list of 'nodes'.
Node nodes[3];
So you have the array populated and the last node has a node pointer of null or the next one on the heap. You can walk the pointers to find the last node.
Node* pNode;
for( pNode= nodes[ 3 ].next; pNode->next; pNode= pNode->next )
;
pNode->next= new Node;
Or you can just keep a pointer to the last node that you update when you add nodes, like:
pNode= pNode->next;
Use this pNode to setup your new node and keep it around to add the next node.
But for an occasional TCHAR array, I don't think I've used an array on the stack for a decade. (I'm really thinking here! :)
Best, Dan.
.
- Follow-Ups:
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- References:
- Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Re: Linked List & Dynamic Memory Allocation
- From: Jonathan Wood
- Re: Linked List & Dynamic Memory Allocation
- From: one-trick-pony
- Linked List & Dynamic Memory Allocation
- Prev by Date: Re: GUI update
- Next by Date: Re: Crash inside of _AfxMsgFilterHook
- Previous by thread: Re: Linked List & Dynamic Memory Allocation
- Next by thread: Re: Linked List & Dynamic Memory Allocation
- Index(es):
Relevant Pages
|
Loading