Yasin
Yasin

Reputation: 619

Problem in finalizing the link list in C#

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!
}

Upvotes: 0

Views: 344

Answers (3)

user7116
user7116

Reputation: 64098

Edit: struct linked list without unsafe pointers...weirdest requirement in a class ever.

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;
    }
}

Upvotes: 0

Diadistis
Diadistis

Reputation: 12174

p always holds reference to the first element, so yes, p->link = null; does exactly what you said. It seems to me that you wanted something like this :

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

Edit :

Proof of concept

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;
        }
    }
}

Upvotes: 7

ChaosPandion
ChaosPandion

Reputation: 78282

I seriously recommend you take a step back and clean up your code. From just a basic glance it doesn't seem to be doing very much for the amount of code you have written. Take notice of the blocks of duplicate code that differ only by the end condition. They are prime candidates for methods. With a few minutes of refactoring you could probably cut out half of your code and get a clear picture of what you are trying to accomplish and easily find the error.

Upvotes: 3

Related Questions