It seems to be a hitch that in the "Udal" function after deleting the second element, the pointer of the first element should point to the beginning of the third. Honestly, this is a lab task. I especially need advice now.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define MAXDL 9 int h = 0; struct EL_SP { char id[MAXDL]; struct EL_SP *sled; }; void Vkl(struct EL_SP **p, char t_id[]) { struct EL_SP *pt, *k, *j; pt = (struct EL_SP *) malloc(sizeof(struct EL_SP)); strcpy(pt->id, t_id); if (*p == NULL || strcmp(pt->id, (*p)->id) < 0) { pt->sled = *p; *p = pt; } else { k = *p; while (k != NULL && strcmp(pt->id, k->id) >= 0) { j = k; k = k->sled; } j->sled = pt; pt->sled = k; } } // функция для удаления и вывода результата после комментария avp: void Udal(struct EL_SP *p) { struct EL_SP *next; int m; for( next=p, m=0; next!=NULL; next=next->sled) { m++; if (m==2) free (next); break; } } void PechSp(struct EL_SP *p) { struct EL_SP *i; printf("n Rezultat:n"); for (i = p; i != NULL; i = i->sled) puts(i->id); } void main() { struct EL_SP *p; unsigned n; unsigned i; char t_id[MAXDL]; printf("n vvedite chislo identificatorovn n="); scanf("%u", &n); getchar(); p = NULL; printf("vvedite identificatori"); printf("(posle kagdogo najimayte klavishu <ENTER>) n"); for (i = 1; i <= n; i++) { gets(t_id); Vkl(&p, t_id); } Udal(p); //здесь её вызываю PechSp(p); printf("nn Dlya zaversheniya najmite lybuyu klavishun"); getch(); } 

The program is compiled without errors, but after entering the identifiers crashes.

  • Straustrup went to punish himself ... =) Go through the list, look at the index, if that is necessary, delete the value - Gorets
  • In the first element, replace sled with sled from the second. Second remove. Just look carefully that the 1st and 2nd exist. - avp

1 answer 1

@AlexC , you are confusing work with pointers in arrays and linked lists (this is how you have it. Struct EL_SP refers to another struct EL_SP at the address in the sled field).

Check the linked lists again and change the logic for building and traversing the list.

I advise you to associate with the list 2 pointers - head (at the first in the list) and tail (at the last). For example, you can define a list like this:

 struct list { struct EL_SP *head, *tail; int list_length; }; 

When initializing (creating an empty list) make head = tail = NULL.

Good luck.

  • @avp, and if an element is suddenly added to the end of the list, then will it be necessary to correct all tails? - insolor
  • @insolor, apparently I wrote something incomprehensible in the answer. It was a comment and I did not refer in detail to the data structures from the question. For each list, the struct list has its own ( one copy per list ). One head, one tail. head points to the first struct EL_SP, the pointer sled ( in it ) to the next struct EL_SP, etc. The tail of the struct list adds the last struct EL_SP in the list. sled in it is null. IMHO obvious. - avp
  • @avp, that's it, got it. Just do not read back. - insolor