I am making a simpel list code example for something, and I've got a bug I can't solve (should I be the one teaching here....? )
Anyway;
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*
Read the functions in the following order:
typedef struct list
main()
insert()
printlist()
*/
// ===============================================================================
/*
This is the data structure. Think of it as a block, with a specific size.
It is a specific size no matter what you store in it. For example, it is
the same size if I store "jamie" or "jam" in it, because "name[19] is always
size same length no matter how many characters you put into it. An int is
always the same memory size too.
Call it a node.
So, it has space for a string and an int.
It also has space for a POINTER to another NODE, which will be the NEXT
one in the list. It is called next (*next).
If this is NULL, it means we are at the end of the list.
E.g. if the list stored 4 items;
ITEM1 > *next > ITEM2 > *next > ITEM3 > *next > ITEM4 > NULL
*/
typedef struct list {
char name[19];
int number;
struct list *next;
} node;
void printlist( node *head )
{
// Create a temporary node.
// We want to start printing from the top of the list,
// which is our HEAD node.
node *tempnode = head;
// As long as tempnode exists, perform the stuff inside the loop.
while ( tempnode )
{
// Get the values from the list and print them.
printf("Name: %s, Number: %d\n", tempnode->name, tempnode->number);
// Now we have printed this value, go onto the next node along
// the list. Remember we said that if node->next is NULL, then we
// have reached the end of the list.
// If we reach NULL, then the while loop will end because tempnode
// will just contain NULL.
tempnode = tempnode->next;
}
}
node *insert( node *head, char name[], int number )
{
// Create a new node.
node *newnode = calloc( 1, sizeof(node) );
// Copy the values into the node.
// We have to use strcpy() to copy a string
strcpy(newnode->name, name);
newnode->number = number;
// This links the new node to the rest of the list
// by setting the next one along from the new node
// to head, and by setting the head to the new node.
// So, this means that newnode is now at the top of
// our list.
// If you don't understand, write a diagram out on paper.
newnode->next = head;
head = newnode;
return head;
}
int main( )
{
/*
Create a "head" node. This is the FIRST NODE
in the list.
*/
node *head;
/*
calloc() creates the "block" talked about earlier - creates
a block of memory just for the head node.
*/
head = calloc( 1, sizeof(node) );
/*
Insert values into the list.
*/
head = insert(head, "Jamie", 451);
head = insert(head, "Chris", 21345);
head = insert(head, "Rocco", 6852);
head = insert(head, "Andy", 45135);
/*
Call the printlist() function. We need to pass HEAD to it
because the function needs to know where the list starts
to print from it.
*/
printlist(head);
}
The code here, when printing the list with printlist() always prints out a blank at the end. Why is this?
Name: Andy, Number: 45135
Name: Rocco, Number: 6852
Name: Chris, Number: 21345
Name: Jamie, Number: 451
Name: , Number: 0