问题
i received great help on my other questions in relation to this link list problem. My current problem is now that the head of the list remains null so i cannot actually link any nodes to it or print anything out to the console.
The problem is the insert_node function, so when print is called the while loop doesn't execute since head is null. I've got through it through the de bugger and it is definitely null it has the address 0x0.
Is this another malloc issue? im not too great on that yet.
code:
/*
* File: main.c
* Author: che16
*
* Created on 20 November 2013, 08:59
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structure.h"
/*
*
*/
node* head = NULL;
int main(int argc, char** argv) {
int no;
printf("enter amount of books \n");
scanf("%d", &no);
create_books(no);
print_list(head);
return (EXIT_SUCCESS);
}
node* create_books(int no_of_books) {
char title[50];
char author[30];
unsigned int number;
int i;
for (i = 0; i < no_of_books; i++) {
node* new_node;
new_node = (node *) malloc(sizeof (node));
printf("enter book title \n");
scanf("%s", title);
printf("enter author name \n");
scanf("%s", author);
printf("enter ISDN number \n");
scanf("%10u", &number);
strncpy(new_node->btitle, title, 40);
strncpy(new_node->name, author, 40);
new_node->isbn = number;
new_node->n = NULL;
insert_node(head, new_node);
}
}
void insert_node(node* head, node* insert) {
printf("insert called \n");
insert->n = NULL;
if (head == NULL) {
head = insert;
} else {
node* curr = head;
while (curr->n != NULL) {
curr = curr->n;
}
curr->n = insert;
}
printf("finished called \n");
}
void delete_node(node* head, node * node) {
}
void print_list(node * head) {
while (head) {
printf("%s: \"%s\" (%u)\n", head->btitle, head->name, head->isbn);
head = head->n;
}
}
SOLUTION
/*
* File: main.c
* Author: che16
*
* Created on 20 November 2013, 08:59
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structure.h"
/*
*
*/
node** head = NULL;
int main(int argc, char** argv) {
int no;
printf("enter amount of books \n");
scanf("%d", &no);
create_books(no);
print_list(head);
return (EXIT_SUCCESS);
}
node* create_books(int no_of_books) {
char title[50];
char author[30];
unsigned int number;
int i;
for (i = 0; i < no_of_books; i++) {
node* new_node;
new_node = (node *) malloc(sizeof (node));
printf("enter book title \n");
scanf("%s", title);
printf("enter author name \n");
scanf("%s", author);
printf("enter ISDN number \n");
scanf("%10u", &number);
strncpy(new_node->btitle, title, 40);
strncpy(new_node->name, author, 40);
new_node->isbn = number;
new_node->n = NULL;
insert_node(&head, new_node);
}
}
void insert_node(node** head, node* insert) {
printf("insert called \n");
insert->n = NULL;
if (*head == NULL) {
*head = insert;
} else {
node* curr = *head;
while (curr->n != NULL) {
curr = curr->n;
}
curr->n = insert;
}
printf("finished called \n");
}
void delete_node(node* head, node * node) {
}
void print_list(node * head) {
while (head) {
printf("%s: \"%s\" (%u)\n", head->btitle, head->name, head->isbn);
head = head->n;
}
}
回答1:
The problem is that you're passing head
as a local variable into the insert_node
function, rather than using the global one.
You have three options here:
Make all your functions act on the global
head
and not pass it around (not recommended);Pass
head
as anode**
, so that functions can change thenode*
value it points to;Use a dummy head strategy where the head is an empty list node that is never NULL, and never changes. You use its
next
pointer as the actual list head. This approach tends to make all your list code much simpler at the expense of a little wasted memory.
回答2:
This is because of your insert_node
function, where you set the local variable head = insert;
. The global head variable remains untouched. You should change your function as follows: insert_node(node*& head, node* insert)
. With that, you pass a reference to the global head.
Edit: yes, sorry, in C there are no references. So see answer #2 from paddy...
来源:https://stackoverflow.com/questions/20108412/c-linked-list-why-is-my-list-head-variable-remaining-null-new-to-c