2010-05-18 38 views
0

我的代码几乎完成了一个疯狂的错误!当我终止最后一个节点来完成链接列表时,它实际上会转储所有链接并使第一个节点链接为空! 当我追踪它时,它的工作完全正常,在循环中创建列表,但在循环完成后会发生,并且通过这样做破坏链接的其余部分,我不明白为什么某些显而易见的问题变得有问题! (最后一行)在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! 
} 
+4

为什么你使用不安全的代码呢? – ChaosPandion 2010-05-18 19:34:27

+0

我意识到你可能为了某些学术目的而建立链表,但是如果你不是这样 - .NET框架包含一个链接列表集合类型:http://msdn.microsoft.com/en-us/library /he2s3bh7.aspx – 2010-05-18 19:36:35

+0

因为我有严重的问题,使用指针,它给我错误,将其更改为不安全,所以我使用不安全 – Yasin 2010-05-18 19:37:49

回答

7

p始终保持对第一个元素的引用,所以是的,p->link = null;完全符合您所说的内容。在我看来,你想是这样的:

... 
    p->link = test; 
    p = test; 
    .... 

编辑:

概念证明

public unsafe struct poly { public int coef; public int pow; public poly* link; } 

public unsafe class Program 
{ 
    static void Main(string[] args) 
    {    
     poly* temp1, temp2, start = 
      (poly*)Marshal.AllocHGlobal(sizeof(poly)).ToPointer(); 
     start->coef = 0; 
     start->pow = 0; 
     temp1 = start; 
     for (int i = 1; i < 10; i++) 
     { 
      temp2 = (poly*)Marshal.AllocHGlobal(sizeof(poly)).ToPointer(); 
      temp2->coef = i; 
      temp2->pow = i; 
      temp1->link = temp2; 
      temp1 = temp2; 
     } 
     temp1->link = null; 
     temp1 = start; 

     while (temp1 != null) 
     { 
      Console.WriteLine(
       string.Format(
        "eoef:{0}, pow:{1}", 
        temp1->coef, 
        temp1->pow)); 
      temp1 = temp1->link; 
     } 
    } 
} 
+0

最后一个节点的空链接怎么样?!这是怎么处理的? – Yasin 2010-05-18 19:52:28

+0

如果有疑问,请手动将其设为空。 AllocHGlobal的文档特别指出,它不会分配内存空,所以你应该确保指针为null。 – 2010-05-18 19:56:15

+0

@diadistis:对不起,它不工作。因为test是在每个循环中创建的单个节点 – Yasin 2010-05-18 20:01:33

3

我认真地建议你退后一步,清理你的代码。从基本的一瞥来看,它似乎没有对你写的代码量做太多的事情。注意只有结束条件不同的重复代码块。他们是方法的主要候选人。重构几分钟后,您可能会删除一半的代码,并清楚地了解您正在尝试完成的任务并轻松找到错误。

+0

是的,你是对的这段代码是如此混乱,但我必须做得太快,我必须在几个小时内将它提交给我的教授:D – Yasin 2010-05-18 19:47:57

+0

你宁愿提交一些看起来很漂亮,但不起作用的东西,或者看起来像一团糟,但仍然不起作用的东西 – 2010-05-18 19:53:38

+0

如果这个简单但未知的错误得到解决,它就必须工作 – Yasin 2010-05-18 19:57:16

0

编辑:struct没有不安全的指针链表...在最奇怪的要求上课。

public struct Polynomial 
{ 
    public int Coefficient; 
    public int Power; 
    public Polynomial? Link; 
} 

private Polynomial? start; 

private void button1_Click(object sender, EventArgs e) 
{ 
    string holder = String.Empty; 
    start = new Polynomial(); 
    int i = 0; 
    while (this.textBox1.Text[i] != ',') 
    { 
     holder += this.textBox1.Text[i]; 
     i++; 
    } 

    start.Value.Coefficient = Int32.Parse(holder); 

    i++; 
    holder = String.Empty; 
    while (this.textBox1.Text[i] != ';') 
    { 
     holder += this.textBox1.Text[i]; 
     i++; 
    } 

    start.Value.Power = Int32.Parse(holder); 
    Polynomial? p = start; 

    //creation of the first node finished! 
    i++; 
    for (; i < this.textBox1.Text.Length; i++) 
    { 
     Polynomial? test = new Polynomial(); 

     holder = String.Empty; 
     while (this.textBox1.Text[i] != ',') 
     { 
      holder += this.textBox1.Text[i]; 
      i++; 
     } 

     test.Value.Coefficient = Int32.Parse(holder); 

     i++; 
     holder = String.Empty; 
     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.Value.Power = Int32.Parse(holder); 
     p.Value.Link = test; //the addresses are correct and the list is complete 
     p = test; 
    } 
} 
+0

thx为答复的家伙,但我必须做的结构,我不能使用类 – Yasin 2010-05-18 20:10:48

+0

我有严重的麻烦相信......有史以来最奇怪的要求。你仍然不需要不安全的指针,我会更新我的代码来显示。 – user7116 2010-05-18 20:23:27

+0

ty,这是非常有用的,但我们也必须在这个程序中使用指针:P。 thx再次 – Yasin 2010-05-18 20:49:00