0

In this program, I'm trying to implement a linked list using an array. I initialised the elements of the array tabList to -1 so that I can consider every element of the array whose value is -1 as an empty box that i can use to store a new element in the list.

I am getting segmentation error when trying to run this program. Can you explain please what is the error in my code

I tried to debug my code, It says that there's an error in line 75 : while(list.tabList[i].valeurElem != -1)

Here is my code :

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;
}
1
  • 1
    Please include code in your question, not a link to code. Have you debugged the program to determine anything about the location of the error or values of variables at the time? You can edit your question to make any improvements. Commented Apr 11, 2020 at 17:02

2 Answers 2

1

In the following code:

// 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;

    }

}

Since you are passing list by value, all of your changes are local to the list variable in this function.

Sign up to request clarification or add additional context in comments.

Comments

0

here is a working example as per to your base-code, Do not forget to release the memory corresponding to malloc. I will leave that as an exercise for you to en devour upon. Additionally, you also need to check boundary conditions associated to tailleMax.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define tailleMax 100


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 = (listT *)malloc(sizeof(listT));

    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 inser 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 // Cas de liste non vide
    {
        int i = 0;

        // On cherche l'indice de la première case vide
        while(list->tabList[i].valeurElem != -1)
        {
            i++;
        }

        // Un élément est dans la tête de la liste s'il n'y aucun élément qui le pointe
        list->tabList[ i ].valeurElem   = elemInserer;
        list->tabList[ i ].indexSuivant = list->debut;
        list->debut = i;

        return list;
    }

}


// Function to print the elements of  the list 
void printList(listT *list)
{
    if( estVideListT(list) )  // testing if the array is empty
    {
        printf("The array is empty\n");
    }
    else
    {
        // To revise
        int i = 0;

        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-1, list->tabList[i-1].valeurElem);

    }
}

int main()
{
    // Creating the list
    listT *myList =  creerListeT();

    // Insertion of the elements in the list 
    insererTeteListT(myList, 5);
    insererTeteListT(myList, 3);
    insererTeteListT(myList, 2);
    insererTeteListT(myList, 4);
    insererTeteListT(myList, 7);

    // Printing the list 
    printList(myList);

    return 0;
}

2 Comments

Thanks. It really helps me
Welcome; The code requires a lot of work. Hope you figure it out as you progress. Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.