0

I wanted to attach two linked lists using concat() function, but I have noticed that concat() returns the last value of head although there is no return at the end of concat()!

what I mean is when I send the heads of the following two linked lists to concat() : 1->2->3->4 and 5->6->7

I do expect an ouput like this :1->2->3->4->5->6->7 ,but instead I get a linked list of this form 4->5->6->7!

can I know where's the problem? thanks in advance

typedef struct node{
int data;
struct node* next;
}list;
list* Concat(list* head,list* P)
{
    if(!head)   return  P;
    if(!P)  return head;
    while(head->next)   head=head->next;
    head->next=P;
}
LEARNER
  • 123
  • 1
  • 9
  • 5
    `can I know where's the problem?` The problem is `there is no return at the end of concat()` – tkausl Oct 24 '20 at 17:03
  • 1
    If the function ends without executing a `return` statement it causes undefined behavior. – Barmar Oct 24 '20 at 17:04
  • 1
    Your code is invalid. A function with a signature other than `void ...` without `return` is undefined behavior. – Marco Bonelli Oct 24 '20 at 17:04
  • 2
    Undefined behaviour. If it works, the value happened to be in the right place to be found accidentally. – Weather Vane Oct 24 '20 at 17:04
  • 3
    What actually gets returned is unspecified, although in many implementations it will end up being the value of the last statement that was executed because it happens to use the same register used for return values. – Barmar Oct 24 '20 at 17:05
  • Didn't you get any compiler warnings? Compile with `-Wall`. – Jabberwocky Oct 24 '20 at 17:22

0 Answers0