2017-02-19 96 views
-2

我正在嘗試學習如何在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函數以相反的順序打印文件的內容,並且在弄了一段時間後,我不知道如何從頭到尾打印它,而不是從頭到尾打印。我想它與指針有關,但不止於此我無所適從......我在這裏錯過了什麼?如何保持(格式化)目前的狀態來顛倒打印順序?

+0

'Student * new_student(Student *)malloc(size (學生));'是無效的c。在某處丟失「等於」符號... –

+1

不需要爲我寫的代碼,只是指向我的寫作方向,老實說,我在這一點上丟失了指針 –

+1

我一直在試圖從fe學習。這個http://www.learn-c.org/en/Linked_lists但結束了一個分段錯誤,並決定從頭開始... –

回答

-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) 
{ 
    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

定義下列方式

int addToList(Student **head, const char *name, int grade); 

你應該在學生名單和地點分配新同學它到最後一個成員的下一個像這樣:

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