help! Placement of nodes in a Linked List
hey everyone I am creating a program that creates a linked list and the program has to have a menu to let the user 1.add to the beginning of the list. 2. add to the end of the list. 3. delete a particular character from the list 4. prints the list and 5. exits. I have the whole structure of the program done but I cannot get the adding to the beginning or the end of the list part down. No matter what you do if there is already an "A" or a "C" in the list, if you add "B" it will put the character right in between the "A" and the "C". Please help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* self-referential structure */
struct listNode {
char data; /* each listnode contains a character */
struct listNode *nextPtr;
};
typedef struct listNode ListNode ;
typedef ListNode *ListNodePtr;
/* prototypes */
void insertbegin( ListNodePtr *sPtr, char value );
void insertend( ListNodePtr *sPtr, char value );
char delete( ListNodePtr *sPtr, char value );
int isEmpty (ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
/***************************************************************************/
int main( void )
{
ListNodePtr startPtr = NULL; /* init no nodes */
int choice; /* user's choice */
char item; /* char entered by user */
instructions(); /* display menu */
printf("? ");
scanf( "%d", &choice );
/* loop while user doesn't choose 5 (quit) */
while ( choice != 5){
switch ( choice ) {
case 1:
printf( "Enter a character: ");
scanf( "\n%c", &item );
insertbegin( &startPtr, item ); /* add item to beginning of list */
break;
case 2:
printf( "Enter a character: ");
scanf( "\n%c", &item );
insertend( &startPtr, item ); /* add item to end of list */
break;
case 3:
/* if list is not empty */
if ( !isEmpty( startPtr ) ) {
printf( "Enter character to be deleted: ");
scanf( "\n%c", &item );
/*if character is found, remove it*/
if ( delete( &startPtr, item ) ) {/* delete item from list */
printf( "%c deleted.\n", item );
}
else {
printf("%c not found.\n\n", item );
}
}
else {
printf("List is empty.\n\n" );
}
break;
case 4:
printList( startPtr); /* print list */
break;
default:
printf( "Invalid Choice.\n\n");
instructions();
break;
} /* end switch */
printf("? ");
scanf( "%d", &choice );
} /* end while */
printf( "End of run.\n" );
return 0;
}
/* display program instructions to user */
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the beginning of the list.\n"
" 2 to insert an element into the end of the list.\n"
" 3 to delete an element from the list.\n"
" 4 to print the list.\n"
" 5 to quit.\n");
}
/***************************************************************************/
void insertbegin( ListNodePtr *sPtr, char value ) /* Insert a new value into the list in sorted order */
{
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;
currentPtr = currentPtr->nextPtr;
} /* 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 */
}
/***************************************************************************/
void insertend( ListNodePtr *sPtr, char value ) /* Insert a new value into the list in sorted order */
{
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;
currentPtr = currentPtr->nextPtr;
} /* 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 */
}
/***************************************************************************/
/* Delete a list element */
char delete( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;
/* delete first node */
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr =( *sPtr )->nextPtr;
free( tempPtr );
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;
currentPtr = currentPtr->nextPtr;
} /* end while */
/*delete node at currentPtr */
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
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;
}
/***************************************************************************/
/* Print the list */
void printList( ListNodePtr currentPtr )
{
/* if list is empty */
if ( currentPtr == NULL ) {
printf("List is empty.\n\n");
}
else {
printf( "The list is:\n" );
/* while not the end of the list */
while ( currentPtr != NULL ) {
printf( "%c ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "\n\n");
}
} /* end function printList */
thank you!