Results 1 to 3 of 3

Thread: linked lists

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Jun:2010
    Location:
    Posts: 171

    linked lists

    , linked list

    typedef ListNode *ListNodePtr; /* synonym for ListNode* */

    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 */
    }
    If you understand what you're doing, you're not learning anything.

  2. #2
    1+1=11 svr's Avatar
    Join Date: Sep:2006
    Location: Inferno, Act 1
    Posts: 790
    typedef- . , , :

    Code:
    typedef ListNode *ListNodePtr;
    :

    Code:
    typedef ListNode* ListNodePtr;
    ListNodePtr ListNode*

    typedef e #define, , . (, - Java, )
    MSI B450 Gaming Pro Carbon AC | Ryzen 9 5900x | HyperX Predator 3200 | Asus Strix 3090 | Kingston KC3000 2TB | WD Red 4TB | Dell G3223Q + LG 27UK650-W | Arctic Freezer 360 | Seasonic Focus GX-1000 | Lian Li O11 Dynamic

  3. #3
    Registered User
    Join Date: Jun:2010
    Location:
    Posts: 171
    If you understand what you're doing, you're not learning anything.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |