Inverted linked list in C
-
Hello friends ! , in my following code in C I want to invert my linked list in the display using a ๐ฟ๐ฒ๐ฐ๐๐ฟ๐๐ถ๐๐ฒ ๐ณu๐ป๐ฐ๐๐ถ๐ผ๐ป ; but the code does not work !! Is there an error; thank you for mentioning it :+1: #include #include struct cellule{ int a; struct cellule *suivant; }; //Recursive function to display the list in invers order void affichage (struct cellule *liste){ while (liste!=NULL){ affichage(liste->suivant); printf(" %d โ,liste->a); } } int main() { struct cellule *liste,*p,*q,*r; int n,i; printf(โDonner le nombre dโelements de la liste:\nโ); scanf(โ%d",&n); printf(โEntrer les elements de la liste:\nโ); for(i=0;i
-
Hello friends ! , in my following code in C I want to invert my linked list in the display using a ๐ฟ๐ฒ๐ฐ๐๐ฟ๐๐ถ๐๐ฒ ๐ณu๐ป๐ฐ๐๐ถ๐ผ๐ป ; but the code does not work !! Is there an error; thank you for mentioning it :+1: #include #include struct cellule{ int a; struct cellule *suivant; }; //Recursive function to display the list in invers order void affichage (struct cellule *liste){ while (liste!=NULL){ affichage(liste->suivant); printf(" %d โ,liste->a); } } int main() { struct cellule *liste,*p,*q,*r; int n,i; printf(โDonner le nombre dโelements de la liste:\nโ); scanf(โ%d",&n); printf(โEntrer les elements de la liste:\nโ); for(i=0;i
There are two issues: 1. Where you create the
cellule
structures:for(i=0;isuivant = NULL; // Mark the last entry as having no follower
2. Your recursive method
void affichage (struct cellule *liste){
if (liste ==NULL) // this just needs to check for the end of the list
{
return; // no more entries
}
affichage(liste->suivant); // process the next entry in the list
printf(" %d ",liste->a); // on return print the current entry's value
} -
Hello friends ! , in my following code in C I want to invert my linked list in the display using a ๐ฟ๐ฒ๐ฐ๐๐ฟ๐๐ถ๐๐ฒ ๐ณu๐ป๐ฐ๐๐ถ๐ผ๐ป ; but the code does not work !! Is there an error; thank you for mentioning it :+1: #include #include struct cellule{ int a; struct cellule *suivant; }; //Recursive function to display the list in invers order void affichage (struct cellule *liste){ while (liste!=NULL){ affichage(liste->suivant); printf(" %d โ,liste->a); } } int main() { struct cellule *liste,*p,*q,*r; int n,i; printf(โDonner le nombre dโelements de la liste:\nโ); scanf(โ%d",&n); printf(โEntrer les elements de la liste:\nโ); for(i=0;i
By "inverted" do you mean you want to print your list in reverse order? E.g. input: 1 2 3 4 5 output: 5 4 3 2 1
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles