簡體   English   中英

C中的鏈接列表和結構-將鏈接列表和結構作為參數傳遞給函數(C)

[英]Linked Lists and Structures in C - Passing Linked List and Structures as Parameters in function (C)

我有以下代碼,並且我試圖使其更加動態和可重復使用。 好吧,我有一個名為Student和struct list的結構,其中包含所有添加的學生。 我有一個函數“ int addStudent(Student b,list StudentList){”,並且我試圖將結構Student和StudentList作為參數傳遞。 但是問題是我做錯了事,我的名單沒有包含所有添加的學生。 它僅包含最后一個。 你能幫助我嗎?

注意:我必須為"int addStudent(Student b, list StudentList)"創建一個主體。 不允許更改此函數的聲明 ...這對我來說非常困難,我需要一些建議來進行工作...

先感謝您!

#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;
}

addStudent方法始終覆蓋先前的節點。 因此,您的列表僅包含1個節點。 另外,要“存儲”鏈接列表,您需要保持指向列表頭(第一個元素)的指針。

花一會兒用指針轉到最后一個元素,然后在列表中創建新元素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; 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;

您有兩個問題:

1)您將局部變量的地址添加到列表中

2)您永遠不會展開列表,即它始終只包含一個元素

解決問題1)更改addStudent函數,如下所示:

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

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

    return 1;
}

並這樣稱呼:

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

解決問題2):

您需要malloc一個新的列表項並將其插入到當前列表中。

但是,您當前的代碼有點奇怪,因為您還在main分配了一個list ,但沒有放入任何內容。 相反,似乎最好只在addStudent函數中分配list項。

一個簡單的方法是:

// 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM