Не работает сортировка списка

Для каждой строки, состоящей из подстрок, разделенных точкой с
запятой, сформировать новую строку путем удаления из исходной строки всех
символов, которые встречаются в каждой из введенных подстрок.

Например на ввод:aaaabsdn;bdam;cdbda а на выводе должно быть :sn;m;c, т.е. удаляются все одинаковые буквы, содержащиеся в словах, разделенных “;”. Ошибка в функции sort(segmentation fault).Скорее всего ошибся в алгоритме. Никак не могу разобраться. Реализовать надо обязательно с помощью списков.

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct table
{
    char value;
    struct table* next;
}table;
 
typedef struct mstring
{
    table* head;
    table* tail;
 
}mstring;
 
void add(mstring* a,  int c)
{
    if (a->head == NULL)
    {
        table* node = malloc(sizeof(table));
        node->value = c;
        node->next = NULL;
        a->head = a->tail = node;
    }
    else
    {
        table* node = malloc(sizeof(table));
        node->value = c;
        node->next = NULL;
        a->tail->next = node;
        a->tail = node;
    }
}
 
void destroy(mstring* a)
{
    table* pointer = a->head;
    table* pointernext = pointer->next;
    while (pointer->next != NULL)
    {
        free(pointer);
        pointer = pointernext;
        pointernext = pointer->next;
    }
    free(pointer);
    free(pointernext);
}
 
void DeleteLikeFirst(mstring* a)
{
    table* pointer = a->head;
    table* pointernext = pointer->next;
    while (pointernext != NULL)
    {
        if (pointernext->value == a->head->value)
        {
            if (pointernext->next)
                pointer->next = pointernext->next;
            else
                pointer->next = NULL;
            free(pointernext);
            pointernext = pointer->next;
            continue;
        }
        pointer = pointer->next;
        pointernext = pointer->next;
    }
    pointernext = a->head;
    a->head = a->head->next;
    free(pointernext);
}
 
void sort(mstring* a)
{
    while((a->tail->value!=59)&&(a->tail)){
        a->tail = a->tail->next;
    }
    point:
        a->tail = a->tail->next;
        
        if((a->tail)&&(a->tail->value!=59)){
            if(a->tail->value == a->head->value){
                while(a->tail->value!=59||(a->tail))
                    a->tail = a->tail->next;
                if(a->tail==NULL){
                    if(a->head->value!=59){
                        DeleteLikeFirst(a);
                    //  a->head = a->head->next;
                        a->tail = a->head;
                        sort(a);          }
                    else print(a);
                                 }
                else goto point;
            }
            else goto point;
        }
        else {
        if(a->head->next->value!=59){
        a->head = a->head->next;
        a->tail = a->head;
        sort(a);
             }
        else print(a);//prls(list);
            }
        //
}
 
 
void print(mstring* a)
{
    printf("\n");
    table* t = (table*)malloc(sizeof(table));
    for (t = a->head; t != NULL; t = t->next)
        printf("%c", t->value);
    free(t);
    destroy(a);
    free(a);
}
 
int main()
{
    
    for(;;){
    
    mstring* a = (mstring*)malloc(1 * sizeof(mstring));
    a->head = a->tail = NULL;
    add(a, getchar());
    if(a->tail->value==10){
        printf("Error!\n");
        continue;}
    while(a->tail->value!=10){
    
    add(a, getchar());
}
table* p = a->head;
while(a->head->next->value!=10){
    a->head = a->head->next;
}
a->tail = a->head;
a->head->next = NULL;
a->head = p;
 
    a->tail = a->head;
    sort(a);
    print(a);
    
    printf("\n");
 
}
 
    return 0;
}

Что такое 59? :thinking:

Скорее всего где-то обращение по NULL указателю.

Обычно студия и другие IDE в режиме отладки показывают на какой примерно строке упало.

Это код символа “;” в таблице ASCII. Это потому что я getcharом ввожу элементы

В С char это просто число, 1 байт.

if (59 == ';') // true

И про getchar гугл говорит, что оно возвращает int только потому что кроме символов оно еще может вернуть EOF, так что можно скастовать к char.

Вообще я бы завел константу (DELIMITER, …) или функцию (isDelimiter(ch), …) вместо повторения ; или 59 везде.