C链表为什么我的列表头变量仍然为空(对C来说是新的)
关于这个链接列表问题,我在其他问题上得到了很大的帮助。我当前的问题是列表的头仍然是空的,因此我无法将任何节点链接到它,也无法将任何内容打印到控制台 问题在于insert_node函数,因此当调用print时,while循环不会执行,因为head为null。我已经通过de bugger完成了,它肯定是空的,它的地址是0x0 这是另一个malloc问题吗?我在这方面还不太在行 代码:C链表为什么我的列表头变量仍然为空(对C来说是新的),c,linked-list,nodes,C,Linked List,Nodes,关于这个链接列表问题,我在其他问题上得到了很大的帮助。我当前的问题是列表的头仍然是空的,因此我无法将任何节点链接到它,也无法将任何内容打印到控制台 问题在于insert_node函数,因此当调用print时,while循环不会执行,因为head为null。我已经通过de bugger完成了,它肯定是空的,它的地址是0x0 这是另一个malloc问题吗?我在这方面还不太在行 代码: /* *文件:main.c *作者:che16 * *创建于2013年11月20日,08:59 */ #包括 #包括
/*
*文件:main.c
*作者:che16
*
*创建于2013年11月20日,08:59
*/
#包括
#包括
#包括
#包括“structure.h”
/*
*
*/
node*head=NULL;
int main(int argc,字符**argv){
国际贸易编号;
printf(“输入图书数量”);
scanf(“%d”和“否”);
创建_书籍(否);
打印列表(标题);
返回(退出成功);
}
节点*创建书籍(书籍的整数编号){
字符标题[50];
char作者[30];
无符号整数;
int i;
for(i=0;i标题,标题,40);
strncpy(新节点->名称、作者,40);
新建节点->isbn=编号;
新建_节点->n=NULL;
插入_节点(头,新_节点);
}
}
无效插入节点(节点*头,节点*插入){
printf(“插入名为\n”);
插入->n=NULL;
if(head==NULL){
头=插入;
}否则{
节点*curr=头部;
while(curr->n!=NULL){
当前=当前->n;
}
当前->n=插入;
}
printf(“已完成调用\n”);
}
void delete_节点(节点*头,节点*节点){
}
无效打印列表(节点*头){
while(head){
printf(“%s:\%s\”(%u)\n),标题->标题,标题->名称,标题->isbn);
头=头->n;
}
}
解决方案
/*
* 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;
}
}
/*
*文件:main.c
*作者:che16
*
*创建于2013年11月20日,08:59
*/
#包括
#包括
#包括
#包括“structure.h”
/*
*
*/
节点**头=空;
int main(int argc,字符**argv){
国际贸易编号;
printf(“输入图书数量”);
scanf(“%d”和“否”);
创建_书籍(否);
打印列表(标题);
返回(退出成功);
}
节点*创建书籍(书籍的整数编号){
字符标题[50];
char作者[30];
无符号整数;
int i;
for(i=0;i标题,标题,40);
strncpy(新节点->名称、作者,40);
新建节点->isbn=编号;
新建_节点->n=NULL;
插入_节点(&头,新_节点);
}
}
无效插入节点(节点**头部,节点*插入){
printf(“插入名为\n”);
插入->n=NULL;
如果(*head==NULL){
*头=插入;
}否则{
节点*curr=*头;
while(curr->n!=NULL){
当前=当前->n;
}
当前->n=插入;
}
printf(“已完成调用\n”);
}
void delete_节点(节点*头,节点*节点){
}
无效打印列表(节点*头){
while(head){
printf(“%s:\%s\”(%u)\n),标题->标题,标题->名称,标题->isbn);
头=头->n;
}
}
问题在于,您将head
作为局部变量传递到insert\u节点
函数中,而不是使用全局变量
这里有三个选项:
头部
,而不是将其传递(不推荐)head
作为节点**
传递,以便函数可以更改它指向的节点*
值next
指针作为实际的列表头。这种方法倾向于使您的所有列表代码更加简单,但会浪费一些内存这是因为您的
insert\u节点
函数设置了局部变量head=insert代码>。全局head变量保持不变。您应该如下更改您的函数:insert\u node(node*&head,node*insert)
。这样,您就可以将引用传递给全局负责人
编辑:是的,对不起,C语言中没有引用。因此,请参阅paddy的答案#2…(猜测时根本不看代码:),因为您修改了一个局部变量,而应该用指针来传递它?(查看代码。意识到是对的。撞到附近的墙上。)结构中有什么。h?创建书籍和打印列表有什么作用?@H2CO3似乎是“不知道传递价值/参考”的季节…@metsales你是我的英雄。从来没有想过#3,但对我来说似乎是最优雅的解决方案,所以你得到一个+0.9999999999999,四舍五入为1。@paddy工作表不允许第一部分使用假头,后面的部分可以。我不知道如何实现**我们还没有被展示,有没有一个例子?对不起,我的C不好,但我正在努力!我试着将insert_node设置为insert_node(节点**节点,节点*节点),但这不起作用。我在传递head时缺少了一个&我会更新原件,谢谢。C中没有引用类型。node*&head
与node head
不是一回事吗?@metsales否,这不是操作员的地址,这是C++的引用限定符,在C.中它不起作用。
/*
* 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;
}
}