This program is a little lengthy but my question is with the main body and first function.
1. This program has
typedef ListNode *ListNodePtr;
Does this mean any struct initialized with ListNodePtr will automatically be a pointer?
2. Switch case 1 in the main body calls the insert function like so
insert( &startPtr, item );
startPtr was declared as type ListNodePtr. Assuming that startPtr is a pointer, why does the function call using the address operator. To get the address the ptr is located at?
Code:#include <stdio.h> #include <stdlib.h> struct listNode { char data; struct listNode *nextPtr; }; typedef struct listNode ListNode; typedef ListNode *ListNodePtr; /* Does this mean any structure declared using ListNodePtr will automatically be a pointer without using the * cast? */ void insert( ListNodePtr *, char ); char delete( ListNodePtr *, char ); int isEmpty( ListNodePtr ); void printList( ListNodePtr ); void instructions( void ); int main() { ListNodePtr startPtr = NULL; int choice; char item; instructions(); printf( "? " ); scanf( "%d", &choice ); while( choice != 3 ) { switch( choice ) { case 1: printf( "Enter a character: " ); scanf( "\n%c", &item ); /* startPtr is a pointer. What does it mean to use the address operator on a pointer? */ insert( &startPtr, item ); printList( startPtr ); break; case 2: if ( !isEmpty( startPtr ) ) { printf( "Enter character to be deleted: " ); scanf( "\n%c", &item ); if ( delete( &startPtr, item ) ) { printf( "%c deleted.\n", item ); printList( startPtr ); } else printf( "%c not found.\n\n", item ); } else printf( "List is empty.\n\n" ); break; default: printf( "Invalid choice.\n\n" ); instructions(); break; } printf( "? " ); scanf( "%d", &choice ); } printf( "End of run.\n" ); return 0; } void instructions( void ) { printf( "Enter your choice:\n" " 1 to insert an element into the lsit.\n" " 2 to delete an element from the list.\n" " 3 to end.\n" ); } void insert( ListNodePtr *sPtr, char value ) { ListNodePtr newPtr, previousPtr, currentPtr; newPtr = malloc( sizeof( ListNode ) ); if ( newPtr != NULL ) { newPtr->data = value; newPtr->nextPtr = NULL; previousPtr = NULL; currentPtr = *sPtr; /* Is currentPtr pointing to the value at sPtr or the address of sPtr? */ while ( currentPtr != NULL && value > currentPtr->data ) { previousPtr = currentPtr; currentPtr = currentPtr ->nextPtr; } if ( previousPtr == NULL ) { newPtr->nextPtr = *sPtr; *sPtr = newPtr; } else { previousPtr->nextPtr = newPtr; newPtr->nextPtr = currentPtr; } } else printf( "%c not inserted. No memory available.\n", value ); } char delete( ListNodePtr *sPtr, char value ) { ListNodePtr previousPtr, currentPtr, tempPtr; if ( value == ( *sPtr )->data ) { tempPtr = *sPtr; *sPtr = ( *sPtr )->nextPtr; free( tempPtr ); return value; } else { previousPtr = *sPtr; currentPtr = ( *sPtr )->nextPtr; while ( currentPtr != NULL && currentPtr->data != value ) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if ( currentPtr != NULL ) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; free( tempPtr ); return value; } } return '\0'; } int isEmpty( ListNodePtr sPtr ) { return sPtr == NULL; } void printList( ListNodePtr currentPtr ) { if ( currentPtr == NULL ) printf( "List is empty.\n\n" ); else { printf( "The list is:\n" ); while ( currentPtr != NULL ) { printf( "%c --> ", currentPtr->data ); currentPtr = currentPtr->nextPtr; } printf( "NULL\n\n" ); } }



LinkBack URL
About LinkBacks




