I am studying now independently linked lists C. Can anyone say why, despite the fact that in the last while loop I only output l1, l2 is also somehow output?
#include <stdio.h> #include <stdlib.h> //dummy=head struct node { int value; struct node *next; }; struct node *head; struct node *tail; struct node *init(void) { head=(struct node *) malloc(sizeof *head); tail=(struct node *) malloc(sizeof *head); head->next = tail; tail->next = tail; return head; } struct node *append(int v) { struct node *ptr; struct node *t; ptr=head; while (ptr->next != tail) ptr = ptr->next; t=(struct node *) malloc(sizeof *t); t->value=v; t->next = tail; ptr->next = t; return ptr; } struct node *insert(int v, struct node *ptr) { struct node *t; t=(struct node *) malloc(sizeof *t); ptr->value=v; t->value=v; t->next = tail; ptr->next = t; return ptr; } void delete(struct node *ptr) { struct node *t; t = ptr->next; ptr->next = ptr->next->next; free(t); } int main () { struct node *l1, *l2; int n, v; scanf("%d", &n); l1=init(); l2=init(); int i=0; while(i<n){ scanf("%d", &v); l1=append(v); i++;} l1=l1->next; scanf("%d", &v); l1=insert(v, l1); delete(l1); i=0; while(i<n){ scanf("%d", &v); l2=append(v); i++; } l2=l2->next; scanf("%d", &v); l2=insert(v, l2); delete(l2); l1=head->next; while (l1 != tail) { printf("%d\n",l1->value); l1 = l1->next; } return 0; }