I am creating a doubly linked list and printing a backwards function. Everything is working great in my code, but when the function is printed in reverse, I need it to say NULL at the end, as it does going forward. When I put puts("NULL") (line 193) it splits up the list and puts NULL after each one. And if I remove that from my code, it works fine, just without NULL printed at the end. I just need NULL to print at the end and I am unsure of how to do this. Thank you!!!

#include <stdio.h>
#include <stdlib.h>


struct listNode {
    char data;
    struct listNode *nextPtr;
    struct listNode *prevPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

// prototypes
void insert( ListNodePtr *sPtr, char value );
char delete( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void printBackwards( ListNodePtr startPtr );

void instructions( void );

int main( void )
{
    ListNodePtr startPtr = NULL; // initially there are no nodes
    int choice; // user's choice
    char item; // 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( "\n%c", &item );
                insert( &startPtr, item ); // insert item in list
                printList( startPtr );
                printBackwards( startPtr );
                break;
            case 2:
                // 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 ) ) { // remove item
                        printf( "%c deleted.\n", item );
                        printList( startPtr );
                        printBackwards( startPtr );
                    } // end if
                    else {
                        printf( "%c not found.\n\n", item );
                    } // end else
                } // end if
                else {
                    printf( "List is empty.\n\n" );
                } // end else
                break;
            default:
                printf( "Invalid choice.\n\n" );
                instructions();
                break;
        } // end switch
        printf( "\n\n? " );
        scanf( "%d", &choice );
    } // end while
    return 0; // indicates successful termination
} // end main


// display program instructions to user
void instructions( void )
{
    printf( "Enter your choice:\n"
           " 1 to insert an element into the list.\n"
           " 2 to delete an element from the list.\n"
           " 3 to end.\n");
} // end function instructions

// Insert a new value into the list in sorted order
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  


// Delete a list element  
char delete( ListNodePtr *sPtr, char value )
{
    ListNodePtr previousPtr; // pointer to previous node in list  
    ListNodePtr currentPtr; // pointer to current node in list  
    ListNodePtr tempPtr; // 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.\n\n" );
    } // end if
    else {
        printf( "\nThe 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
} // end function printList




void printBackwards(ListNodePtr currentPtr)
{
    // if list is empty
    if(currentPtr==NULL) {
        puts("List is empty.\n");
        return;
    } // end if
    else if ( currentPtr->nextPtr != NULL ) {
        printBackwards(currentPtr->nextPtr);

    } // end else if
    else
        puts("\nThe list in reverse is:");
        printf("%c --> ", currentPtr->data);
    puts("NULL");

} // end function printBackwards

Your else clause is missing braces. That only works when the body of the clause has one statement, yet here you have (presumably) three.

But when I do put braces, it doesn't work either:

void printBackwards(ListNodePtr currentPtr)
{
    // if list is empty
    if(currentPtr==NULL) {
        puts("List is empty.\n");
        return;
    } // end if
    else if ( currentPtr->nextPtr != NULL ) {
        printBackwards(currentPtr->nextPtr);

    } // end else if
    else {
        puts("\nThe list in reverse is:");
    printf("%c --> ", currentPtr->data);
    puts("NULL");
    }
} // end function printBackwards

The printBackwards function only prints the last character entered:

Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: a

The list is:
a --> NULL

The list in reverse is:
a --> NULL

? 1
Enter a character: b

The list is:
a --> b --> NULL

The list in reverse is:
b --> NULL

? 1
Enter a character: c

The list is:
a --> b --> c --> NULL

The list in reverse is:
c --> NULL

?

The reason it isn't printing the rest of the list is because... you don't have it printing the rest of the list. Try the following:

void printBackwards(ListNodePtr currentPtr)
{
    // if list is empty
    if(currentPtr==NULL) {
        puts("List is empty.\n");
        return;
    } // end if
    else if ( currentPtr->nextPtr != NULL ) {
        printBackwards(currentPtr->nextPtr);
        printf("%c --> ", currentPtr->data);   // printing the intermediate data here
    } // end else if
    else {
        puts("\nThe list in reverse is:");
        printf("%c --> ", currentPtr->data);
        puts("NULL");
    }
} // end function printBackwards

In addition, I think you'll find that there is a problem in the insert() function; you never set the newPtr->prevPtr value.

    else { // insert new node between previousPtr and currentPtr  
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
        //  should have "newPtr->prevPtr = previousPtr;" here
    } // end else

Thank you! That has helped a little, but for some reason it is only printing NULL after the last character (which is actually the first now).

Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: a

The list is:
a --> NULL

The list in reverse is:
a --> NULL

? 1
Enter a character: b

The list is:
a --> b --> NULL

The list in reverse is:
b --> NULL
a -->

? 1
Enter a character: c

The list is:
a --> b --> c --> NULL

The list in reverse is:
c --> NULL
b --> a -->

?

void printBackwards(ListNodePtr currentPtr)
{
    // if list is empty
    if(currentPtr==NULL) {
        puts("List is empty.\n");
        return;
    } // end if
    else if ( currentPtr->nextPtr != NULL ) {
        printBackwards(currentPtr->nextPtr);
        printf("%c --> ", currentPtr->data);
    } // end else if
    else {
        puts("\nThe list in reverse is:");
        printf("%c --> ", currentPtr->data);
        puts("NULL");
    }
} // end function printBackwards

I just don't understand why it is printing this way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.