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; } 
  • @dropitlikeitshot, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

1 answer 1

Because after the second init() call, the variables head and tail reflect the list of l2 (even when you call append(l1) , etc.).


By the way, after delete , you are accessing data in an already freed memory. This is wrong and the behavior (whether or not the program crashes) depends on the version of the memory allocator.


The idea of ​​using dummy elements at the beginning and at the end of an IMHO list is rather strange, just like the last element of the list (dummy), which loops over itself.

Update

@dropitlikeitshot , what's with any dummy node?

As far as I can see the code, you are trying to make 2 lists (in main) l1 and l2, but for both you operate in your functions with the same global head and tail .

That's where you all mixed up. Throw them away.

Make a structure containing for each list its head and tail. And here is a pointer to such a structure (you will have 2 lists of l1 and l2) and pass in functions that manipulate lists.


Now you just want to invent common algorithms for working with lists? An exciting experience.

  • @avp, that is, in fact, I can write in this code just append (v), not l2 = append (v), because the append function therefore operates on the head, which reflects l2? --- And what alternative to dummy nod'am do you offer? - dropitlikeitshot
  • one
    Updated the answer - avp
  • one
    @avp thanx) It is sometimes useful to invent a bicycle for programming practice. - dropitlikeitshot
  • @dropitlikeitshot, but I also love such cycling. It is advisable to attach instructions in Russian only to your bikes (comments before the functions init, append, insert ... what exactly you intend to do ). - By the way, well, and how did the bike show itself with a sign of the end of the list as a pointer to itself instead of NULL? More comfortable than usual? (I am not ironic, in general, but just interested). - avp
  • @avp well, I try .. - dropitlikeitshot