c - C中的动态列表头疼,反向打印列表?为什么?

标签 c list function pointers dynamic

我正在尝试学习如何在 C 中使用动态列表,但似乎无法理解它 - 所以我们将不胜感激。我有一个结构体,其中包含一些我正在命令行中从 txt 文件中读取的信息,并且需要将这些信息添加到动态列表中。

这就是我到目前为止所拥有的。我对指针感到茫然,不知道参数是否正确以及从哪里开始。

周末的大部分时间都在寻找完成这项工作的方法。我理解这个概念并不难,只是它的具体细节我只是不明白......

#include <stdio.h>
#include <stdlib.h>
#define SIZE_MAX  20
#define BUFFER_MAX 256
FILE *file;


/*struct*/
struct student {
char name[SIZE_MAX];
int grade;
struct student *next;
};

typedef struct student Student;



int addToList(Student **head, char *, int);
void printList(Student **head);
void releaseMem(Student **head);

/*functions*/
void addToList(Student **head, char *name, int grade ){

//???
}

/*Main*/

int main (int argc, char *argv[]){

Student *head=NULL,*tail=NULL;
int grade = 100 ;
char buffer [BUFFER_MAX];
char name[SIZE_MAX];


/*opening file*/
file = fopen(argv[1], "r");
if (file == NULL){
  printf("\n\tWARNING: No data found.\n");
  exit(1);
}
else{
    printf("Reading file %s \n",argv[1]);
}
/*creating first node*/

Student* new_student(Student*)malloc(sizeof(Student));


while(fgets(buffer, BUFFER_MAX,file)!= NULL){
  sscanf(buffer,"%s%d",name,&grade);
    //printf("%s %d\n",string, grade);
    addToList(&head,name,grade);
}

return 0;
}

编辑:到目前为止,我已成功将文件中的数据添加到动态列表中(感谢您的帮助)。这是我所拥有的:

#include <stdio.h>
#include <stdlib.h>
#define SIZE_MAX  20
#define BUFFER_MAX 256
FILE *file;


/*Struct*/
struct student {
char name[SIZE_MAX];
int grade;
struct student *next;
};

typedef struct student Student;



int addToList(Student **head, char *, int);
void printList(Student *head);
void releaseMem(Student *head);

/*functions*/
int addToList(Student **head, char *name, int grade ){

Student *new_student = malloc( sizeof( Student ) );
{
Student *new_student = malloc( sizeof( Student ) );
int success = new_student != NULL;


if ( success )
{
        strcpy( new_student->name, name );
        new_student->grade = grade;
        new_student->next = *head;
        *head = new_student;




}

return success;
}
}
void printList(Student *head){

Student * current = head;
int i = 1;

while (current != NULL) {
    printf("%d. Student: %s grade %d\n",i,current->name  ,current->grade);
            i++;
    current = current->next;
}

}


void releaseMem(Student *head){
Student * current = head;

    while (current != NULL) {
            free(current);
            current = current->next;
    }
printf("mem cleared.\n");


}

/*Main*/

int main (int argc, char *argv[]){

Student *head=NULL,*first=NULL, *temp = NULL;
int grade = 100 ;
char buffer [BUFFER_MAX];
char name[SIZE_MAX];


/*opening file*/
file = fopen(argv[1], "r");
if (file == NULL){
printf("\n\tWARNING: No data found.\n");
exit(1);
}
else{
printf("reading file %s. \n",argv[1]);
}
printf("data added to list.\n");
while(fgets(buffer, BUFFER_MAX,file)!= NULL){
sscanf(buffer,"%s%d",name,&grade);
addToList(&head,name,grade);

}

printList(head);
releaseMem(head);
return 0;
}

(几乎)像我希望的那样工作。由于某种原因, printList 函数以相反的顺序打印文件的内容,在摆弄它一段时间后,我不知道如何从头到尾而不是从尾到头打印它。我想这与指针有关,但更重要的是我不知道该怎么做......我在这里错过了什么?我如何在保持(格式)不变的情况下反转打印顺序?

最佳答案

您应该在学生列表中分配新学生并将其放置到最后一个成员的下一个,如下所示:

//since we are adding new members after the last member in linked list 
//we are not going to change value of head so sending **head is not useful
void addToList(Student *head,char *name,int grade){
   Student *node;
   for(node = head; node->next != NULL; node = node->next );
   // now node points the last member of your linked list
   // now we are adding new student to the linked list with allocating memory  
  node->next = (Student *)malloc(sizeof(student));
  node->next->grade = grade;
  strcpy(node->next->name,name);
}

关于c - C中的动态列表头疼,反向打印列表?为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42332459/

相关文章:

javascript - 如何从 Javascript 的父函数返回在子 jQuery 函数中创建的数组?

c - Linux 中 LoadString 函数的等效项是什么?

c - 在C中显示特殊字符

c - sscanf 行为不同

c - (C) 使用月份中的天数和月份开始日显示日历(使用 1 表示星期一,2 表示星期二等)

c - 未初始化的局部变量错误

python - 从列表中提取 raw_input 选项

pandas - 如何将包含元组列表的字典内的字典转换为 pandas 数据框

c# - 在 .net 中添加两个集合的最佳方式

python - 基于状态与传递参数