动态列表在C中,打印列表在反向?为什么?
我正试图学习如何使用C语言中的动态列表,但我似乎无法理解这一点,因此希望得到帮助。我有一个结构,其中包含一些我正在从命令行中的txt文件读取的信息,需要将这些信息添加到动态列表中 这就是我目前所拥有的。我对指针不知所措,不知道这些参数是否正确,以及从哪里开始 我已经花了周末的大部分时间寻找一种方法来完成这件事。这并不难,因为我得到的概念,只是它的螺母和螺栓只是逃避我动态列表在C中,打印列表在反向?为什么?,c,list,function,pointers,dynamic,C,List,Function,Pointers,Dynamic,我正试图学习如何使用C语言中的动态列表,但我似乎无法理解这一点,因此希望得到帮助。我有一个结构,其中包含一些我正在从命令行中的txt文件读取的信息,需要将这些信息添加到动态列表中 这就是我目前所拥有的。我对指针不知所措,不知道这些参数是否正确,以及从哪里开始 我已经花了周末的大部分时间寻找一种方法来完成这件事。这并不难,因为我得到的概念,只是它的螺母和螺栓只是逃避我 #include <stdio.h> #include <stdlib.h> #define SIZE_M
#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函数以相反的顺序打印文件的内容,在对其进行了一段时间的处理后,我不知道如何从开始打印到结束,而不是从结束打印到开始。我想这和指针有关,但更重要的是我不知道该怎么做。。。我错过了什么?我该如何扭转打印顺序并保持当前格式?程序将不会编译,至少因为此语句
Student* new_student(Student*)malloc(sizeof(Student));
这是无效的。即使是这样写
Student* new_student = (Student*)malloc(sizeof(Student));
这没有意义,因为应该使用函数addToList将新项目添加到列表中
变量tail的声明也没有意义,因为它不可能传递给函数,因为它与head一起声明为addToList
对于函数本身,最好按照以下方式声明它
int addToList( Student **head, const char *name, int grade );
该函数的定义如下
int addToList( Student **head, const char *name, int grade )
{
Student *new_student = malloc( sizeof( Student ) );
int success = new_student != NULL;
if ( success )
{
strcpy( new_student->name, name );
// or
// strncpy( new_student->name, name, SIZE_MAX );
// new_student->name[SIZE_MAX - 1] = '\0';
new_student->grade = grade;
new_student->next = *head;
*head = new_student;
}
return success;
}
您应该在学生列表中分配新学生,并将其放置到最后一个成员的下一个,如下所示:
//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);
}
学生*新生*mallocsizeofStudent;是无效的c。在某处缺少等号…不需要为我编写代码,只需向我指出编写方向,老实说,我在这一点上迷失了指针,我一直在努力向fe学习。但最终会出现分段错误,并决定从头开始…就在这里: