I have a code for a doubly linked list, and I need to print it in reverse. I can't seem to figure out what I'm doing wrong, or maybe I just don't fully understand what I'm doing.
The part that I'm questioning is in the printList function. The while statement seems to never be true, so nothing ever gets printed. I tried adding the line currentPtr = currentPtr->prev
, but my program crashes when I do. I'm guessing that it has something to do with currentPtr being NULL before I attempt to print the list backwards, but I have no idea how to fix it. How do I get the program to step backwards in the list and print it?
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node *next,*prev;
} *head=NULL, *tail=NULL, *newnode ,*Traverse;
typedef struct node ListNode;
typedef ListNode *ListNodePtr;
typedef struct Node *NodePtr;
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( void )
{
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 );
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 list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = malloc( sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->next = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if ( previousPtr == NULL ) {
newPtr->next = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->next = newPtr;
newPtr->next = currentPtr;
}
}
else {
printf( "%c not inserted. No memory available.\n", value );
}
}
char delete( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->next;
free( tempPtr );
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->next;
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
free( tempPtr );
return value;
}
}
return '\0';
}
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
ListNodePtr startPtr = NULL;
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
}
else {
{printf( "The list is:\n" );
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->next;}
printf( "NULL\n" );
{ printf( "\nThe list in reverse is:\n" );
printf( "NULL" );
while ( currentPtr != NULL ) {
printf( " <-- %c", currentPtr->data );
currentPtr = currentPtr->prev;
}
printf("\n\n");
}
}
}
}