1

I have been having trouble with this linked list specifically it seems like my head pointer is not linking to the rest of my list and I am confused as to why it is not. Where I insert my head pointer by pointer by reference it is not connected to the linked list referenced in main. unless the list is not linked together in the main function and I am missing something.

#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;
}
2
  • BTW int sum; --> int sum = 0; Commented Apr 23, 2017 at 17:39
  • It is similar to this Commented Apr 23, 2017 at 17:40

2 Answers 2

2

The error is that you are linking everything as next of head

  head -> next = newnode; 

You need to use a pointer that gets updated:

Nodeptr newnode = NULL;
Nodeptr last = head;
for(n=0;n<99;n++)
{
  newnode = malloc(sizeof(Nodeptr));  
  newnode->number = rand()%50+50;
  newnode->next =NULL;
  last -> next = newnode;
  last = last->next;
}

You should also change this:

head ->next = malloc(sizeof(Node)); // otherwise you will lose this element.

into

head ->next = NULL;
Sign up to request clarification or add additional context in comments.

3 Comments

I seem to be getting a memory leak somewhere.... when I run this program it crashes around the middle of list printing.
@GuillermoDiazGranados as you can see here it prints correctly. The error is not in the insert
Ah ok I just ran my code in that and it works fine and asked my friend to run it and it ran fine. huh ok then. Thank you for your help!
0

You execute these steps in a loop:

newnode = malloc(sizeof(Nodeptr));  
newnode->number = rand()%50+50;
newnode->next =NULL;
head -> next = newnode;

You are setting the newnode->next to point to null, and head->next to point to newnode.

This means, each time through the loop your head gets a new next, and that's it.

Effectively, each time you pass through the loop you drop the previous newnode on the floor, and link to a new one. At the end, you'll have head pointing to 1 node, and you'll have 98 nodes dropped on the floor that you can't reach.

You need to either maintain a "tail" pointer, or a copy of "head", and set head or tail or something to the most recent value of newnode. Then, you can set tail->next = newnode; tail = newnode; which will continually extend your list, rather than overwriting the same head->next each time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.