Advertisement
Doragonroudo

EGCO112 #4.1 - Example

Feb 23rd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.47 KB | None | 0 0
  1. // Fig. 12.3: fig12_03.c
  2. // Inserting and deleting nodes in a list
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. // self-referential structure                      
  7. struct Node {                                      
  8.    int data; // each listNode contains a character
  9.    struct Node *nextPtr; // pointer to next node
  10. }; // end structure listNode                        
  11.  
  12. typedef struct Node LLnode; // synonym for struct listNode
  13. typedef LLnode *LLPtr; // synonym for ListNode*
  14.  
  15. // prototypes
  16.  
  17. int deletes( LLPtr *sPtr, int value );
  18. int isEmpty( LLPtr sPtr );
  19. void insert( LLPtr *sPtr, int value );
  20. void printList( LLPtr currentPtr );
  21. void instructions( void );
  22.  
  23. int main( void )
  24. {
  25.    LLPtr startPtr = NULL; // initially there are no nodes
  26.    unsigned int choice; // user's choice
  27.    int item; // char entered by user
  28.  
  29.    instructions(); // display the menu
  30.    printf( "%s", "? " );
  31.    scanf( "%u", &choice );
  32.  
  33.    // loop while user does not choose 3
  34.    while ( choice != 3 ) {
  35.  
  36.       switch ( choice ) {
  37.          case 1:
  38.             printf( "%s", "Enter a number: " );
  39.             scanf( "%d", &item );
  40.             insert( &startPtr, item ); // insert item in list
  41.             printList( startPtr );
  42.             break;
  43.          case 2: // delete an element
  44.             // if list is not empty
  45.             if ( !isEmpty( startPtr ) ) {
  46.                printf( "%s", "Enter number to be deleted: " );
  47.                scanf( "%d", &item );
  48.  
  49.                // if character is found, remove it
  50.                if ( deletes( &startPtr, item ) ) { // remove item
  51.                   printf( "%d deleted.\n", item );
  52.                   printList( startPtr );
  53.                } // end if
  54.                else {
  55.                   printf( "%d not found.\n\n", item );
  56.                } // end else
  57.             } // end if
  58.             else {
  59.                puts( "List is empty.\n" );
  60.             } // end else
  61.  
  62.             break;
  63.          default:
  64.             puts( "Invalid choice.\n" );
  65.             instructions();
  66.             break;
  67.       } // end switch
  68.  
  69.       printf( "%s", "? " );
  70.       scanf( "%u", &choice );
  71.    } // end while
  72.  
  73.    puts( "End of run." );
  74. } // end main
  75.  
  76. // display program instructions to user
  77. void instructions( void )
  78. {
  79.    puts( "Enter your choice:\n"
  80.       "   1 to insert an element into the list.\n"
  81.       "   2 to delete an element from the list.\n"
  82.       "   3 to end." );
  83. } // end function instructions
  84.  
  85. // insert a new value into the list in sorted order
  86. void insert( LLPtr *sPtr, int value )
  87. {
  88.    LLPtr newPtr; // pointer to new node
  89.    LLPtr previousPtr; // pointer to previous node in list
  90.    LLPtr currentPtr; // pointer to current node in list
  91.  
  92.    newPtr =(LLPtr) malloc( sizeof( LLnode ) ); // create node
  93.  
  94.    if ( newPtr != NULL ) { // is space available
  95.       newPtr->data = value; // place value in node
  96.       newPtr->nextPtr = NULL; // node does not link to another node
  97.  
  98.       previousPtr = NULL;
  99.       currentPtr = *sPtr;
  100.  
  101.       // loop to find the correct location in the list      
  102.       while ( currentPtr != NULL && value > currentPtr->data ) {
  103.          previousPtr = currentPtr; // walk to ...              
  104.          currentPtr = currentPtr->nextPtr; // ... next node
  105.       } // end while                                        
  106.  
  107.       // insert new node at beginning of list
  108.       if ( previousPtr == NULL ) {
  109.          newPtr->nextPtr = *sPtr;
  110.          *sPtr = newPtr;
  111.       } // end if
  112.       else { // insert new node between previousPtr and currentPtr
  113.          previousPtr->nextPtr = newPtr;
  114.          newPtr->nextPtr = currentPtr;
  115.       } // end else
  116.    } // end if
  117.    else {
  118.       printf( "%d not inserted. No memory available.\n", value );
  119.    } // end else
  120. } // end function insert
  121.  
  122. // delete a list element
  123. int deletes( LLPtr *sPtr, int value )
  124. {
  125.    LLPtr previousPtr; // pointer to previous node in list
  126.    LLPtr currentPtr; // pointer to current node in list
  127.    LLPtr tempPtr; // temporary node pointer
  128.  
  129.    // delete first node
  130.    if ( value == ( *sPtr )->data ) {
  131.       tempPtr = *sPtr; // hold onto node being removed
  132.       *sPtr = ( *sPtr )->nextPtr; // de-thread the node
  133.       free( tempPtr ); // free the de-threaded node
  134.       return value;
  135.    } // end if
  136.    else {
  137.       previousPtr = *sPtr;
  138.       currentPtr = ( *sPtr )->nextPtr;
  139.  
  140.       // loop to find the correct location in the list
  141.       while ( currentPtr != NULL && currentPtr->data != value ) {
  142.          previousPtr = currentPtr; // walk to ...  
  143.          currentPtr = currentPtr->nextPtr; // ... next node  
  144.       } // end while
  145.  
  146.       // delete node at currentPtr
  147.       if ( currentPtr != NULL ) {
  148.          tempPtr = currentPtr;
  149.          previousPtr->nextPtr = currentPtr->nextPtr;
  150.          free( tempPtr );
  151.          return value;
  152.       } // end if
  153.    } // end else
  154.  
  155.    return '\0';
  156. } // end function delete
  157.  
  158. // return 1 if the list is empty, 0 otherwise
  159. int isEmpty( LLPtr sPtr )
  160. {
  161.    return sPtr == NULL;
  162. } // end function isEmpty
  163.  
  164. // print the list
  165. void printList( LLPtr currentPtr )
  166. {
  167.    // if list is empty
  168.    if ( isEmpty( currentPtr ) ) {
  169.       puts( "List is empty.\n" );
  170.    } // end if
  171.    else {
  172.       puts( "The list is:" );
  173.  
  174.       // while not the end of the list
  175.       while ( currentPtr != NULL ) {
  176.          printf( "%d --> ", currentPtr->data );
  177.          currentPtr = currentPtr->nextPtr;  
  178.       } // end while
  179.  
  180.       puts( "NULL\n" );
  181.    } // end else
  182. } // end function printList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement