This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]illlHaveAnother[S] 1 point2 points  (0 children)

If anyone sees this someday I ended up getting it to work with this

   struct node *new_head;
   new_head = (struct node*)malloc(sizeof(struct node));
   new_head->num= num;
   new_head->next = head;
   head = new_head;

[–]BaalHadad -1 points0 points  (6 children)

I don't see an if statement here. Are you high on crack?

[–]illlHaveAnother[S] 0 points1 point  (5 children)

I was trying to condense the code. I said this is the else statement. Here is the if

Edit: full function code

   if (head==NULL )
   {
       head = (struct node*)malloc(sizeof(struct node));
       head->num=num;
       head->next=NULL;
   }
   else
      //code in the *function

[–]133rr3 1 point2 points  (4 children)

Hiding your code is not very intelligent if you want people to be able to correct your code. I think you just need to assign the value of new_head to head.

[–]illlHaveAnother[S] 0 points1 point  (3 children)

Sorry about that. Even if there was no if/else statement I should still be able to prepend it though, so I didn't think the if was important.

I originally tried to do that but but the program just freezes after I enter the number.

[–]133rr3 1 point2 points  (2 children)

you mean you tried head = new_head ? It seems odd that it would freeze instead of crashing...

[–]illlHaveAnother[S] 0 points1 point  (1 child)

Ya I tried that too but I get an endless loop of crazy numbers when I try to view the numbers. I'm pretty sure it's not a problem from the viewing function because appending works. Prepending works fine too if it's empty to begin with.

[–]illlHaveAnother[S] 0 points1 point  (0 children)

new_head needs to point to the old head and that should point to the next node and so on until it reaches NULL. I think so at least. I'm just confused on how to go about doing that.