添加到链表C中的数组

时间:2017-05-25 22:49:20

标签: c arrays linked-list doubly-linked-list

我目前有一个文本文件如下

# This is the small address book file.
# Each line of this file contains a phone book entry.
# The order of the data is: ID,Name,Telephones
# 
# Note: Lines starting with # are comments and should be ignored.
# 
100,Alice,0411112221
101,Bob,0411112222,0422221111
102,Ali,0411112223,0422221112,0422221113
103,Reza,0411112224
104,Ryan,0411112225,0422221114,0422221115,0422221116
105,Aaron,0411112226
106,Jessica,0411112227,0422221117
107,Xudong,0411112228
108,James,0411112229
109,Shaahin,0411112230
110,Matthew,0411112231
111,Paul
112,Elton,0411112233
113,Joe,0411112234,0422221118,0422221119,0422221120
114,Daniel,0411112235
115,Anthony,0411112236
116,Kelvin,0411112237
117,Harry,0411112238
118,Zhaowei,0411112239
119,Lei,0411112240,0422221121,0422221122,0422221123
120,Forest

我应该用逗号分隔每个元素:

ID:100姓名:Alice电话:0411112221

ID:101姓名:Bob电话:0411112222,0422221111

Structure of the linked list and array within

这是我应该读取文件的加载命令。

AddressBookList *commandLoad(char *fileName)
{

    AddressBookList *addressBookList;
    AddressBookNode *addressBookNode;
    AddressBookArray *addressBookArray;
    int id, i, j, count;
    char *ptr;
    char line[BUFSIZ];
    char name[NAME_LENGTH];
    char telestring[BUFSIZ];
    char telephone[TELEPHONE_LENGTH];
    FILE *fp;
    fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("> Failed to load\n");
        return NULL;
    }
    else
    {
        addressBookList = createAddressBookList();
        addressBookArray = createAddressBookArray();
        printf("> Opening the file ");
        printf("%s.\n", fileName);
        printf("> Loading the file ...\n");
        while (fgets(line, sizeof(line), fp) != NULL)
        {
            char *p = line;
            if (*p == '#')
                continue;
            for (i = 0, count = 0; i < line[i]; i++)
            {
                count += (line[i] == ',');
            }
            if (count == 1)
            {
                id = strtol(strtok(line, ",\n"), &ptr, 10);
                strcpy(name, strtok(NULL, ",\n"));
                addressBookNode = createAddressBookNode(id, name);
                insertNode(addressBookList, addressBookNode);
            }
            else if (count >= 2)
            {
                int j = 0;
                id = strtol(strtok(line, ",\n"), &ptr, 10);
                strcpy(name, strtok(NULL, ",\n"));
                strcpy(telestring, strtok(NULL, "\n"));
                telephone[j] = strtok(telestring, ",");
                while(telephone[j] != NULL)
                {
                    telephone[j++] = strtok(NULL, ","); 
                }
                addressBookNode = createAddressBookNode(id, name);
                insertNode(addressBookList, addressBookNode);
                addTelephone(addressBookArray, telephone);
                printf("%s\n", telephone);
            }
            else
            {
                printf("Unable to load file");
            }
        }
        printf("> %d phone book entries have been loaded from the file\n", addressBookList->size);
        return addressBookList;
    }
    return addressBookList;
}

这是我的方法,应该将电话添加到阵列中。

Boolean addTelephone(AddressBookArray * array, char * telephone)
{  
    int i;
    char * newTelephone = malloc(TELEPHONE_LENGTH);
    strcpy(newTelephone, telephone);
    for(i = 0; i < array->size; i++)
    {
        if(strcmp(newTelephone, array->telephones[i]) == 0)
        {
            return FALSE;
        }
    }
    array->telephones = realloc(array->telephones, sizeof(*array->telephones) *(array->size + 1));
    array->telephones[array->size] = newTelephone;
    printf("%s\n", array->telephones[array->size]);
    array->size++;
    return TRUE;
}

这些是与此问题相关的结构

typedef struct addressBookArray
{
    int size;
    char ** telephones;
} AddressBookArray;

typedef struct addressBookNode
{
    int id;
    char name[NAME_LENGTH];
    AddressBookArray * array;
    struct addressBookNode * previousNode; /* The previous node in the linked-list */
    struct addressBookNode * nextNode; /* The next node in the linked-list */
} AddressBookNode;

typedef struct addressBookList
{
    int size;
    AddressBookNode * head;
    AddressBookNode * tail;
    AddressBookNode * current;
} AddressBookList;

我不知道如何将这些电话添加到阵列中。 char **电话也让我感到困惑,我不确定我应该如何使用它。

0 个答案:

没有答案