将新节点添加到列表中

时间:2014-10-15 03:12:52

标签: c linked-list char

嘿所以我想知道如何将新节点添加到列表中。所以有一个容纳列表的容器,我也需要添加它。其定义如下。 这是列表的容器

struct vm
{
    struct vm_list * item_list;
    struct coin * coins;
    char * foodfile;
    char * coinsfile;
};

这是保存节点的列表。

struct vm_list
{
    struct vm_node * head;
    unsigned length;
};

这是节点的定义

struct vm_node
{
    struct stock_item * data;
    struct vm_node * next;
};

这就是我试图将一个项目添加到列表中的方式。

BOOLEAN add_item(struct vm* vm)
{
    struct stock_item *newItem;
    newItem = malloc(sizeof(struct stock_item));
    struct vm_node *newNode; //setup a new item to store the data in.
    newNode = malloc(sizeof(struct vm_node));
    struct vm_node *VMP; //setup the new Node to hold the newItem
    VMP = malloc(sizeof(struct vm_node));
    struct vm_node *VMF; //too keep track of the first element in the list. So i can print out the list from the start.
    VMF = malloc(sizeof(struct vm_node));
    VMF->next = vm->item_list->head;
/* the following loops through until it finds the last element in the list. Which it stores in the VMP structure. So we can increment the ID counter by 1.*/
    while(vm->item_list->head){
        VMP->data = vm->item_list->head->data; //stores the last element of the linked list. Allowing us to add information to the nth bottom of the list.
        vm->item_list->head = vm->item_list->head->next;
    }
/* defining the items to be stored in the newItem struct. */
    char *newName;
    char *newDesc;
    char * onHand;
    char *price;
    newName = malloc(sizeof(char));
    newDesc = malloc(sizeof(char));
    price = malloc(sizeof(char));
    onHand = malloc(sizeof(char));
    char *dem = VMP->data->id; //returns the ID of the last item in the list.
    int change = atoi(&dem[4]); //changes the item to a int.
    change++; //increments by 1.
    char str;
    sprintf(&str, "%d", change); // converts back to char.
    dem[4]=str; // stores back in the char array.
    read_rest_of_line(); //buffer handler.
    printf("Enter the item name: ");
    newName = getlines();
    printf("\n");
    printf("Enter the item description: ");
    newDesc = getlines();
    printf("\n");
    printf("Enter how many will be stocked: ");
    onHand = getlines();
    printf("\n");
    printf("How much will the item cost: ");
    price = getlines();
    printf("\n");
//adding a new item to the list.
    newNode->next = NULL;
    newItem  = add_tobottomoflist(dem,newName,newDesc,onHand,price); //add_tobottom is setups the newItem.
    newNode->data = newItem; // sets the data for newNode = to newItem
    vm->item_list->head = newNode; //sets up the new Node.
    vm->item_list->head->next = VMF; //points to the first element in the list for linking.
    display_items(vm); // displays all the elements in the list.

    /* The UNUSED() function is designed to prevent warnings while your
     * code is only partially complete. Delete this function call once
     * you are using vm in your own code */
    UNUSED(vm);
    return FALSE;
}

运行代码时的输出如下所示。

IO        NAME                Description                                                           On Hand        price
I0006     Nam
                desc
                                                                 12             $3.04
p�                                                                                                -2108446504    $-2108446504.32615
I0001     Coke                375 ml Can of coke                                                    50             $3.50
I0002     Pepsi               375 ml Can of pepsi                                                   20             $3.00
I0003     Lemon Cheesecake    A delicious, 1/8 size slice of cheesecake                             10             $4.00
I0004     Mars Bar            A delicious 50 g Mars Bar chilled just the way you like it.           20             $3.00
I0006     Lemon Tart          A delicious lemon butter tart with a pastry based                     12             $3.75

柠檬馅饼ID应该等于I0005而不是I0006,我不知道它在哪里递增,并且新项目已经添加到列表的开头而不是最后。列表的开头似乎也有一个null对象 关于这里可能发生什么的任何想法?

1 个答案:

答案 0 :(得分:1)

我看到一些有问题的事情。 一,你在开始时的空位是VMF。你的评论说这个变量是为了跟踪列表的开头。你已经有head了。为什么要为此VMF生成更多内存?

您似乎正在尝试修改添加到列表开头的现有代码,而不是结束。要添加到最后,您必须更改while循环。在while循环中,遍历列表,并跟踪最后一个非null的节点。然后将last_node->next指向新节点