0
這段代碼應該打印:'tree two one' 但它不起作用。 (new_nod沒有添加到mylist的前面) 有人知道爲什麼嗎? (實際上在這段代碼中,我想使用一個指針指向另一個函數中的指針輸入,但它不起作用(沒有更改應用於mylist)。但是,它在直接在main中使用add_front函數時有效。不能在另一個函數中使用具有「指向指針」輸入的函數。在C語言
#include <stdio.h>
#include <stdlib.h>
struct node{
char *word;
struct node *next;
};
struct node* create_node(char *str){
struct node *s;
s = (struct node *)malloc(sizeof(struct node));
if(s==NULL){
printf("couldn't malloc :(\n");
exit(-1);
}
s->word = str;
s->next = NULL;
return s;
}
void print_node(struct node *list){
struct node *current;
for(current = list; current !=NULL; current = current->next)
printf("%s ", current->word);
}
void add_front(struct node **list, struct node *new_node){
new_node->next= *list;
*list = new_node;}
void func(struct node*list, struct node*new_node){
add_front(&list, new_node);
}
int main()
{
struct node* mylist = create_node("one");
mylist->next = create_node("two");
struct node *new_node = create_node("tree");
func(mylist, new_node);
print_node(mylist);
}