Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • what if I want to insert at the end of the list? SO like 1 2 3 4 5 ... 1 -> 2 -> 3 -> 4 -> 5 -> NULL Commented Jan 27, 2013 at 5:04
  • @Masterminder. Then you need a pointer to the head which is set when you allocate the very first node. After that you keep a pointer to the last node similar to Quirliom's answer... I could write this code for you but you'd enjoy it more figuring it our yourself. :-) Commented Jan 27, 2013 at 5:06
  • :D Well I know I would have to keep a pointer to the very beginning and just keep adding but what I am unsure of is that to write code so I do not have to make an unnecessary allocation, like I do not want to this 1 2 3 4 5 plus an extra node, what I want to do is 1-> 2-> 3-> 4-> 5 -> NULL Commented Jan 27, 2013 at 5:08
  • @Masterminder. You can do that with an if statement, check if the head is NULL as a special case. If it is, allocate new node and assign it to the head. If it's not allocate new node in ptr->next. Commented Jan 27, 2013 at 5:12
  • 1
    @Masterminder: yes, assigning one pointer to another means they contain the same address which means point to the same thing. Commented Jan 27, 2013 at 5:30