问题
I created linked list of my structure, but for some reason every time I add another link it changes head address, but I want y head address be first entry. this is my code:
struct checkPoints *tgh = NULL;
struct checkPoints **linkedlist = &tgh;
struct checkPoints *cp = malloc(sizeof (struct checkPoints));
chPo = fopen(fileName, mode);
if (chPo == NULL) {
printf("Can't find the files.");
exit(1);
} else {
for (i = 0; i < lines; i++) {
fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
cp->next = NULL;
if (*linkedlist == NULL) {
printf("ONCE");
*linkedlist = cp;
} else {
struct checkPoints *new = *linkedlist;
while (new->next != NULL) {
new = new->next;
}
new->next = cp;
}
}
}
every fscanf occurs it changes head address to next, any ideas?
Head address changes after this line: fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
The structure is this:
struct checkPoints{
char dropOut;
int currentPoint;
int competitor;
int hour;
int minute;
struct checkPoints *next;
};
回答1:
The problem here is that you do not allocate new nodes, you only have one node that you change over and over again. You need to allocate the node inside the loop.
回答2:
I don't see any malloc/calloc to create new nodes, which will get added to the list.
You need to create new nodes, which will get added to the list. Correct position would be just before
cp->next = NULL;
line
回答3:
In your case you are allocating a single node and over writting the data in the same address.
You need to allocate the memory for new nodes inside the loop.
The following line has to be added in the begining of the loop,
struct checkPoints *cp = malloc(sizeof (struct checkPoints));
回答4:
You need to allocate a fresh struct checkPoints
for each line you read. It will give you a loop like:
struct checkPoints *linkedlist = NULL;
/* … */
for (i = 0; i < lines; i++) {
struct checkPoints *cp = malloc(sizeof(struct checkPoints));
fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
cp->next = NULL;
if (linkedlist == NULL) {
linkedlist = cp;
} else {
struct checkPoints *new = linkedlist;
while (new->next != NULL) {
new = new->next;
}
new->next = cp;
}
}
Note that this way of proceeding is highly inefficient as it requires scanning your entire list again for each line. You should either keep a pointer to the tail of your list so that you can append without the while
loop. A clever alternative is to prepend each line to the front of the list and once you have read the whole file reverse the whole list.
struct checkPoints *cp;
for (i = 0; i < lines; i++) {
cp = malloc(sizeof(struct checkPoints));
fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
cp->next = linkedlist;
linkedlist = cp;
}
cp = linkedlist;
linkedlist = NULL;
struct checkPoints *next = cp;
while (cp) {
next = cp->next;
cp->next = linkedlist;
cp = next;
}
来源:https://stackoverflow.com/questions/13875395/linked-list-head-address-changes-c