1
            struct node{

                char name[50];
                double grade;
                struct node* next;


            };





            void append(struct node* root){

                int n;

                printf("Enter the number of students: ");
                scanf("%d",&n);

                while(n !=0){

                    struct node* temp;

                    temp=(struct node*)malloc(sizeof(struct node));

                    printf("\nEnter the name of the student: ");
                    scanf("%s",&temp->name);
                    printf("\nEnter the grade for the student named %s: ",temp->name);
                    scanf("%f",&temp->grade);
                    temp->next=NULL;

                    if(root==NULL){
                       root=temp;
                    }else{
                        struct node* iterate=root;

                        while(iterate->next != NULL){

                            iterate=iterate->next;
                        }

                        iterate->next=temp;

                    }

                n--;
                }


            }

int listLength(struct node* root){

    struct node* temp = root;
    int counter=0;

    while(temp !=NULL){

        counter++;
        temp=temp->next;
    }

    return counter;

}

      int main()
        {
            struct node* root = NULL;

            append(&root);
            //printList(&root);
            printf("Node length: %d",listLength(&root));
            return 0;
        }

This is what I have so far as I'm starting out with linked lists. I tried to make it so I can append to multiple linked lists with the function. So I'd just create a different root pointer in main and call the append function with it as a parameter to add nodes.

This seems to work, however, it adds an extra empty node at the beginning of the list. This node doesn't contain any data. So for example, if I add 4 students to the list the nodeLength function would return 5.

kalvin
  • 25
  • 4
  • Please compile with warnings enabled. For example,you pass `&root` from `main` to a function that expects a `struct node *`, not a `struct node **`. – M Oehm May 11 '17 at 14:21
  • Yes, that's a warning I'm getting. "note: expected 'struct node *' but argument is of type 'struct node **'. If I pass just 'root' then the nodes don't get added. The length function returns 0. – kalvin May 11 '17 at 14:23
  • That's a warning that really should be an error. When you add nodes, you must be able to update the root, so `append(&root)` is correct, but your function should be `void append(struct node **root)`. And you must use `*root` instead of just `root` in that function. – M Oehm May 11 '17 at 14:27
  • It works now. Thank you. – kalvin May 11 '17 at 14:29
  • Your indentation is all over the place. Please fix that. – Ajay Brahmakshatriya May 11 '17 at 14:33
  • It's only like that on stackoverflow. Haven't really used this website. Sorry. – kalvin May 11 '17 at 14:37

2 Answers2

2

Change this:

void append(struct node* root)

to this:

void append(struct node** root)

so that you make the changes last even when append() terminates.

Of course then you will have to use *root instead of root inside the body of that function.


PS: Do I cast the result of malloc? No.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • .: Really they just need to know the answer to make the answer correct. Be it complicated doesn't matter..anyway I don't know what to say..demoralizing. – user2736738 May 11 '17 at 14:37
  • Yeah @coderredoc, BTW nice answer you have posted there. – gsamaras May 11 '17 at 14:39
  • No your answer is good too..but you tell me 2 pointer is hardly needed here..isn't it? Single level indirection can achieve a lot of things. Your answer is slected because it incurs minimum change so yes it's helpful but just saying. – user2736738 May 11 '17 at 14:40
  • Both approaches are valid @coderredoc, that's why I upvoted you! =) – gsamaras May 11 '17 at 14:42
  • 1
    Yes thats true..thanks for that. I learned something form you. All the best. – user2736738 May 11 '17 at 14:43
  • hey +1 to you..op changed his mind. I don't know what to say. – user2736738 May 11 '17 at 19:07
1
        struct node{
            char name[50];
            double grade;
            struct node* next;
        };
        struct node * append(struct node* root){
            int n;
            printf("Enter the number of students: ");
            scanf("%d",&n);
            while(n !=0){
               struct node* temp;
                temp=(struct node*)malloc(sizeof(struct node));
                printf("\nEnter the name of the student: ");
                scanf("%s",&temp->name);
                printf("\nEnter the grade for the student named %s: ",temp->name);
                scanf("%f",&temp->grade);
                temp->next=NULL;
                if(root==NULL){
                   root=temp;
                }else{
                    struct node* iterate=root;
                    while(iterate->next != NULL){
                        iterate=iterate->next;
                   }
                    iterate->next=temp;
                    root=iterate;
                }
            n--;
            }
      return root;            }

 int nodeLength(struct node* root){
       struct node* temp = root;
       int counter=0;
       while(temp !=NULL){
           counter++;
           temp=temp->next;
       }
      return counter;
}
int main()
{
      struct node* root = NULL;
      root= append(root);
      //printList(&root);
      printf("Node length: %d",nodeLength(root));
      return 0;
}

Check this code to understand a bit. Will explain more.

And don't cast the return type of malloc.

Here what you are doing will work fine for if you correctly changed the parameter to struct node** but yes it can be done more easily like this(example shown above). It's natural and more intuitive rather than using double pointer.

user2736738
  • 30,591
  • 5
  • 42
  • 56