|
省略しすぎです。コンパイル可能なソースを提示してください。
ソースを添付する際には「HTML変換ツール」で字下げしてください。(コピペ)
Windows2000sp4/VC++6sp5で動作確認済み
#include <stdio.h>
#include <stdlib.h>
//データを入れる構造体
struct dat{
char a[10];
char b[10];
};
//リストを構成するセル
struct cell{
struct dat *elem; //要素
struct cell *pre; //前のセルへ
struct cell *next; //次のセルへ
};
//適当な値をセット
void MyRead(struct dat *d)
{
static int num = 0;
if (d == NULL)
return;
sprintf(d->a, "%d", num);
sprintf(d->b, "%d", num * 10);
num++;
}
//初期化処理
void init(struct cell **c, struct dat *d)
{
if (*c == NULL){
*c = malloc(sizeof(struct cell));
if (*c == NULL)
exit(1); // error
}
(*c)->elem = malloc(sizeof(struct dat));
if ((*c)->elem == NULL){
free(*c);
exit(1); // error
}
*((*c)->elem) = *d;
(*c)->next = (*c);
(*c)->pre = (*c);
}
//リストを作る関数:右端に追加
void MyList(struct cell **c, struct dat *d)
{
struct cell *k = NULL;
init(&k, d);
//リストの結合
k->next = *c;
k->pre = (*c)->pre;
(*c)->pre->next = k;
(*c)->pre = k;
}
void view(struct cell *c, int viewCount, int moveNext)
{
int i;
if (c != NULL){
for (i = 0;i < viewCount; i++){
printf("\t %s \t %s\n", c->elem->a, c->elem->b);
if (moveNext != 0)
c = c->next;
else
c = c->pre;
}
}
printf("\n");
}
void term(struct cell *c)
{
c->pre->next = NULL; //無限ループにならないようにするための配慮
while(c != NULL){
if(c->next == NULL){ /* 最後のノード */
free(c->elem);
c->elem = NULL;
free(c);
c = NULL;
}else{
c = c->next; /* ポインタを次のノードに移動 */
/* 注意:ここで順番を間違えると、次のノードの場所が不明になり、全部削除できなくなる */
free(c->pre->elem);
c->pre->elem = NULL;
free(c->pre); /* 前のノードを削除 */
c->pre = NULL;
}
}
}
int main()
{
struct dat d;
struct cell *c = NULL;
int i, size = 10;
//初期化
MyRead(&d); //構造体にデータを入れる関数
init(&c, &d);
for (i = 0; i < 3; i++){
MyRead(&d); //構造体にデータを入れる関数
MyList(&c, &d);
}
printf("左から%d個を表示 >>\n", size);
view(c, size, 1);
printf("右から%d個を表示 >>\n", size);
view(c, size, 0);
//後処理
term(c);
return 0;
}
|