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.