Connecting Tech Pros Worldwide Forums | Help | Site Map

problem updating linked list

Member
 
Join Date: Aug 2007
Posts: 35
#1: Sep 25 '07
Hello all
I have a linked list and a method that inserts certain elements in the link. When going trough the list in the method I make the changes the list is changed as i want, but when i use the list in another function the list is not updated, it contains null components. What can I do in order for my list to remain changed?
this is the functin that modifies the list
Expand|Select|Wrap|Line Numbers
  1. BYTE CheckComponents()
  2. {
  3.   Node *comp=components, *x, *y;
  4.   while (comp!=NULL)
  5.   {
  6.     ..............................
  7.     y = new Node("Telegram", telegram, PLAIN_DESC);
  8.     y->setNext(comp->getNext());
  9.     comp->setNext(y);
  10.     comp=comp->getNext();
  11.  
  12.   }
  13. }
  14.  
can you please tell me what am i doing wrong?
thanks

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Sep 25 '07

re: problem updating linked list


Quote:

Originally Posted by Dreea

Hello all
I have a linked list and a method that inserts certain elements in the link. When going trough the list in the method I make the changes the list is changed as i want, but when i use the list in another function the list is not updated, it contains null components. What can I do in order for my list to remain changed?
this is the functin that modifies the list

Expand|Select|Wrap|Line Numbers
  1. BYTE CheckComponents()
  2. {
  3.   Node *comp=components, *x, *y;
  4.   while (comp!=NULL)
  5.   {
  6.     ..............................
  7.     y = new Node("Telegram", telegram, PLAIN_DESC);
  8.     y->setNext(comp->getNext());
  9.     comp->setNext(y);
  10.     comp=comp->getNext();
  11.  
  12.   }
  13. }
  14.  
can you please tell me what am i doing wrong?
thanks

Show me your full code.
Then I will be able to solve your code.

Kind regards,
Dmjpro.
Member
 
Join Date: Aug 2007
Posts: 35
#3: Sep 26 '07

re: problem updating linked list


I was deleting y variable at the end of the function, that is why the list was truncated in other functions. Now I have another problem... I have pieces of code similar to this one:
Expand|Select|Wrap|Line Numbers
  1.     p2=new Node(x->getComponentName(), cVal, cVal);
  2.     p1->setNext(p2);
  3.     p1=p2;
  4.     p2=NULL;
  5.     delete p2;
  6.  
and in the output window I find the following message signaling i have a memory leak at the line where I allocate memory to p2
program.cpp(1311) : {501} normal block at 0x00435EC0, 56 bytes long.
Data: < [B ^C A%C > F4 5B 42 00 00 5E 43 00 CC CD CD CD 41 25 43 00
{500} normal block at 0x00435F30, 33 bytes long.
Data: < 21 23 4A E3 > 00 32 31 20 32 33 20 34 41 20 45 33 20 00 CD CD
{497} normal block at 0x004359E0, 33 bytes long.
Data: < Telegram > 00 54 65 6C 65 67 72 61 6D 00 CD CD CD CD CD CD

Any idea what am I doing wrong?
Reply