Linked list problem

This is a discussion on Linked list problem within the C Programming forums, part of the General Programming Boards category; Hi ,I am trying to understand linked list ,and I have hard time with the red part of the code ...

  1. #1
    lio
    lio is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Linked list problem

    Hi ,I am trying to understand linked list ,and I have hard time with the red part of the code ,is this a pointer to the first record
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef
    struct listNode {            
       char data; /* each listNode contains a character */
       struct listNode *nextPtr; /* pointer to next node*/ 
                     }ListNode; /* end structure listNode */
    typedef ListNode *ListNodePtr; /* synonym for ListNode* */
    void insert( ListNodePtr *sPtr, char value );
    void printList( ListNodePtr currentPtr );
    int main( void )
    { 
       ListNodePtr startPtr = NULL; /* initially there are no nodes */
       insert( &startPtr, 'a' ); 
       insert( &startPtr, 'b' );
       insert( &startPtr, 'c' );
       printList( startPtr );
       getch();
       return 0; 
    } /* end main */
    void insert( ListNodePtr *sPtr, char value )
    { 
       ListNodePtr newPtr;      /* pointer to new node */
       ListNodePtr previousPtr; /* pointer to previous node in list */
       ListNodePtr currentPtr;  /* pointer to current node in list */
       newPtr = malloc( sizeof( ListNode ) ); /* create node */
       if ( newPtr != NULL ) { /* is space available */
          newPtr->data = value; /* place value in node */
          newPtr->nextPtr = NULL; /* node does not link to another node */
          previousPtr = NULL;
          currentPtr = *sPtr;
          /* loop to find the correct location in the list */
          while ( currentPtr != NULL && value > currentPtr->data ) { 
             previousPtr = currentPtr;          /* walk to ...   */
             currentPtr = currentPtr->nextPtr;  /* ... next node */
          } /* end while */
         /* insert new node at beginning of list */
          if ( previousPtr == NULL ) { 
             newPtr->nextPtr = *sPtr;
             *sPtr = newPtr;
          } /* end if */
          else { /* insert new node between previousPtr and currentPtr */
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = currentPtr;
          } /* end else */
       } /* end if */
       else {
          printf( "%c not inserted. No memory available.\n", value );
       } /* end else */
    } /* end function insert */
    void printList( ListNodePtr currentPtr )
    { 
       /* if list is empty */
       if ( currentPtr == NULL ) {
          printf( "List is empty.\n\n" );
       } /* end if */
       else { 
          printf( "The list is:\n" );
         /* while not the end of the list */
          while ( currentPtr != NULL ) { 
             printf( "%c --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;   
          } /* end while */
          printf( "NULL\n\n" );
       } /* end else */
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    No, it's a typedef (a type definition). It simply creates a new type called ListNodePtr which is a ListNode *. It allows you to hide the * (which in my opinion is a bad thing).

    So this
    Code:
    ListNodePtr newPtr;      /* pointer to new node */
    is actually the same as
    Code:
    ListNode* newPtr;      /* pointer to new node */

  3. #3
    lio
    lio is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks,how shall i represent to be more understndable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Linked list ()
    By caraie in forum C Programming
    Replies: 7
    Last Post: 08-30-2010, 01:56 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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