#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node{
int data;
struct node * next;
};
struct node * init(int data){
struct node * head = (struct node *)malloc(sizeof(struct node *));
struct node * n = (struct node * )malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
head = n;
return head;
}
void add2tail(struct node * list, int data){
struct node * n = list;
while (n != NULL){
n = n->next;
}
struct node * newnode = (struct node * )malloc(sizeof(struct node));
n = newnode;
newnode->data = data;
newnode->next = NULL;
return;
}
void print(struct node * list){
struct node * n = list;
while (n != NULL){
printf("%d ", n->data);
n = n->next;
}
printf("\n");
return;
}
int main(void){
struct node * head = init(123);
print(head);
add2tail(head, 7);
print(head);
system("pause");
return 0;
}
代码如上,本人的思路是init创建一个节点和一个指针(都在堆上面),然后返回这个指针作为head指针,add2tail就是向链表尾部添加节点,但是代码无法正常运行,本人想了很久都没能理解,只能求帮助各位高手
解决方案
50
解决了记得结帖