954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

The linked list display problem


<pre><code>void Display(Productptr t)
{
Productptr g;
g=t;
if(t==NULL)
cout&lt;&lt;&quot;The list is empty.&quot;&lt;&lt;endl;
else
{
cout&lt;&lt;&quot;Code\tDescription\tPrice\n&quot;;
cout&lt;&lt;&quot;====\t===========\t=====\n&quot;;
while(g!= NULL)
{
cout&lt;&lt;g-&gt;code&lt;&lt;&quot;\t&quot;
&lt;&lt;g-&gt;description&lt;&lt;&quot;\t\t&quot;
&lt;&lt;g-&gt;price&lt;&lt;endl;
}
cout&lt;&lt;&quot;End of the list&quot;&lt;&lt;endl;
}
system(&quot;pause&quot;);
}</code></pre>

This code give me problem.
When I show 1st time is ok.
After 1st time I add something or delete something it will shown repeated 1st node that I insert.After that I cannot get back the normal already.
Any one can explain and solve it??
Text file I also get the same problem.
When I add or delete something in function cannot work normal agian.
The picture down show my problem.

Attachments Problem.bmp (851.36KB)
Jakiro
Newbie Poster
8 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

The loop that begins on line 11 of the code you posted: where in that loop do you increment g to point to the next node in the linked list? Don't you want something like this?

while(g != NULL)
{
  ... <snip>
   g = g->next;
}


Also I don't see any need to save the original parameter pointer because it is never used again in that function. You could just deleteg and use t instead.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I solve my problem already.
Thank you.
I miss up one g=g->Next;
so i alway get back the samething.

Jakiro
Newbie Poster
8 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You