Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Concatenate the current linked list with a built-in list D->E->F. 2)Merge the current list with a built-in list G->H->I into a single ordered list.

1) Concatenate the current linked list with a built-in list D->E->F.

2)Merge the current list with a built-in list G->H->I into a single ordered list.

For the first part I need to create a built in list and with D E F and concatenate with with current list that is being added as the code states it asks the user to add a character to a list then displays the list. I need to add this function of concatenating into the code as an another option that the user can input. For instance if the option were to be 3 for the user to "concatenate the built in list D->E->F" and the user already put in A and B in the current list the output would look like A->B->D->E->F.

For the second part I need to add another function where I need to merge G H I into the current list that is being added by the user in the current list . This also needs to be added by prompting the user with a choice so the user would be prompted with option 4 "merge G H I into the current list". And when this is done correctly if the user typed in the characters A and B then chose option 4 the output should look like this, A->B->G->H->I.

/* Fig. 12.3: fig12_03.c Operating and maintaining a list, which adds characters in alphabetical (ASCII) order */ #include #include /* self-referential structure */ struct listNode { char data; /* each listNode contains a character */ struct listNode *nextPtr; /* pointer to next node*/ }; /* end structure listNode */ typedef struct listNode ListNode; /* synonym for struct listNode */ typedef ListNode *ListNodePtr; /* synonym for ListNode* */ /* prototypes */ void insert( ListNodePtr *sPtr, char value ); char delete( ListNodePtr *sPtr, char value ); int isEmpty( ListNodePtr sPtr ); void printList( ListNodePtr currentPtr ); void instructions( void ); int main() { ListNodePtr startPtr = NULL; /* initially there are no nodes */ int choice = 0; /* user's choice */ char item = 0; /* char entered by user */ instructions(); /* display the menu */ printf( "? " ); scanf( "%d", &choice ); /* loop while user does not choose 3 */ while ( choice != 3 ) { switch ( choice ) { case 1: printf( "Enter a character: " ); scanf( " %c", &item ); insert( &startPtr, item ); /* insert item in list */ printList( startPtr ); break; case 2: /* if list is not empty */ if ( !isEmpty( startPtr ) ) { printf( "Enter character to be deleted: " ); scanf( " %c", &item ); /* if character is found, remove it */ if ( delete( &startPtr, item ) ) { /* remove item */ printf( "%c deleted. ", item ); printList( startPtr ); } /* end if */ else { printf( "%c not found. ", item ); } /* end else */ } /* end if */ else { printf( "List is empty. " ); } /* end else */ break; default: printf( "Invalid choice. " ); instructions(); break; } /* end switch */ printf( "? " ); scanf( "%d", &choice ); } /* end while */ printf( "End of run. " ); return 0; /* indicates successful termination */ } /* end main */ /* display program instructions to user */ void instructions( void ) { printf( "Enter your choice: " " 1 to insert an element into the list. " " 2 to delete an element from the list. " " 3 to end. " ); } /* end function instructions */ /* Insert a new value into the list in sorted order */ void insert( ListNodePtr *sPtr, char value ) { ListNodePtr newPtr = NULL; /* pointer to new node */ ListNodePtr previousPtr = NULL; /* pointer to previous node in list */ ListNodePtr currentPtr = NULL; /* pointer to current node in list */ newPtr = malloc( sizeof( ListNode ) ); /* create node on heap */ 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. ", value ); } /* end else */ } /* end function insert */ /* Delete a list element */ char delete( ListNodePtr *sPtr, char value ) { ListNodePtr previousPtr = NULL; /* pointer to previous node in list */ ListNodePtr currentPtr = NULL; /* pointer to current node in list */ ListNodePtr tempPtr = NULL; /* temporary node pointer */ /* delete first node */ if ( value == ( *sPtr )->data ) { tempPtr = *sPtr; /* hold onto node being removed */ *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */ free( tempPtr ); /* free the de-threaded node */ return value; } /* end if */ else { previousPtr = *sPtr; currentPtr = ( *sPtr )->nextPtr; /* loop to find the correct location in the list */ while ( currentPtr != NULL && currentPtr->data != value ) { previousPtr = currentPtr; /* walk to ... */ currentPtr = currentPtr->nextPtr; /* ... next node */ } /* end while */ /* delete node at currentPtr */ if ( currentPtr != NULL ) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; free( tempPtr ); return value; } /* end if */ } /* end else */ return '\0'; } /* end function delete */ /* Return 1 if the list is empty, 0 otherwise */ int isEmpty( ListNodePtr sPtr ) { return sPtr == NULL; } /* end function isEmpty */ /* Print the list */ void printList( ListNodePtr currentPtr ) { /* if list is empty */ if ( currentPtr == NULL ) { printf( "List is empty. " ); } /* end if */ else { printf( "The list is: " ); /* while not the end of the list */ while ( currentPtr != NULL ) { printf( "%c --> ", currentPtr->data ); currentPtr = currentPtr->nextPtr; } /* end while */ printf( "NULL " ); } /* end else */ } /* end function printList */ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

67% of the women attending

Answered: 1 week ago

Question

What are the purposes of promotion ?

Answered: 1 week ago

Question

3. An overview of the key behaviors is presented.

Answered: 1 week ago

Question

2. The model is credible to the trainees.

Answered: 1 week ago