c - 如何将对象添加到单向链表

标签 c nodes singly-linked-list

正在将文件中的信息读入 Car 结构“newcar”。我需要使用下面的“sll_add”函数将该信息添加到我命名为“list”的 Carlist 类型的单个喜欢列表中。我只是无法理解这一切是如何开始的。感谢您的任何帮助。

int main (void)//main function
{ 

FILE *fp;//file pointer
fp = fopen("car_inventory.txt", "r");//opens the car inventory data within the program.
int num=0;//vairable created to keep track of the information.
int year;//, choice;//variables use for user input.
char str1[100];//string created to ensure the program reads the information from the original file correctly. 
Carlist list;//creates a string of type "Car" named file to hold up to 100 cars. 
Car newcar;//Car variable to store the info from the file. 

list *last=num;

if (fp)
    {
        {                   
        while(!feof(fp))//takes input from the data file.
            {
        fgets(str1,50, fp);
        sscanf(str1,"%d %s %[^\n]s", &newcar.year, newcar.make, newcar.model);

        fgets(str1,50, fp);
        sscanf(str1,"%[^\n]s", newcar.style);

        fgets(str1,50, fp);
        sscanf(str1,"%[^\n]s", newcar.color);

        fgets(str1,50, fp);
        sscanf(str1,"%s", newcar.mileage);

        fgets(str1,50, fp);
        sscanf(str1,"%c", &newcar.air_condition);

        fgets(str1,50, fp);
        sscanf(str1,"%s", newcar.inventory_num);

        fgets(str1,50, fp);
        sscanf(str1,"%lf", &(newcar).price);

        fgets(str1,50, fp);
        sscanf(str1,"%[^\n]s", newcar.previous_owner);

        fgets(str1,50,fp);


        num++;
        sll_add(*newcar, &list);
            }
        }

return 0;


#define MAX_CARS     100

/* Type definitions */

typedef struct car
{
    int year;
    char make[25];
    char model[25];
    char style[25];
    char color[20];
    char mileage[8];
    char air_condition;
    char inventory_num[16];
    double price;
    char previous_owner[30];
    struct car *next;

} Car;


typedef struct carlist
{
    Car *first;
    Car *last;
} Carlist;


void sll_init(Carlist *l);
Car *sll_first(Carlist *l);
Car *sll_end(Carlist *l);
Car *sll_next(Car *current, Carlist *l);
Car *sll_retrieve(Car *element, Carlist *l);
void sll_delete(Car *c, Carlist *l);
void sll_delete_year(Carlist *l, int year);
void sll_add(Car *newcar, Carlist *l);


    #endif

最佳答案

从头开始。首先,您的 Carlist 需要初始化,因此您需要充实 sll_init() 函数。这个非常简单,因为它需要做的就是将 firstlast 指针设置为空。它可能看起来像这样:

void sll_init(Carlist *l)
{
    l->first = l->last = NULL;
}

接下来,您需要能够将条目添加到您的列表中,因此您需要充实 sll_add()。它可能看起来像这样:

void sll_add(Car *newcar, Carlist *l)
{
    Car *new = malloc(sizeof(*new));
    *new = *newcar;
    new->next = NULL;    // just to be safe in case newcar wasn't properly initialized

    if (l->first == NULL)
    {
        // the list is empty, so add the first entry
        l->first = l->last = new;
    }
    else
    {
        // the list is not empty, so append a new entry to the end
        l->last->next = new;
        l->last = new;
    }
}

有了这个,您应该可以开始了。

关于c - 如何将对象添加到单向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989096/

相关文章:

mysql - C、如何将字符串保存到二进制文件中?

c - 使用多个自定义信号处理程序在程序中阻止 SIGCHILD

c - 如何修复 C 中的 PCAP_SRC_IF_STRING

c# - 在 umbraco 中为节点创建默认子节点

java - XML 将 : Loop through a child node and save field, 值解析为 HashMap

将程序集转换为等效的 C 代码

nodes - 更新 cytoscape.js 中的图形 : node positions not updated

Java:通用类异常

java - 将节点插入链表中间,不小心也插入了空节点

c - 结构指针分配未分配预期值 - 链表