#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct module {
char name[10];
int note;
struct module * next;
};
typedef struct module module;
struct student {
char name[10];
char adress[20];
struct student * next;
module * head;
} ;
typedef struct student student;
student *etudiant=NULL;
module* add_module(char name[],int note){
module *p=(module*)malloc(sizeof(module));
p->note=note;
p->next=NULL;
strcpy(p->name,name);
return p;
}
void add_student(char name[], char adress[])
{
student *p=(student*)malloc(sizeof(student));
strcpy(p->name,name);
strcpy(p->adress,adress);
p->head= add_module("algo",15);
p->next=NULL;
if (etudiant==NULL){
etudiant=p;
}
else{
student *q = etudiant;
while(q->next!=NULL){
q=q->next;
}
q->next=p;
}
}
void print_module(module *m){
if (m==NULL)
{
printf("NULL");
}
else
{
while(m->next!=NULL){
printf("%s ",m->name);
printf("%d\n",m->note);
m=m->next;
}
}
}
void print(){
student *p;
module *m;
p = etudiant;
if (etudiant==NULL){
printf("NULL");
}
else
{
while (p->next!=NULL);
{
printf("%s ",etudiant->name);
printf("%s ",etudiant->adress);
m = p->head;
while(m != NULL){
printf("%s ",m->name);
printf("%d ",m->note);
m= m->next;
}
p = p->next;
}
}
}
int main () {
add_student("jack","nowhere");
print();
return 0;
}
我想要创建的是列表示例中的列表
Student list :
Student || subject || ==> student 2 || subject
| |
maths POO
| |
physiques English
这是我的结构的近似描述,我来到一个学生添加一个主题,但我不知道如何添加更多。 提前谢谢。
我将学生列表定义为全局列表,因为我只需要一个包含所有学生的列表
答案 0 :(得分:0)
这是你的学生名单:
Student1 ==> student2 ==> NULL
| |
maths POO
| |
physiques English
现在,如果您想向student2添加“Computer Science”模块,那么您必须执行以下步骤:
你的功能将是这样的:
typedef struct student student;
void addModule(char studentName[], char subject[], int note) {
// searching student in the list..
if(etudiant != NULL) {
struct student *s = etudiant; //start of the list
while(s && strcmp(s->name, studentName) != 0)
s = s->next;
if(s != NULL) {
// creating module...
module* new = (module*)malloc(sizeof(module));
strcpy(new->name, subject);
new->note = note;
new->next = NULL;
//adding module to the front of the module list of student s ...
module* tmp = s->head;
s->head = new;
new->next = tmp;
}
}
}
void add_student(char name[], char adress[]) {
student *p=(student*)malloc(sizeof(student));
strcpy(p->name,name);
strcpy(p->adress,adress);
p->next=NULL;
if (etudiant==NULL){
etudiant=p;
}
else {
struct student *q = etudiant;
while(q->next!=NULL){
q=q->next;
}
q->next=p;
}
addModule(p->name, "algo", 15);
}
int main() {
add_student("A", "XYZ");
addModule("A", "CS", 1);
addModule("A", "MECH", 1);
add_student("B", "PQR");
addModule("B", "DAA", 1);
addModule("b", "SE", 1);
//printing the list...
student *q = etudiant;
while(q != NULL) {
module *p = q->head;
printf("%s -> ", q->name);
while(p != NULL) {
printf("%s ", p->name);
p = p->next;
}
printf("\n");
q = q->next;
}
}