Simple list code

This is a discussion on Simple list code within the C Programming forums, part of the General Programming Boards category; I am making a simpel list code example for something, and I've got a bug I can't solve (should I ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Simple list code

    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

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You're allocating memory for your head pointer in main() for no reason. This initial head node always ends up at the end of your list. However, your intention for the head pointer is just to the point to the first node in the list rather than be an actual node instance like you're experiencing now. Instead, just do node *head = NULL; in main() and don't allocate any memory for it. That should fix your problem.

    Also, be aware that calloc() is not necessarily the best for creating a NULL pointer. It zeroes out the memory, but NULL is not always a zero value on all systems. It might work in the environment you're using, but you know what they say about bad habits. It's best to specifically set pointers to NULL when that's what you intend.

    Here's a link in reference to that claim: http://c-faq.com/null/machexamp.html
    Last edited by itsme86; 05-24-2006 at 02:16 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 04:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21