現在リスト構造を使ったプログラムを組んでおります。
どうしても分からないところがありまして、よろしければどなたかご教示願います。
環境:
WindowsXP HomeSP3
VC++2008EE
コンソールアプリケーション
自分としてはループで変数をカウントして、32~1までの数値をリスト要素の矩形構造体に
繰り返し代入する処理を実行したい。
最後にそれらの数値が入ったリストを、先頭から順に表示させたいのですが、どうもうまくいきません。
現状の問題点:
①動的に確保したメモリ領域のポインタが取得できない?
↓ソースです。
#include <stdio.h> #include <stdlib.h> //--矩形構造体 typedef struct RECT { int x,y; unsigned int w,h; }Rect; //--矩形リスト構造体 typedef struct RectNode { Rect rect; struct RectNode *next; } RectNode; static RectNode *nodeTop = NULL; //--関数宣言 static void add(Rect *rect); static void update(void); static void freeList(void); int main(void) { Rect rect; const int n = 32; int i; for(i = n; i ; i--) { rect.h = i; rect.w = i; rect.x = n - i; rect.y = n - i; add( &rect ); } update(); freeList(); return 0; } //--矩形情報の追加 static void add(Rect *rect) { RectNode *tmp; RectNode *node = (RectNode*)malloc(sizeof(RectNode)); node->rect = (*rect); node->next = NULL; tmp = nodeTop; if( tmp != NULL ) { tmp = nodeTop->next; while( tmp != NULL ) { tmp = tmp->next; } tmp = node; }else{ nodeTop = node; } } //--画面の更新 static void update(void) { RectNode *tmp; if(nodeTop == NULL) { return; } tmp = nodeTop; while( tmp != NULL ) { printf("h:%d\t",tmp->rect.h); printf("w:%d\t",tmp->rect.w); printf("x:%d\t",tmp->rect.x); printf("y:%d\n",tmp->rect.y); tmp = tmp->next; } printf("Finish!\n"); } //--矩形情報の削除 static void freeList(void) { RectNode *tmp,*next; tmp = nodeTop; while(tmp != NULL) { next = tmp->next; //リストを退避 free(tmp); //要素を解放 tmp = next; //次のリスト } }よろしくお願いします。