В этой программе я пытаюсь реализовать связанный список, используя массив. Я инициализировал элементы массива tabList равным -1, чтобы я мог рассматривать каждый элемент массива со значением -1 как пустое поле, которое я могу использовать для хранения нового элемента в списке.
I получаю ошибку сегментации при попытке запустить эту программу. Не могли бы вы объяснить, в чем заключается ошибка в моем коде
Я попытался отладить свой код. В строке 100 * есть ошибка 75: while (list.tabList [i] .valeurElem! = - 1)
Вот мой код:
typedef struct elementList
{
int valeurElem;
int indexSuivant;
int indexElem;
} elementList;
typedef struct list{
elementList tabList[tailleMax];
int debut;
int fin;
} listT;
// To initialize the list
void initListT(listT list)
{
for(int i = 0; i < tailleMax; i++)
{
list.tabList[i].valeurElem = -1;
list.tabList[i].indexSuivant = -1;
list.tabList[i].indexElem = -1;
}
}
// To create a new linked list
listT creerListeT()
{
listT alist;
initListT(alist);
alist.debut = 0;
alist.fin = 0;
return alist;
}
// To test if the list is empty
bool estVideListT(listT list)
{
return list.tabList[ 0 ].valeurElem == -1 ;
}
// Function To insert in the head of the linked list
listT insererTeteListT(listT list, int elemInserer)
{
if( estVideListT(list) )
{
int a = list.debut;
list.tabList[ a ].valeurElem = elemInserer;
list.tabList[ a ].indexSuivant = list.debut + 1;
list.tabList[ a ].indexElem = list.debut;
return list;
}
else
{
int i = 0;
while(list.tabList[i].valeurElem != -1)
{
i++;
}
list.tabList[ i ].valeurElem = elemInserer;
list.tabList[ i ].indexSuivant = list.debut;
list.debut = i;
return list;
}
}
void printList(listT list)
{
if( estVideListT(list) ) // testing if the array is empty
{
printf("The array is empty\n");
}
else
{
int i = list.debut;
while( list.tabList[i].indexSuivant != -1 )
{
printf("Element with index %d is : %d \n", i, list.tabList[i].valeurElem);
i++;
}
printf("Element with index %d (Last element) is : %d \n", i, list.tabList[i].valeurElem);
}
}
int main()
{
// Creating the list
listT myList = creerListeT();
// Insertion of the elements in the list (each element in the head of the list)
insererTeteListT(myList, 5);
insererTeteListT(myList, 3);
insererTeteListT(myList, 2);
insererTeteListT(myList, 4);
insererTeteListT(myList, 7);
// Printing the list
printList(myList);
return 0;
}