I'm writing a simple program which is about polynomials using linked lists in C#. The problem I have is that whenever it creates a new struct (node) in the for loop it gives it the same address as the previous node was given. How do I fix that? Here is my struct:
struct poly { public int coef; public int pow; public poly* link;} ;
And here is where the problem occurs:
for (; i < this.textBox1.Text.Length; i++)
{
q = new poly();
...
p->link = &q;
}
But &q
remains unchanged!
Update:
In order to clarify it more, here is the full code:
namespace PolyListProject
{
unsafe public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
struct poly { public int coef; public int pow; public poly* link;} ;
poly *start ;
poly *p;
private void button1_Click(object sender, EventArgs e)
{
string holder = "";
poly q = new poly();
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++)
{
q = new poly();
while (this.textBox1.Text[i] != ',')
{
holder += this.textBox1.Text[i];
i++;
}
q.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++;
}
q.pow = int.Parse(holder);
holder = "";
p->link = q;
}
p->link = null;
}
}
}
Our professor asked us to do it in C but we decided to do it in C# yet giving it a C look, since no one actually uses C anymore.