【发布时间】:2019-04-27 17:50:32
【问题描述】:
这是我的代码:
typedef struct noeud{
int x;
struct noeud* suivant;
} noeud;
typedef noeud* file;
file enfiler(file f, int val){
file nv = (file) malloc(sizeof(noeud));
nv->x = val; nv->suivant = NULL;
if (f == NULL)
f = nv;
else {
file tmp = f;
while(tmp->suivant != NULL) tmp = tmp->suivant;
tmp->suivant = nv;
}
return f;
}
file defiler(file f){//removing an element of the FIFO data structure
if (f == NULL)
return f;
else {
file tmp = f;
f = f->suivant;//receiving address of next node, the last one points to NULL
free(tmp);
return f;
}
}
int tete(file f){
return f->x;//getting the element of the head
}
void init(file * f) {
*f = NULL;
}
void affiche(file f){//print data structure's elements
if (f == NULL)
printf("File vide.\n");
else {//emptying the FIFO data structure into tmp to access elements
file tmp; init(&tmp);
while(f != NULL){
tmp = enfiler(tmp, tete(f));
f = defiler(f);
}
int i = 0;
while(tmp != NULL) {//emptying tmp to original f
printf("F[%d] = %d\n", ++i, tete(tmp));
f = enfiler(f, tete(tmp));
tmp = defiler(tmp);
}
}
}
这是我的输入:
file f; init(&f);//initializing f to NULL
f = enfiler(f, 6);//adding elements
f = enfiler(f, 45);
f = enfiler(f, 78);
f = enfiler(f, 5);
affiche(f);
affiche(f);
affiche(f);
这是输出:
F[1] = 6
F[2] = 45
F[3] = 78
F[4] = 5
F[1] = 78
F[2] = 5
F[1] = 2036736 //这是一个随机值
每个void affiche(file f)都丢了两个头,我修改了函数file defiler(file f)但似乎找不到错误,file enfiler(file f, int x)也可以。
感谢您的宝贵时间!
【问题讨论】:
-
能否分享enfiler(...)的代码?
-
完成!不应该跳过那个。
-
如果我很理解
void init(file * f) { *f = NULL; } -
是的,就是这样!
-
我编辑了您的问题以添加它,并修复了缩进