我的代码几乎完成了一个疯狂的错误!当我终止最后一个节点来完成链接列表时,它实际上会转储所有链接并使第一个节点链接为空! 当我追踪它时,它的工作完全正常,在循环中创建列表,但在循环完成后会发生,并且通过这样做破坏链接的其余部分,我不明白为什么某些显而易见的问题变得有问题! (最后一行)在C中完成链接列表时出现问题#
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!
}
为什么你使用不安全的代码呢? – ChaosPandion 2010-05-18 19:34:27
我意识到你可能为了某些学术目的而建立链表,但是如果你不是这样 - .NET框架包含一个链接列表集合类型:http://msdn.microsoft.com/en-us/library /he2s3bh7.aspx – 2010-05-18 19:36:35
因为我有严重的问题,使用指针,它给我错误,将其更改为不安全,所以我使用不安全 – Yasin 2010-05-18 19:37:49