私は次の構造体を持っています:
struct coords
{
int x;
int y;
struct coords* previous;
struct coords* next;
};
次に、これらの座標の二重にリンクされたリスト ((x,y) としてフォーマット) を作成します。これは次のようになります (先頭と末尾はリストの開始点と終了点です)。
head tail
(-1, -1) <--> (0, 1) <--> (2, 1) <--> (1, 0) <--> (0, 2) <--> (-1, -1)
このリストを印刷したいので、次のコードがあります。
struct coords* iter = head;
while (iter->next != NULL)
{
printf("\n [this node: (%d, %d)] -> [next node: (%d), (%d)]", iter->x, iter->y, iter->next->x, iter->next->y);
iter = iter->next;
}
printf("done with loop");
私が得る出力はこれです:
[this node: (-1, -1)] -> [next node: (0, 1)]
[this node: (0, 1)] -> [next node: (2, 1)]
[this node: (2, 1)] -> [next node: (1, 0)]
[this node: (1, 0)] -> [next node: (0, 2)]
[this node: (0, 2)] -> [next node: (-1, -1)]
これはすべて正しいです。ただし、最後の行を印刷した直後に、「done with loop」を印刷せずにプログラムがクラッシュします。