Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Most of the program is here already (at the bottom), I am just trying to make it

ID: 3622473 • Letter: M

Question

Most of the program is here already (at the bottom), I am just trying to make it a doubly link list and also use a print backwards function. The program should, for example, display the following at startup:

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

Then if the user entered "1" right after the "?" then it would display:

     Enter a character:

Then if the user entered "a" it would display:

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

Then if the user entered 1 to insert an element it would show the following again:

     Enter a character: z

Then again, display:

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

Below is the code that I have so far. (Sorry the copy and paste formatted it a little funny) Thank you in advance for any and all help.

_______________________________________________________________________

#include
#include

/* self-referential structure */

struct queueNode {  
char data; /* define data as a char */
struct queueNode *nextPtr; /* queueNode pointer */

}; /* end structure queueNode */

typedef struct queueNode QueueNode;

typedef QueueNode *QueueNodePtr;

/* function prototypes */

void printQueue( QueueNodePtr currentPtr );

int isEmpty( QueueNodePtr headPtr );

char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr );

void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,

   char value );

void instructions( void );

/* function main begins program execution */

int main( void )

{

   QueueNodePtr headPtr = NULL; /* initialize headPtr */

   QueueNodePtr tailPtr = NULL; /* initialize tailPtr */

   int choice; /* user's menu choice */

   char item; /* char input by user */

   instructions(); /* display the menu */

   printf( "? " );

   scanf( "%d", &choice );

   /* while user does not enter 3 */

   while ( choice != 3 ) {

      switch( choice ) {

         /* enqueue value */

         case 1:

            printf( "Enter a character: " );

            scanf( " %c", &item );

            enqueue( &headPtr, &tailPtr, item );

            printQueue( headPtr );

            break;

         /* dequeue value */

         case 2:

            /* if queue is not empty */

            if ( !isEmpty( headPtr ) ) {

               item = dequeue( &headPtr, &tailPtr );

               printf( "%c has been dequeued. ", item );

            } /* end if */

            printQueue( headPtr );

            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 add an item to the queue "

           "   2 to remove an item from the queue "

           "   3 to end " );

} /* end function instructions */

/* insert a node a queue tail */

void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,

   char value )

{

   QueueNodePtr newPtr; /* pointer to new node */

   newPtr = malloc( sizeof( QueueNode ) );

   if ( newPtr != NULL ) { /* is space available */

      newPtr->data = value;

      newPtr->nextPtr = NULL;

      /* if empty, insert node at head */

      if ( isEmpty( *headPtr ) ) {

         *headPtr = newPtr;

      } /* end if */

      else {

         ( *tailPtr )->nextPtr = newPtr;

      } /* end else */

      *tailPtr = newPtr;

   } /* end if */

   else {

      printf( "%c not inserted. No memory available. ", value );

   } /* end else */

} /* end function enqueue */

/* remove node from queue head */

char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr )

{

   char value; /* node value */

   QueueNodePtr tempPtr; /* temporary node pointer */

   value = ( *headPtr )->data;

   tempPtr = *headPtr;

   *headPtr = ( *headPtr )->nextPtr;

   /* if queue is empty */

   if ( *headPtr == NULL ) {

      *tailPtr = NULL;

   } /* end if */

   free( tempPtr );

   return value;

} /* end function dequeue */

/* Return 1 if the list is empty, 0 otherwise */

int isEmpty( QueueNodePtr headPtr )

{

   return headPtr == NULL;

} /* end function isEmpty */

/* Print the queue */

void printQueue( QueueNodePtr currentPtr )

{

   /* if queue is empty */

   if ( currentPtr == NULL ) {

      printf( "Queue is empty. " );

   } /* end if */

   else {

      printf( "The queue is: " );

      /* while not end of queue */

      while ( currentPtr != NULL ) {

         printf( "%c --> ", currentPtr->data );

         currentPtr = currentPtr->nextPtr;

      } /* end while */

      printf( "NULL " );

   } /* end else */

} /* end function printQueue */

Explanation / Answer

You could use a singly linked list to print the list backwards using recursion like void printReverse(struct node *ptr){ if(ptr->next!=NULL){ printReverse(ptr->nextPtr); } printf("%c",ptr->data); } in doubly linked list just traverse the list and using backward pointer traverse again and print the elements. But for insertion and deletion you have to manipulate the pointers accordingly.