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

时间:2017-02-19 20:24:53

标签: 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函数以相反的顺序打印文件的内容,并且在摆弄它一段时间后,我不知道如何从头到尾而不是从头到尾打印它。我想它与指针有关但不仅仅是我不知道该做什么......我在这里想念的是什么?如何保持打印顺序并保持当前的(格式化)?

2 个答案:

答案 0 :(得分:-1)

该程序至少不会编译,因为这句话

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

无效。即使写它像

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

它没有意义,因为应使用函数addToList将新项目添加到列表中。

变量tail的声明也没有意义,因为不可能将它与头部一起传递给函数(如声明的那样)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;
}

答案 1 :(得分:-1)

你应该在学生名单中分配新学生,并将其放在最后一个成员的下一个,如下所示:

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