Reputation: 19
So, recently I had to create a linked list structure and I think got a function of creating it to work (hopefully), but now I have such simple problem as printing it into the console. I dont know whether there is something wrong with my structure that I created, or I do something wrong with printing. I would appreciate if somebody could find what is wrong with my code:
struct z { int a; struct z *next; };
struct z *head, *node, *next;
int data, x = 1;
int CreateList() {
printf("Enter 0 to end\n");
printf("Enter data no. %d: ", x);
x++;
scanf("%d", &data);
if (data == 0) return 0;
head = (struct z *)malloc(sizeof(struct z));
if (head == NULL) { printf("Error creating head"); return 0; }
node = head;
node->a = data;
node->next = NULL;
while (data) {
next = (struct z *)malloc(sizeof(struct z));
if (next == NULL) { printf("Error creating next node no. %d", x); return 0;}
node = next;
printf("Enter data no. %d: ", x);
x++;
scanf("%d", &data);
node->a = data;
node->next = NULL;
}
return 0;
}
int main() {
CreateList();
node = head;
while (node != NULL) {
printf("%d ", node->a);
node = node->next; //<=== crash on this line
}
return 0;
}
My output is always just the first entered int and then it all crashes on the marked line.
Upvotes: 1
Views: 3227
Reputation: 144740
Your main
loop uses the wrong variable:
int main(){
CreateList();
node = head;
while (next != NULL) {
printf("%d ", node->a);
node = node->next; //<=== crash on this line
}
return 0;
}
You should instead use node
:
int main(){
CreateList();
node = head;
while (node != NULL) {
printf("%d ", node->a);
node = node->next; //<=== crash on this line
}
return 0;
}
Incidentally, head
, node
and next
should be local variables, and head
should be returned by CreateList()
.
CreateList()
does not actually create the list correctly: nodes are not linked to the list as they are created, only the first node is stored in head
.
Here is a corrected version that returns the list and the corresponding main
function:
struct z { int a; struct z *next; };
struct z *CreateList(void) {
struct z *head, *node, *next;
int data, x = 1;
printf("Enter 0 to end\n");
printf("Enter data no. %d: ", x);
x++;
if (scanf("%d", &data) != 1 || data == 0)
return NULL;
head = malloc(sizeof(struct z));
if (head == NULL) {
printf("Error creating head");
return NULL;
}
node = head;
node->a = data;
node->next = NULL;
for (;;) {
printf("Enter data no. %d: ", x);
x++;
if (scanf("%d", &data) != 1 || data == 0)
break;
next = malloc(sizeof(struct z));
if (next == NULL) {
printf("Error creating next node no. %d", x - 1);
return NULL;
}
node->next = next;
node = next;
node->a = data;
node->next = NULL;
}
return head;
}
int main(void) {
struct z *head = CreateList();
struct z *node;
for (node = head; node != NULL; node = node->next) {
printf("%d ", node->a);
}
printf("\n");
return 0;
}
Upvotes: 1
Reputation: 1159
I think your problem is the global variables. Make them in the function, at least the node and the next. Create these on demand, for when you are actually adding the values. As a final tip, for this case, a do-while loop would make your code look cleaner than what it is right now, definitely you'd have less code repeat.
Upvotes: 0