Problem in finalizing the link list in C#
Posted
by Yasin
on Stack Overflow
See other posts from Stack Overflow
or by Yasin
Published on 2010-05-18T19:31:09Z
Indexed on
2010/05/18
20:00 UTC
Read the original article
Hit count: 301
My code was almost finished that a maddening bug came up! when i nullify the last node to finalize the link list , it actually dumps all the links and make the first node link Null ! when i trace it , its working totally fine creating the list in the loop but after the loop is done that happens and it destroys the rest of the link by doing so, and i don't understand why something so obvious is becoming problematic! (last line)
struct poly { public int coef; public int pow; public poly* link;} ;
poly* start ;
poly* start2;
poly* p;
poly* second;
poly* result;
poly* ptr;
poly* start3;
poly* q;
poly* q2;
private void button1_Click(object sender, EventArgs e)
{
string holder = "";
IntPtr newP = Marshal.AllocHGlobal(sizeof(poly));
q = (poly*)newP.ToPointer();
start = q;
int i = 0;
while (this.textBox1.Text[i] != ',')
{
holder += this.textBox1.Text[i];
i++;
}
q->coef = int.Parse(holder);
i++;
holder = "";
while (this.textBox1.Text[i] != ';')
{
holder += this.textBox1.Text[i];
i++;
}
q->pow = int.Parse(holder);
holder = "";
p = start;
//creation of the first node finished!
i++;
for (; i < this.textBox1.Text.Length; i++)
{
newP = Marshal.AllocHGlobal(sizeof(poly));
poly* test = (poly*)newP.ToPointer();
while (this.textBox1.Text[i] != ',')
{
holder += this.textBox1.Text[i];
i++;
}
test->coef = int.Parse(holder);
holder = "";
i++;
while (this.textBox1.Text[i] != ';' && i < this.textBox1.Text.Length - 1)
{
holder += this.textBox1.Text[i];
if (i < this.textBox1.Text.Length - 1)
i++;
}
test->pow = int.Parse(holder);
holder = "";
p->link = test; //the addresses are correct and the list is complete
}
p->link = null; //only the first node exists now with a null link!
}
© Stack Overflow or respective owner