我正在嘗試學習如何在C中使用動態列表,但似乎無法將我的頭圍繞在它的周圍 - 所以幫助將被appreaciated。我有一個結構,有一些信息,我從命令行中的txt文件中讀取,並需要將信息添加到動態列表。C頭痛動態列表,反向打印列表?爲什麼?
這是迄今爲止我所擁有的。我不知道這些論點是否正確以及從哪裏出發。
已經花了更多的週末尋找一種方法來完成這件事。不難,因爲我得到的概念,只是螺母和螺栓它只是躲避我......
#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(size (學生));'是無效的c。在某處丟失「等於」符號... –
不需要爲我寫的代碼,只是指向我的寫作方向,老實說,我在這一點上丟失了指針 –
我一直在試圖從fe學習。這個http://www.learn-c.org/en/Linked_lists但結束了一個分段錯誤,並決定從頭開始... –