I'm pretty newbie in C language and I have this little code in which I want to do some test in the main code.
struct ListNode
{
int val;
struct ListNode *next;
};
I want to create some examples (For example: 4 -> 9 -> 1) in the main code, but I don't really know how to initialize them. Thank you in advance.
I would do something like this:
struct ListNode* addnode(struct ListNode *head,int val)
{
struct ListNode* newnode = malloc(sizeof(*this));
newnode->val = val;
newnode->next = NULL;
if (!head) head = newnode;
else {
struct ListNode *this = head;
while(this->next)
this = this->next;
this->next = newnode;
}
return head;
}
int main(void)
{
struct ListNode *head = NULL;
head = addnode(head,4);
addnode(head,9);
addnode(head,1);
return 0;
}