Reputation: 53
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;
}
Upvotes: 0
Views: 1573
Reputation: 13
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;
Upvotes: 1
Reputation: 44274
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;
}
Upvotes: 1
Reputation: 39
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.
Upvotes: 1