tags:

views:

120

answers:

4

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!
}
+3  A: 

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.

ChaosPandion
yeah you are right this code is such a mess but i had to do it too quickly and i have to submit it to my professor in a few hours :D
Yasin
Would you rather submit something that looks beautiful, but doesn't work, or something that looks like a mess, and still doesn't work?
Lasse V. Karlsen
well it must work if this simple yet unknown bug gets fixed !
Yasin
If a student handed me code that looked like it was designed by committee, they would get a low grade even if it worked. Maintainability and understandability are important aspects.Also, if your Professor does a google search on the first line, s/he will find this page at the top of the search results.Maybe s/he will give you credit for using SO ;-)
Simon Chadwick
well i'm sure he wont google it :D . he is old enough not to do that, lol. also he wont really look at the code
Yasin
@Yasin, I wouldn't underestimate your professors. As I understand it, he also asked you to implement it in C. If I were your professor, that alone would be enough to warrant a failing grade, as you've failed to meet one of the explicit requirements.
Dan Bryant
+7  A: 

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;
        }
    }
}
Diadistis
what about the null link of the last node ?! how does this take care of that ?
Yasin
if in doubt, null it manually. The documentation for AllocHGlobal specifically says that it doesn't null the memory allocated, so you should ensure the pointer is null yourself.
Lasse V. Karlsen
@diadistis: sorry its not working. this will also spoil the link list by assigning test to p since test is a single node created in each loop
Yasin
`p = test;` must go right after `p->link = test;` just before for-loop ends. When you exit the loop, p will be the last element and `p->link = null;` will work as expected. At that time you can access the list using `start`.
Diadistis
I exactly did what you said but still it just keeps "test" in it with the link to null so only one node there :(
Yasin
@diaditis: THX a bunch mate, i finally got it working as you mentioned :) . my problem was that i almost forgot about start at first
Yasin
No problem mate, but you should know that every time you run this code, a kitten dies...
Diadistis
A: 

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;
    }
}
sixlettervariables
thx for the reply dude but i HAVE to do it with structs and i can't use Class
Yasin
I have serious trouble believing that...most bizarre requirement ever. You still don't need unsafe pointers, I'll update my code to show that.
sixlettervariables
ty, it was very helpful but we also have to use pointers in this program :P . thx again
Yasin
Based on reading Yasin's other question, the professor actually asked the students to implement it in C. This was a 'creative' attempt to do it with C-ish syntax in C#.
Dan Bryant
@Dan Bryant: Hah, while typing up my answer I was reminded of the line from Billy Madison: "Everyone in this room is now dumber for having listened to it. I award you no points, and may God have mercy on your soul."
sixlettervariables
A: 

thx guys specially Diadistis, the problem is solved. issue was the missing p = test; in the loop

Yasin