所以我正在创建一个包含 3 个节点的链表,现在我正在尝试实现一个在 C 中显示列表的函数,代码如下:
struct node *createList(struct node *start)
{
int i, n, data;
printf("Enter the number of nodes : ");
scanf("%d", &n);
if (n==0)
return start;
printf("Enter the first element to be inserted : ");
scanf("%d", &data);
printf("%d",data);
start=insertInBeginning(start, data);
for(i=2; i<=n; i++)
{
printf("Enter the next element to be inserted : ");
scanf("%d", &data);
insertAtEnd(start, data);
}
return start;
}
// end of createList
struct node *insertInBeginning(struct node *start, int data)
{
printf("we in begining \n");
struct node *new_node,*current;
printf("declared nodes");
new_node = &data;
printf("got new_node data \n");
if(start == NULL) {
printf("start is null! \n");
start = new_node;
current = new_node;
} else {
printf("start isn't null! \n");
new_node->link=start;
start=new_node;
}
}
//end of insertInBeginning
void insertAtEnd(struct node *start, int data)
{
struct node *new_node,*current,*temp;
new_node->info = data;
if(start == NULL)
{
start= new_node;
current= new_node;
} else
{
temp = start;
while(temp->link!=NULL) {
temp = temp->link;
}
temp -> link = new_node;
}
}
// end of insertAtEnd
void displayList(struct node *start)
{
printf("we in displaylist");
struct node *temp;
temp = start->link;
printf("temp has start values?");
if(temp = NULL)
{
printf("we null now \n");
} else {
printf("we NOT null \n" );
while(temp!=NULL)
{
printf("%d \n",temp->info);
temp = temp->link;
}
}
}
我遇到的问题是,在显示函数中,循环完成了,但它没有打印任何值,但是我知道 temp 不为 NULL,所以它显示的东西,只是不是我想要的,是有办法解决这个问题吗?
最佳答案
这里有两个大问题:首先,(正如 Martin James 指出的),insertInBeginning() 例程没有返回,因此 start 设置为某个未定义的值。您还面临一个巨大的问题,即您只是将 int data
转换为 node *
。您必须为新节点 malloc() 内存并向其中写入数据。 (然后返回它)您的 insertAtEnd() 例程中也有同样的问题——您必须 malloc() 内存来保存新节点。
static struct node *
insertAtStart (struct node *start, int data)
{
struct node *new;
new = malloc(sizeof(*new));
if (new == NULL) {
perror("malloc");
exit(1);
}
new->value = data;
new->next = start;
return new;
}
static struct node *
insertAtEnd (struct node *start, int data)
{
struct node *cur, *new;
// create the node * we will be adding. (This really ought to have been done
// in the calling routine, so the same code would be used to create the node
// entry and to store the value. Note a big issue with this simple a struct,
// but more complex structs really ought to only be created in one place, so
// when they change, you won't have bugs where you forget to change one of
// the creation locations)
new = malloc(sizeof(*new));
if (new == NULL) {
perror("malloc");
exit(1);
}
new->value = data;
new->link = NULL;
// if this is going to be the only entry in the list, just return that it
// is the new start
if (start == NULL)
return new;
// find the last entry
for (cur = start ; cur->link != NULL ; cur = cur->link)
;
cur->link = new;
return start;
}
您确实想打开编译器警告 - 让编译器告诉您此类错误可以节省大量时间。如果您使用的是 gcc,请将 -Wall -Werror 添加到编译命令中(如果您使用的是不同的编译器,则需要阅读手册页)。
(您还可以打开其他 gcc 警告,但是 -Wall 为您提供了很多有用的警告。如果您想打开更多警告,可以阅读手册页来查找所有您想要的警告)
关于C: 无法显示链表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46517598/