when i execute this code and lets say i enter "hello", then "world", and then "dude", when i traverse through the list, it only prints out:
dude
dude
dude
and not this:
dude
world
hello
Any help would be much appreciated!
#include <stdio.h>
#include <stdlib.h>
struct Node {
void *data;
struct Node *next;
};
struct List {
struct Node *head;
};
static inline void initList(struct List *list)
{
list->head = 0;
}
struct Node *addFront(struct List *list, char *data);
void traverseList(struct List *list0);
int main(){
// initialize list
struct List list;
initList(&list);
char input[100];
// test addFront()
printf("testing addFront(): ");
int i;
for (i = 0; i < 3; i++) {
fgets(input, sizeof(input), stdin);
addFront(&list, input);
}
traverseList(&list);
return 0;
}
struct Node *addFront(struct List *list, char *data)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL)
return NULL;
node->data = data;
node->next = list->head;
list->head = node;
return node;
}
void traverseList(struct List *list)
{
struct Node *node = list->head;
while (node) {
node = node->next;
}
}