在C#中完成链接列表的问题

Yas*_*sin 0 c# linked-list

我的代码差不多完成了一个疯狂的bug出现了!当我取消最后一个节点以完成链接列表时,它实际上转储所有链接并使第一个节点链接为空!当我跟踪它时,它的工作完全正常在循环中创建列表但是在循环完成后发生并且它通过这样做破坏链接的其余部分,并且我不明白为什么这么明显的东西变得有问题!(最后一行)

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!
}
Run Code Online (Sandbox Code Playgroud)

Dia*_*tis 7

p总是保留对第一个元素的引用,所以是的,p->link = null;完全按照你说的去做.在我看来,你想要这样的东西:

    ...
    p->link = test;
    p = test;
    ....
Run Code Online (Sandbox Code Playgroud)

编辑:

概念证明

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 没问题,但是你应该知道每次运行这段代码时,小猫都会死... (2认同)