2016-12-07 17 views
1

Pour un exercice que je fais sur les pointeurs en C, je veux insérer une structure au début d'une liste chaînée en envoyant le pointeur listStart à la fonction insertEntry avec la structure que je veux insérer . Cependant, avec ce code actuel, je suis incapable de faire cela car le pointeur listStart ne porte pas avec lui l'adresse de ce qu'il pointait dans la fonction principale (la toute première structure de la liste).Envoyer le pointeur de la liste à la fonction

Je comprends que je ne fais que copier le pointeur lui-même dans la fonction insertEntry et donc l'adresse vers laquelle il pointait est omise. Cela signifie que tout ce que j'obtiens du pointeur listStart dans la fonction insertEntry est un pointeur nul.

Pour résoudre ce problème, j'ai essayé d'envoyer le listStart à la fonction insertEntry comme un pointeur, cependant, cela m'a juste donné un pointeur vers un pointeur qui pointe vers null. J'ai essayé d'envoyer l'adresse de listStart à la fonction qui ne fonctionnait pas car elle envoyait null à la fonction.

Les questions que j'ai: est-ce possible de le faire et je manque juste quelque chose? Ou est-ce pas possible?

Merci à l'avance.

// header to include standard input and output 
#include <stdio.h> 

struct entry 
{ 
    int value; 
    struct entry *next; 
}; 


// prototype for insertEntry function 
void insertEntry(struct entry *l, struct entry *i, struct entry *j); 

int main(void) 
{ 

    // declaration of array for linked list 
    struct entry list1 = { 1 }, list2 = { 2 }, list3 = { 3 }, list4 = { 4 }, list5 = { 5 }, insert = { 8 }; 
    struct entry *listStart = &list1; 

    // test to see if the value of the insert.value struct is correct 
    printf("insert.value = %i\n", insert.value); 


    // assign pointers in list.next to the next struct in the list to create a linked list 
    list1.next = &list2; 
    list2.next = &list3; 
    list3.next = &list4; 
    list4.next = &list5; 
    list5.next = (struct entry *) 0; 

    // print the linked list to make sure the pointers are going to the correct struct member 
    printf("Original list!\n"); 
    while (listStart != (struct entry *) 0) 
    { 
     printf ("%i\n", listStart->value); 
     listStart = listStart->next; 
    } 

    // send struct to change and struct to insert 
    insertEntry(listStart, &insert, &list1); 

    // restart the list from the beginning because in the last while loop the listStart was assigned to the null pointer. 
    listStart = &list1; 

    // print the new list to show what has been inserted and moved around 
    printf("New list!\n"); 
    while (listStart != (struct entry *) 0) 
    { 
     printf ("%i\n", listStart->value); 
     listStart = listStart->next; 
    } 

    return 0; 
} 

// function to insert a new struct in the list and redirect an old struct in the list 
void insertEntry(struct entry *l, struct entry *i, struct entry *j) 
{ 
    i->next = l; // this is assigning the mem add of the pointer is list2.next to that of insert.next 
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next 

} 
+0

* facepalm * duh! Merci à tous ceux qui ont répondu! – alucinare

Répondre

0

Problème 1

Vous changez où listStart points dans le premier while boucle. À la fin de cette boucle, la valeur de listStart est NULL.

Je vous suggère d'utiliser une variable différente pour parcourir les éléments de la liste et de garder listStart pointant vers le début de la liste.

struct entry *iter = listStart; 
while (iter != NULL) 
{ 
    printf ("%i\n", iter->value); 
    iter = iter->next; 
} 

Utilisez iter dans les deux boucles pour maintenir listStart pointage au début de la liste.

Problème 2

Après l'appel à insertEntry, listStart ne pointe pas au début de la liste. Aucune n'est list1 la première entrée dans la liste. insert est l'objet au début de la liste. Remplacez la ligne

listStart = &list1; 

par

listStart = &insert; 

Suggestion pour le nettoyage

La ligne

l = i; 

dans insertEntry est inutile. Cela ne fait rien dans la fonction.Cela ne change rien non plus dans main. Retirez-le.

0

Voici un exemple de la façon dont vous feriez votre tâche:

void insertEntry(struct entry** pplistStart, struct entry *pNewEntry) 
{ 
    pNewEntry->next = *pplistStart; 
    *pplistStart = pNewEntry; 
} 

Il peut être appelé comme

insertEntry(&listStart, insert); 
0

vous avez deux question: -

One est: -

printf("Original list!\n"); 
while (listStart->next != (struct entry *) 0) 
{ 
    printf ("%i\n", listStart->value); 
    listStart = listStart->next; 
} 

// send struct to change and struct to insert 
insertEntry(listStart, &insert, &list1); 

dans insertEntry() listStart pointe maintenant NULL, alors quand vous appelez insertEntry() vous obtiendrez segfault.

Deuxième problème est: -

Votre logique adresse passée de l dans i-> suivant est erroné. vous devriez assigner l'adresse de i dans l-> suivant.

void insertEntry(struct entry *l, struct entry *i, struct entry *j) 
{ 
    i->next = l; // this is assigning the mem add of the pointer is list2.next to that of insert.next 
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next 

} 

I Modifié votre programme Jetez un oeil.

// header to include standard input and output 
#include <stdio.h> 

struct entry 
{ 
    int value; 
    struct entry *next; 
}; 


// prototype for insertEntry function 
void insertEntry(struct entry *l, struct entry *i, struct entry *j); 

int main(void) 
{ 

    // declaration of array for linked list 
    struct entry list1 = { 1 }, list2 = { 2 }, list3 = { 3 }, list4 = { 4 }, list5 = { 5 }, insert = { 8 }; 
    struct entry *listStart = &list1; 

    // test to see if the value of the insert.value struct is correct 
    printf("insert.value = %i\n", insert.value); 

    // assign pointers in list.next to the next struct in the list to create a linked list 
    list1.next = &list2; 
    list2.next = &list3; 
    list3.next = &list4; 
    list4.next = &list5; 
    list5.next = (struct entry *) 0; 

    // print the linked list to make sure the pointers are going to the correct struct member 
    printf("Original list!\n"); 
    while (listStart->next != (struct entry *) 0) 
    { 
     printf ("%i\n", listStart->value); 
     listStart = listStart->next; 
    } 

    // send struct to change and struct to insert 
    insertEntry(listStart, &insert, &list1); 

    // restart the list from the beginning because in the last while loop the listStart was assigned to the null pointer. 
    listStart = &list1; 

    // print the new list to show what has been inserted and moved around 
    printf("New list!\n"); 
    while (listStart != (struct entry *) 0) 
    { 
     printf ("%i\n", listStart->value); 
     listStart = listStart->next; 
    } 

    return 0; 
} 

// function to insert a new struct in the list and redirect an old struct in the list 
void insertEntry(struct entry *l, struct entry *i, struct entry *j) 
{ 
    l->next = i; // this is assigning the mem add of the pointer is list2.next to that of insert.next 
    l = i; // this is assigning the mem add of i which would be insert.value to the pointer in list2.next 

} 
0

Après while (listStart != (struct entry *) 0) exécution de la boucle, listStart pointe vers l'intérieur et NULLinsertEntry, vous assignez pointeur de next insert à l istStart qui est NULL.

Vous pouvez essayer de suivre deux solutions,

  1. intérieur insertEntry changement

i->next = l

Pour

i->next = j

ou

  1. Avant d'appeler insertEntry point pointer listStart à list1

listStart = &list1; insertEntry(listStart, &insert, &list1);