Thread: Linked list generates errors..

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Linked list generates errors..

    Hi,

    Below is a program i wrote which generates 25 random numbers and store it in a linked list. Then the program should output the total of all elements ( which are integers ) in the list and also the floating-point average of the list. But when i run it, it generates errors. Pls give a hand and tell me what's wrong. It's my first program to use linked lists. Thnx in advance.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode {
       struct listNode *nextPtr;
       int data;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    void printList( ListNodePtr currentPtr );
    int generateList( ListNodePtr *startPtr );
    
    int main()
    {
       int total;
       ListNodePtr startPtr = NULL;
    
       total = generateList( &startPtr );
       printList( startPtr );
    
       printf( "\nTotal of all 25 elements is %d", total );
       printf( "\nAverage of all 25 elements is %.2f\n\n", total / 25 );
    
       getch();
       return 0;
    }
    
    /* Generate random list - returns sum of all elements */
    int generateList( ListNodePtr *startPtr )
    {
       int randNum, total = 0, i = 0;
       ListNodePtr newPtr, currentPtr, previousPtr;
    
       srand( time( NULL ) );
    
       previousPtr = NULL;
       currentPtr = *startPtr;
    
       for (; i < 25; i++ ) {
          randNum = rand() % 100;
          newPtr = malloc( sizeof( ListNode ) );
          newPtr->data = randNum;
          previousPtr->nextPtr = currentPtr;
          currentPtr->nextPtr = newPtr;
          total = total + randNum;
       }
       return total;
    }
    
    /* Print the list */
    void printList( ListNodePtr currentPtr )
    { 
       if ( currentPtr == NULL )
          printf( "List is empty.\n\n" );
       else { 
          printf( "The list is:\n" );
    
          while ( currentPtr != NULL ) { 
             printf( "%d --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;
          }
    
          printf( "NULL\n\n" );
       }
    }

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    no problems now, i corrected it. But why doesn't the floating-point number calculated correctly? Can't i do this:

    Code:
    printf( "%.2f", 1111 / 25 );
    It prints 0.00

    But when save the calculation in a float variable and displays it, it's rounded off. Why ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode {
       struct listNode *nextPtr;
       int data;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    void printList( ListNodePtr currentPtr );
    int generateList( ListNodePtr *startPtr );
    
    int main()
    {
       int total;
       ListNodePtr startPtr = NULL;
       float average;
    
       total = generateList( &startPtr );
       average = total / 25;
       printList( startPtr );
    
       printf( "\nTotal of all 25 elements is %d", total );
       printf( "\nAverage of all 25 elements is %.2f\n\n", average );
    
       getch();
       return 0;
    }
    
    /* Generate random list - returns sum of all elements */
    int generateList( ListNodePtr *startPtr )
    {
       int randNum, total = 0, i = 0;
       ListNodePtr newPtr, currentPtr, previousPtr;
    
       srand( time( NULL ) );
    
       previousPtr = NULL;
       currentPtr = *startPtr;
    
       for (; i < 25; i++ ) {
          newPtr = malloc( sizeof( ListNode ) );
    
          if ( newPtr != NULL ) {
             randNum = rand() % 100;
             newPtr->data = randNum;
    
             if ( previousPtr == NULL ) {
                newPtr->nextPtr = *startPtr;
                *startPtr = newPtr;
             }
             else {
                previousPtr->nextPtr = newPtr;
                newPtr->nextPtr = currentPtr;
             }
    
             total = total + randNum;
          }
          else {
             printf( "\nNot enough memory.\n\n" );
             break;
          }
       }
       return total;
    }
    
    /* Print the list */
    void printList( ListNodePtr currentPtr )
    { 
       if ( currentPtr == NULL )
          printf( "List is empty.\n\n" );
       else { 
          printf( "The list is:\n" );
    
          while ( currentPtr != NULL ) { 
             printf( "%d --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;
          }
    
          printf( "NULL\n\n" );
       }
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > printf( "%.2f", 1111 / 25 );
    It's done using integer division, and passed as an integer to printf (not a good idea).

    Force it to be a float expression, by saying
    printf( "%.2f", 1111.0 / 25 );

    printf( "%.2f", (float)1111 / 25 );

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    thnx, but i just found out that my program doesn't really work. The version i posted just puts each number generate BEFORE the previous number. But i want it AFTER the previous number. I edited and changed my code back and forth for several hours and still couldn't get it to work. Here is my final code. Pls tell me where to change the code to make it work, i've done my best. Thnx in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode {
       struct listNode *nextPtr;
       int data;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    void printList( ListNodePtr currentPtr );
    int generateList( ListNodePtr *startPtr );
    
    int main()
    {
       int total;
       ListNodePtr startPtr = NULL;
    
       total = generateList( &startPtr );
       printList( startPtr );
    
       printf( "\nTotal of all 25 elements is %d", total );
       printf( "\nAverage of all 25 elements is %.2f\n\n", ( float ) total / 25 );
    
       getch();
       return 0;
    }
    
    /* Generate random list - returns sum of all elements */
    int generateList( ListNodePtr *startPtr )
    {
       int randNum, total = 0, i = 0;
       ListNodePtr newPtr, endPtr, previousPtr;
    
       srand( time( NULL ) );
    
       previousPtr = NULL;
       endPtr = *startPtr;
    
       for (; i < 25; i++ ) {
          newPtr = malloc( sizeof( ListNode ) );
    
          if ( newPtr != NULL ) {
             randNum = rand() % 100;
             newPtr->data = randNum;
    
             if ( previousPtr == NULL ) {
                newPtr->nextPtr = *startPtr;
                *startPtr = newPtr;
                printf( "\nRandom number is: %d\n", randNum );
             }
    
    
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = endPtr;
             endPtr = endPtr->nextPtr;
    
    
             total = total + randNum;
          }
          else {
             printf( "\nNot enough memory.\n\n" );
             break;
          }
       }
       return total;
    }
    
    /* Print the list */
    void printList( ListNodePtr currentPtr )
    { 
       if ( currentPtr == NULL )
          printf( "List is empty.\n\n" );
       else { 
          printf( "The list is:\n" );
    
          while ( currentPtr != NULL ) { 
             printf( "%d --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;
          }
    
          printf( "NULL\n\n" );
       }
    }

  5. #5
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Just working the logic.. needs more polish..

    Code:
    #include <stdio.h>
    //#include <conio.h> /*uncomment if you are using TC in DOS*/
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode {
       struct listNode *nextPtr;
       int data;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    void printList( ListNodePtr currentPtr );
    int generateList( ListNodePtr *startPtr );
    
    int main()
    {
       int total;
       ListNodePtr startPtr = NULL;
    
    generateList( &startPtr );
    
    total = get_total(startPtr);
    
       printList( startPtr );
    
       printf( "\nTotal of all 25 elements is %d", total );
       printf( "\nAverage of all 25 elements is %.2f\n\n", total / 25 );
    
       getchar();
       return 0;
    }
    
    /* Generate random list - returns sum of all elements */
    int generateList( ListNodePtr *startPtr )
    {
       int randNum, total = 0, i = 0;
       ListNodePtr newPtr, currentPtr;
       srand( time( NULL ) );
    
       
    
       for (; i < 25; i++ ) {
          randNum = rand() % 100;
          newPtr = malloc( sizeof( ListNode ) );
          newPtr->data = randNum;
          newPtr->nextPtr = NULL;
    
    	if(*startPtr == NULL)	{ 
    	*startPtr = newPtr; 
    	currentPtr=newPtr;
    	continue;
    	}
          currentPtr->nextPtr=newPtr;	
          currentPtr=currentPtr->nextPtr;
       }
       return 0;
    }
    
    
    
    /* Print the list */
    void printList( ListNodePtr currentPtr )
    { 
       if ( currentPtr == NULL )
          printf( "List is empty.\n\n" );
       else { 
          printf( "The list is:\n" );
    
          while ( currentPtr != NULL ) { 
             printf( "%d --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;
          }
    
          printf( "NULL\n\n" );
       }
    }
    
    
    int get_total(ListNodePtr currentPtr)
    {
    
    if(currentPtr==NULL) return 0;
    
    return currentPtr->data + get_total(currentPtr->nextPtr);
    
    }
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM