0

I have the following code and I am trying to make it more dynamic and reusable. Well, I have a struct named Student and struct list, which contains all the added students. I have a function "int addStudent(Student b, list StudentList){", and I am trying to pass the structs Student and StudentList as parameters. But the problem is that I am doing something wrong and my list does not contains all the Students added. It contains only the last one. Can you help me?

NOTE: I have to create a body for the "int addStudent(Student b, list StudentList)". It is not permitted to change the declaration of this function ... this is very difficult for me and I need suggestions to work on ...

thank you in advance!

#include <stdio.h>
#include <stdlib.h>

#define MAXSTRING 100
#define MAXLessonS 100

typedef  enum genders{
    female, 
    male
} genders;

typedef struct Student
{
    char name[MAXSTRING];
    char Surname[MAXSTRING];
    enum genders gender;
    int id;
    char Lessons[MAXLessonS][MAXSTRING];
} Student;


typedef struct list 
{
    struct list * next;
    struct Student * Student;
} list;

void printlist(list * StudentList) 
{
    list * current = StudentList;
    while (current != NULL) {
        printf("Student ID      = %d\n", current->Student->id);
        printf("Student name    = %s\n", current->Student->name);
        printf("Student Surname = %s\n", current->Student->Surname);
        printf("Student gender  = %d\n", current->Student->gender);
        printf("Student Lesson  = %s\n", current->Student->Lessons);     
        current = current->next;
    }
}


int main()
{
    Student b={"name 1","Surname 1",male,22,{"Lesson 1"}};  
    Student c={"name 2","Surname 2",female,32,{"Lesson 2"}};  

    list* StudentList = NULL;
    StudentList = malloc(sizeof(list));
    StudentList->next = NULL;
    //StudentList->next->next = NULL;

    int x=addStudent(b,StudentList);
    StudentList->next=NULL;
    int xx=addStudent(c,StudentList);

    printlist(StudentList);
    return 0;
}

int addStudent(Student b, list StudentList){
    //StudentList=malloc(sizeof(list));
    StudentList.Student = &b;
    //StudentList.next->next=NULL; 
    //free(StudentList);
    return 1;
}
4
  • 1
    StudentList.Student = &b;: overwrites the pointer with the last one (c)... Commented May 31, 2017 at 13:59
  • 1
    Your list only ever contains one node - also surprised that you don't get warnings/errors for the definition of addStudent as it differs from how you're using it. Commented May 31, 2017 at 14:01
  • @Jean-François Fabre I can not understand how I could resolve it ... Commented May 31, 2017 at 14:03
  • @ Chris Turner I am using Dev-C++ compiler ... and I have no errors either warnings ... Commented May 31, 2017 at 14:04

3 Answers 3

1

The addStudent method always overwrites previous nodes. So your list only ever contains 1 node. Also, to "store" a linked list, you would want to keep a pointer to the head (first element) of the list.

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

1 Comment

I usually dont like to change the format of the code as you may have your own reasons for doing things how you did. However, it might make more sense to store the pointer to the next node inside the Student struct. Then you could use the list struct to store things relative to the entire list. Like a head/tail pointer and all of your insert/pop/etc methods. That way, students would be individual nodes in your list. You would have only 1 list object to store the entire list.
1

You have two problems:

1) You add the address of a local variable to the list

2) You never expand the list, i.e. it always contain a single element

To solve problem 1) change the addStudent function like:

int addStudent(Student* b, list* StudentList){
                     ^^^     ^^^
                   Use a pointer

    StudentList->Student = b;
                         ^^^
                       Save the pointer

    return 1;
}

and call it like:

int x=addStudent(&b, StudentList);
                 ^^

To solve problem 2):

You need to malloc a new list item and insert it into the current list.

However, your current code is a bit strange as you also allocate a list in main but puts nothing into it. Instead it would seem better to only allocate list items in the addStudent function.

A simple way to do that is:

// This function inserts new item in the front of the list
list*  addStudent(Student* b, list* StudentList){
    list* t;
    t = malloc(sizeof(list));
    if (!t) exit(1);
    t->next = StudentList;
    t->Student = b;
    return t;
}

int main()
{
    Student b={"name 1","Surname 1",male,22,{"Lesson 1"}};  
    Student c={"name 2","Surname 2",female,32,{"Lesson 2"}};  

    list* StudentList = NULL;

    StudentList = addStudent(&b, StudentList);
    StudentList = addStudent(&c, StudentList);

    printlist(StudentList);
    return 0;
}

Comments

1

Use a while to go to the last element with a pointer, then create your new element in your list void addStudent(Student *b, list *StudentList) list *elem; elem = StudentList; while (elem->next) elem = elem->next; //now you can create your elem elem->next = malloc(sizeof(list)); elem->next->student = b; elem->next->next = NULL;

3 Comments

elem->next->student = b; assigns a Student to a Student* That is illegal
@elfuretto thank you for your help! but I have to create a body for the "int addStudent(Student b, list StudentList)". It is not permitted to change the declaration of this function ....
You can't give a pointer parameters : int x=addStudent(b,StudentList); And call your fonction with a simple structure : int addStudent(Student b, list StudentList) What is the part of code you can change exactly ? only the addstudent body ? cause there is some problems in your code. Last question, is this a school project ?

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.