将链表传递到结构中

时间:2017-05-03 01:28:23

标签: c function linked-list

好的,所以这可能是我最负载的问题,因为我讨厌发布一个问题的大量代码,但我根本无法找到一种方法来简化这个问题,因为我不喜欢我真的很了解链接列表。结构对我来说很有意义,但链接列表真的让我失望了。无论如何,我需要制作一个模块化程序,用于按照用户的指示执行各种任务。我开始将程序放在main中以保持简单:

#include <stdio.h>
#include <stdlib.h>      /* has the malloc prototype      */
#define TSIZE 50         /* size of array to hold title   */
struct film {
    char title[TSIZE], author[TSIZE],year[5];
    struct film * next;  /* points to next struct in list */
};
typedef struct film * Node;
int main(void)
{
    Node head = NULL;
    Node prev, current;
    char input[TSIZE];
    const char fname[]="HW15Data.txt";
/* Gather  and store information          */
    FILE * fp;
    char line[256];
    int loops=0;
    fp=fopen(fname,"r");
    while(current != NULL){
        current = (Node) malloc(sizeof(struct film));
        if (head == NULL)       /* first structure       */
            head = current;
        else                    /* subsequent structures */
            prev->next = current;
        current->next = NULL;
        if (fgets(current->title,sizeof(line),fp)==NULL) break;
        if (fgets(current->author,sizeof(line),fp)==NULL) break;
        if (fgets(current->year,sizeof(line),fp)==NULL) break;
        prev = current;
        loops++;
    }
/* Show list of books from file                    */
    if (head == NULL)
        printf("No data entered. ");
    else
        printf ("Here is the books list:\n");
    current = head;
    while (current != NULL)
    {
        printf("Book: %sAuthor: %sYear: %s\n",
        current->title, current->author, current->year);
        current = current->next;
    }
    printf("Number of records wrote: %d\n",loops);



/* Program done, so free allocated memory */
    current = head;
    while (current != NULL)
    {
        free(current);
        current = current->next;
    }
    printf("Bye!\n");

    return 0;
}

所以,不知何故,这种方式应该如此。现在,我尝试使其模块化,从第一个函数IntializeList我遇到了问题。因此,为了使我的第一个程序更好地匹配,我编写了我想要做的其余部分,忽略了函数传递问题。显然,编译器现在给我带来了一些错误,我无法从这里弄清楚该怎么做:

#include <stdio.h>
#include <stdlib.h>      /* has the malloc prototype      */
#define TSIZE 50         /* size of array to hold title   */
struct book {
    char title[TSIZE], author[TSIZE],year[5];
    struct book * next;  /* points to next struct in list */
};
//typedef struct book ITEM;
typedef struct book * Node;     // struct book *
void InitializeList(Node *List);
void InitializeList(Node *List){
    *List = NULL;
    puts("Initialization successful.");
}   
void Append(Node *List, char filename[TSIZE]);
void Append(Node *List, char filename[TSIZE]){
    FILE * fp;
    Node head=NULL;
    Node prev, current;
    char line[256];
    int loops=0;
    fp=fopen(filename,"r");
    while(current != NULL){
        current = (Node) malloc(sizeof(struct book));
        if (head == NULL)       /* first structure       */
            head = current;
        else                    /* subsequent structures */
            prev->next = current;
        current->next = NULL;
        if (fgets(current->title,sizeof(line),fp)==NULL) break;
        if (fgets(current->author,sizeof(line),fp)==NULL) break;
        if (fgets(current->year,sizeof(line),fp)==NULL) break;
        prev = current;
        loops++;
    printf("Number of records wrote: %d\n",loops);
    }
}
int Print(void);
int Print(void){
    int num;
    int i=0;
    if (head == NULL){
        printf("No data entered. ");
        return -1;
    }
    printf("Enter record # to print: ");
    scanf("%d",num);
    current = head;
    while (current != NULL)
    {
        for (i=0;i<num;i++){
            current = current->next;
        }
        printf("Book: %sAuthor: %sYear: %s\n",
        current->title, current->author, current->year);
    }
    return 0;
}
int WriteBinary(const char * pfname);
int WriteBinary(const char * pfname){
    FILE * fp;
    fp = fopen(pfname,"wb");
    if (head == NULL){
        printf("No data to write. ");
        return -1;
    }
    current = head;
    while (current != NULL){
        fwrite(current->title,sizeof(TSIZE),1,fp);
        fwrite(current->author,sizeof(TSIZE),1,fp);
        fwrite(current->year,sizeof(TSIZE),1,fp);
        current = current->next;
    }
    return 0;
}
int main(void){
    Node head;
    Node prev, current;
    int books;
    InitializeList(&head);
    char input[TSIZE];
    char fname[]="HW15Data.txt";
    char op;
    do{
        puts("Select operation from the following list:");
        puts("i. Initialize    a. Append    p. Print");
        puts("w. Write binary file    q. quit");
        op = getchar();
        while (getchar()!='\n');
        if (op=='i'){
            InitializeList(&books);
        }
        else if (op=='a'){
            /* Gather  and store information          */
            books=Append(&head,fname);
        }
        else if (op=='p'){
            /*Print book record of user's choice*/
            Print();
        }
        else if (op=='w'){
            /*Write list in binary to a file*/
            WriteBinary("HW15bin");
        }
        else if (op=='q'){
            /* User quit, so free allocated memory */
            current = head;
            while (current != NULL)
            {
                free(current);
                current = current->next;
            }
            printf("Bye!\n");
            return 0;
        }
        else{
            /* Program done, so free allocated memory */
            current = head;
            while (current != NULL)
            {
            free(current);
            current = current->next;
            }
            printf("Invalid characted entered. Bye!\n");
            return 0;
        }
    } while (op!='q');
    return 0;
}

所有我真的需要做的事情我只是清理事情是如何传递或不传递给函数的,我应该全部设置好!我确定这是一个非常严重的问题,很可能是我的最后一个问题,但此时我真的不知道该怎么做。如果有人回答这个问题,那你就是一个疯狂的传奇。

0 个答案:

没有答案