I'm not sure if you are misusing some of the terminology or if your instructor is teaching non-standard linked lists.
Let's define a node for your program. It would probably be a structure with three member variables:
- a char to hold the letter
- an int to hold the count
- a pointer to point to the next node in the list (if the node is the last node in the list, then the pointer would be set to null)
You mention a 'head' node. This is one place where I think that you are confusing the terminology.
You don't have a head node, you have a 'head' pointer. This is a pointer to the first node in the list. This pointer is not an actual node, just a pointer to the first node.
Let's assume that your node structure is defined as:
struct nodestruct {
char letter;
int count;
struct nodestruct *next;
}
You'd declare the head pointer as:
struct nodestruct *head = NULL;
So 'head' is pointing at nothing. The list is empty.
The first time that you create a node (for 'h'), you'll add the new node so that it is first on the list.
struct nodestruct *node;
node = calloc(1,sizeof(struct nodestruct));
node->letter = 'h';
node->count = 1;
node->next = NULL;
When you go to search the list, you'll start at 'head'. If head == NULL, then you know that the list is empty, so it is a special case in the code to add the first node.
head = node;
head now points to the first node and the node's next value is NULL.
Now, with the second word, you'll create a new node:
node = calloc(1,sizeof(struct nodestruct));
node->letter = 'm';
node->count = 1;
node->next = NULL;
To verify that there is a non-empty list to search, do (as before):
if (head == NULL) {
..add first node code here
} else {
.. search list code here...
}
To search the list, you start with the first node:
struct nodestruct *ptr;
ptr = head; /* ptr now points to the first node on the list */
/* Does this node contain the letter that you want? */
if (ptr->letter == 'm') {
.. got a match, so can increment the count and exit the search loop
}
/* if didn't match, move on to the next node */
ptr = ptr->next;
Remember, ptr is pointing to a node and ptr->next contains the address of the next node in the list. So the above line, copies the address of the next node in the list into the variable 'ptr'. So now 'ptr' points to the next node in the list. If 'ptr' is now null, then that means that you've reached the end of the list.
I'm walking the line between explaining how this works and not writing the code for you. So I've included some parts, but not always in the exact way you'll need to use them.
Let's see how much this helps.
Be sure to come back and let us know if our suggestions helped!