我一直有这个链表的问题,特别是它似乎像我的头指针没有链接到我的列表的其余部分,我很困惑,为什么它不是。在我通过引用指针插入我的头指针的地方,它没有连接到main中引用的链表。除非该列表在主函数中没有链接在一起,而我错过了一些东西。链接列表的问题列表没有链接到头指针
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int number;
struct node * next;
} Node;
typedef Node * Nodeptr;
void printlist (Node * head){
Node * n = head;
while(n != NULL){
printf("%d\n",n ->number);
n = n ->next;
}
}
void sumlist (Node * head){
Node * n = head;
int sum;
while(n != NULL){
sum = n ->number +sum;
n = n ->next;
}
printf("the total of all numbers in this list is %d",sum);
}
search(head){
}
int main(){
int i =0;
Nodeptr head=NULL;
if((head = malloc(sizeof(Node))) == NULL)
return 0;
head->number =rand()%50+50;
head ->next = malloc(sizeof(Node));
int n;
Nodeptr newnode = NULL;
for(n=0;n<99;n++)
{
newnode = malloc(sizeof(Nodeptr));
newnode->number = rand()%50+50;
newnode->next =NULL;
head -> next = newnode;
}
printlist(head);
sumlist(head);
return 0;
}
BTW'int sum;' - >'int sum = 0;' – BLUEPIXY
它与[this]类似(http://stackoverflow.com/q/43564342/971127) – BLUEPIXY