创建一个链表+添加新节点+打印列表,但无法弄清楚为什么它不起作用
creating a linked list + add new node + print the list and cannot figure out why it's not working
所以我正在尝试创建一个链表,它具有在列表的开头添加一个新节点的功能,然后打印出列表。我是一名新手编码员,仍在学习如何执行此操作,当我执行代码时,没有打印任何内容..
#include <stdio.h>
typedef char DATA;
struct node
{
DATA d;
struct node *next;
};
int main()
{
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
struct node *addnew(node, d, header)
{
struct node *addnew = header;
addnew->d = 'k';
addnew->next = header;
return addnew;
}
int printList(node, next, header)
{
struct node *current = header;
while (next != 'NULL')
{
printf('/c', current->d);
current = current->next;
}
return current->d;
}
程序可以如下所示(未经测试)
#include <stdio.h>
#include <stdlib.h>
typedef char Data ;
struct node
{
Data d ;
struct node *next ;
};
struct node * addNew( struct node *head, Data value )
{
struct node *new_node = malloc( sizeof( struct node ) );
if ( new_node != NULL )
{
new_node->d = value;
new_node->next = head;
head = new_node;
}
return head;
}
void printList( struct node *head )
{
for ( ; head; head = head->next ) printf( "%c ", head->d );
}
struct node * freeList( struct node *head )
{
while ( head != NULL )
{
struct node *tmp = head;
head = head->next;
free( tmp );
}
return head;
}
int main( void )
{
struct node *head = NULL;
head = addNew( head, 'a' );
head = addNew( head, 'b' );
head = addNew( head, 'c' );
printList( head );
printf( "\n" );
head = freeList( head );
}
至于你的代码,那么它包含很多错误。例如,您没有为列表的元素分配内存。您只定义了指向未分配元素的指针,并将 NULL 分配给这些指针。
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
接下来的语句
header->d = 'a';
header->next = second;
//..
错了。
还有这个声明
struct addnew;
没有意义。
并且您没有正确定义函数。
@jazuze 很多错误。就算我在学链表,也没关系。我在下面评论了您需要更正的区域。我已经编译了您的代码并进行了更正,并且它可以正常工作,但我不会轻易为您提供解决方案。我建议你在下面工作::
#include <stdio.h>
typedef char DATA ; //why do you need to typedef char
struct node {
DATA d ;
struct node * next ;
};
int main (){
struct node* header = NULL;
struct node* second = NULL;
struct node* third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
// place all functions before main
struct node* addnew (node, d, header){ //should be struct node* addnew(struct node* header). This whole function needs to be corrected
struct node* addnew = header; //incorrect logic. You need to malloc addnew first so that you get a new node in memory
addnew->d = 'k';
addnew->next = header;
return addnew; //you lose the header
}
int printList(node, next, header){ //(node, next, header) what is this?? It should be printList(struct node* header)
struct node *current = header;
while (next != 'NULL'){ // incorrect logic. What is next. Should be (current != NULL)
printf('/c', current->d); //should be printf(“%c”, current->d);
current = current->next;
}
return current->d; //why return current->d
}
所以我正在尝试创建一个链表,它具有在列表的开头添加一个新节点的功能,然后打印出列表。我是一名新手编码员,仍在学习如何执行此操作,当我执行代码时,没有打印任何内容..
#include <stdio.h>
typedef char DATA;
struct node
{
DATA d;
struct node *next;
};
int main()
{
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
struct node *addnew(node, d, header)
{
struct node *addnew = header;
addnew->d = 'k';
addnew->next = header;
return addnew;
}
int printList(node, next, header)
{
struct node *current = header;
while (next != 'NULL')
{
printf('/c', current->d);
current = current->next;
}
return current->d;
}
程序可以如下所示(未经测试)
#include <stdio.h>
#include <stdlib.h>
typedef char Data ;
struct node
{
Data d ;
struct node *next ;
};
struct node * addNew( struct node *head, Data value )
{
struct node *new_node = malloc( sizeof( struct node ) );
if ( new_node != NULL )
{
new_node->d = value;
new_node->next = head;
head = new_node;
}
return head;
}
void printList( struct node *head )
{
for ( ; head; head = head->next ) printf( "%c ", head->d );
}
struct node * freeList( struct node *head )
{
while ( head != NULL )
{
struct node *tmp = head;
head = head->next;
free( tmp );
}
return head;
}
int main( void )
{
struct node *head = NULL;
head = addNew( head, 'a' );
head = addNew( head, 'b' );
head = addNew( head, 'c' );
printList( head );
printf( "\n" );
head = freeList( head );
}
至于你的代码,那么它包含很多错误。例如,您没有为列表的元素分配内存。您只定义了指向未分配元素的指针,并将 NULL 分配给这些指针。
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
接下来的语句
header->d = 'a';
header->next = second;
//..
错了。
还有这个声明
struct addnew;
没有意义。
并且您没有正确定义函数。
@jazuze 很多错误。就算我在学链表,也没关系。我在下面评论了您需要更正的区域。我已经编译了您的代码并进行了更正,并且它可以正常工作,但我不会轻易为您提供解决方案。我建议你在下面工作::
#include <stdio.h>
typedef char DATA ; //why do you need to typedef char
struct node {
DATA d ;
struct node * next ;
};
int main (){
struct node* header = NULL;
struct node* second = NULL;
struct node* third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
// place all functions before main
struct node* addnew (node, d, header){ //should be struct node* addnew(struct node* header). This whole function needs to be corrected
struct node* addnew = header; //incorrect logic. You need to malloc addnew first so that you get a new node in memory
addnew->d = 'k';
addnew->next = header;
return addnew; //you lose the header
}
int printList(node, next, header){ //(node, next, header) what is this?? It should be printList(struct node* header)
struct node *current = header;
while (next != 'NULL'){ // incorrect logic. What is next. Should be (current != NULL)
printf('/c', current->d); //should be printf(“%c”, current->d);
current = current->next;
}
return current->d; //why return current->d
}