printing link list using a function. [don't know what argument to pass]

This is a discussion on printing link list using a function. [don't know what argument to pass] within the C Programming forums, part of the General Programming Boards category; I created a program which prints out the alphabet in reverse order i.e. z y x w etc.. I'm trying ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    printing link list using a function. [don't know what argument to pass]

    I created a program which prints out the alphabet in reverse order i.e. z y x w etc..

    I'm trying to call a function which prints out the list however, I'm not sure what argument to pass into the function

    code is below

    Code:
    /*Linked list to display the letters of the alphabet in REVERSE order*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char DATA;
    
    /*Create the node*/
    
     struct node{
    
    	DATA d;
    
    	struct node *next;
    
    	};
    
    
    
    /*pointers to node. Declared so that memory may be allocated*/
    
    struct node *first_item;
    
    struct node *current_item;
    
    int main()
    
    {
    
    	int n;
    
    	first_item = NULL;
    
    	
    
    	for (n='a';n<='z';n++)
    
    	{
    
    	  current_item = (struct node *)malloc(sizeof(struct node)); 
    
    	  current_item->d = n;
    
    	  current_item->next = first_item;
    
    	  first_item=current_item;	
      
    	}
    
    	showlist(??);
    
    getchar();
    
    return(0);
    
    }
    
    
    void showlist(??);
    {
    	current_item=first_item;
    
    	while(current_item!=NULL)
    
    	{
    
    		printf("%c  ", current_item->d);
    
    		current_item=current_item->next;
    }
    
    	}

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The answer is pretty much what you'd expect:

    Code:
    void showlist( struct node *first_item)
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    thanks for your reply. THe program prints the list now.

    Just wondering if my understanding of linked list (reverse order is correct)

    1. first_item (head) points to null
    2. current_item structure is then created and data 'A' is added. The second member of the element *next is set to first_item(head)
    3. then pointer first_item is set to current_item
    4. the final linked list will look like the one in yellow
    3.

    http://i41.tinypic.com/63vl7s.png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-19-2011, 08:18 PM
  2. How to pass an argument into a function?
    By mykolg in forum C Programming
    Replies: 13
    Last Post: 11-22-2010, 11:12 AM
  3. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. How to pass an array of strings as a function argument?
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 04-02-2007, 10:38 AM

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