Your code contains some obvious glitches.
struct node *new = null;
new = malloc(sizeof(struct node));
That is overdoing it with "safe" programming. You don't need to initialize the
new
pointer if you are going to assign to it in the very next statement.
Besides, the name
new
is not a good choice, because it is a keyword in C++. Some time later you want to upgrade to C++ and find that you have to edit a lot of places.
temp = list -> head;
while(temp != null)
{
count++;
temp = temp -> next;
}
temp -> next = new;
count is not used anywhere else, so you can throw it out. And you actually never use the count member of your llist structure.
The while loop runs one iteration too much. It runs until temp is NULL and then you try to assign to temp->next. That won't work.
And finally:
temp = list -> head;
while(temp != null)
{
temp = temp -> next;
total += temp -> data;
}
You need to exchange the two statements inside the loop. You want to first access temp->data and then move on to the next node.
Your first question:
If you have
struct A
{
int x;
int y;
};
struct B
{
struct A aaa;
int z;
};
And pB is a pointer to a B, then you would access x by: pB->aaa.x.