Hey, I am currently following a code skeleton to implement the missing code for some function. Below is only some part of the implementation I've done so far. Yes it is not much yet...
I got stuck for the implementation of the list_add() function.

What I want to do there is to create a new node, and allocate some memory to it so I afterward can insert values to it. However I get compilation error. I have trial error it for awhile now without founding any solution. So I hoped for if someone know what error it is.

The error I get is

error: invalid conversion from `void*' to `my_list*'

#include <iostream>

using namespace std;

struct list_item
{
    int key;                // identifies the data
    double value;           // the data stored
    struct list_item* next; // a pointer to the next data
};

struct my_list
{
    struct list_item* first; // a pointer to the first element of the list
};

void list_init(struct my_list* my_this)
{
    my_this = NULL;
}

void list_add(struct my_list* my_this, int key, double value)
{
    my_list *new_node;
	
	//allocate memory for next node
    new_node = malloc(sizeof(my_list));
}

int main()
{
	my_list a;
	list_init(a);
	
	return 0;
}

Your code is C++, not C. In C++ void* conversions are not implicit and require a cast.

Ah sry, I was unsure whether it is C or C++. I guess C since it uses malloc...

anyway I've continued on it and got stuck again...

void list_add(struct my_list* my_this, int key, double value)
{
    cout << key << " " << value << endl;
    my_list *new_node;

	//allocate memory for next node
	//typecast to (my_list) is needed here
    new_node = (my_list*)malloc(sizeof(my_list));

    if(my_this)
    {
        new_node->first->key = key;
        new_node->first->value = value;

        new_node->first->next = my_list->first;

    }
}

Now that I have assigned values to the new created node, I want to attach it to the linked list.

So I'm guessing this part
new_node->first->next = my_list->first;
needs to be attached to my_list->first. However doesnt work so well. It is complaining about "expected primary-expression...."

>new_node->first->next = my_list->first;
my_list is a type, not an object. Perhaps you meant:

new_node->first->next = my_this->first;

>new_node->first->next = my_list->first;
my_list is a type, not an object. Perhaps you meant:

new_node->first->next = my_this->first;

I did wrong there, thanks for pointing out...
I hit at problem again though. I have written a test code to see whether it will add to the linked list without problem. It will add the first set of values without problem. However it crashes during the second add.

I thought I got most of the requisite now.
1. create a new node
2. add values to the node
3. point the new nodes to the first node
4. change first node address to the new node

Have I missed out something?

void list_add(struct my_list* my_this, int key, double value)
{
    // ADD YOUR CODE HERE (approx 23 lines)
    cout << key << " " << value << endl;
    my_list *new_node;

	//allocate memory for next node
	//typecast to (my_list) is needed here
    new_node = (my_list*)malloc(sizeof(my_list));

    if(my_this)
    {
        cout << "pang" << endl;

        //adding key and value
        new_node->first->key = key;
        new_node->first->value = value;

        new_node->first->next = my_this->first;

        //point new_node to become the first element
        my_this->first = new_node->first;

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.