C# でリンク リストを使用した多項式に関する単純なプログラムを作成しています。私が抱えている問題は、for ループで新しい構造体 (ノード) を作成するたびに、前のノードが指定されたのと同じアドレスを指定することです。どうすれば修正できますか?ここに私の構造体があります:
struct poly { public int coef; public int pow; public poly* link;} ;
そして、問題が発生する場所は次のとおりです。
for (; i < this.textBox1.Text.Length; i++)
{
q = new poly();
...
p->link = &q;
}
でも&q相変わらず!
アップデート:
さらに明確にするために、完全なコードを次に示します。
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;
}
}
}
私たちの教授は私たちに C でそれを行うように頼んだが、C# で行うことに決めたが、実際に C を使用する人はもう誰もいないので、C の外観を与えることにした。