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

I am writing a program in C and have the code written but this error has me stum

ID: 3759143 • Letter: I

Question

I am writing a program in C and have the code written but this error has me stumped. The error is:

newPtr = malloc(sizeof(ListNode)); <---a value of type "void" cannot be assigned to an entity type of "ListNodePTR", the = has the error line under it. It is marked in bold in the code so you can take a look at it.

Here is the assignment:

Modify the linked list example in the book so that it is a doubly linked list. Prove that your program works properly by implementing a print backwards function.

Here is the output I need:

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: z

The list is:

a --> z --> NULL

The list in reverse is:

z --> a --> NULL

? 1

Enter a character: n

The list is:

a --> n --> z --> NULL

The list in reverse is:

z --> n --> a --> NULL

? 1 Enter a character: d The list is: a --> d --> n --> z --> NULL The list in reverse is: z --> n --> d --> a --> NULL ? 2 Enter character to be deleted: x x not found. ? 2 Enter character to be deleted: n n deleted. The list is: a --> d --> z --> NULL The list in reverse is: z --> d --> a --> NULL ? 2 Enter character to be deleted: a a deleted. The list is: d --> z --> NULL The list in reverse is: z --> d --> NULL ? 2 Enter character to be deleted: z z deleted. The list is: d --> NULL The list in reverse is: d --> NULL ? 2 Enter character to be deleted: d d deleted. List is empty. ? 1 Enter a character: s The list is: s --> NULL The list in reverse is: s --> NULL ? 1 Enter a character: t The list is: s --> t --> NULL The list in reverse is: t --> s --> NULL ? 3 End of run. Press any key to continue . . .

Here is my code:

#include <stdio.h>
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>

/* self-referential structure */

struct listNode {

   char data; /* each listNode contains a character */

   struct listNode *nextPtr; /* pointer to next node*/

   struct listNode *prevPtr; /* pointer to previous 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 printListBackwards(ListNodePtr currentPtr);

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(" %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; /* 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 */ <----- Error

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

       newPtr->data = value; /* place value in node */

       newPtr->nextPtr = NULL; /* node does not link to another node */

       newPtr->prevPtr = NULL; /* node does not link to previous 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;

           newPtr->prevPtr = previousPtr;

           if (currentPtr != NULL)

               currentPtr->prevPtr = newPtr;

       } /* 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; /* 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 */

       if ((*sPtr) != NULL)

           (*sPtr)->prevPtr = NULL; /* nullify previous 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;

           if (currentPtr->nextPtr != NULL)

               currentPtr->nextPtr->prevPtr = previousPtr;

           free(tempPtr);

           return value;

       } /* end if */

   } /* end else */

   return '';

} /* 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 then call printListBackwards*/

void printList(ListNodePtr currentPtr)

{

   ListNodePtr previousPtr; /* pointer to previous node in list */

                           /* 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);

           previousPtr = currentPtr;

           currentPtr = currentPtr->nextPtr;

       } /* end while */

       printf("NULL ");

       printListBackwards(previousPtr);

   } /* end else */

} /* end function printList */

/* Print the list Backwards*/

void printListBackwards(ListNodePtr currentPtr)

{

   printf("The list in reverse is: ");

   /* while not the end of the list */

   while (currentPtr != NULL) {

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

       currentPtr = currentPtr->prevPtr;

   } /* end while */

   printf("NULL ");

} /* end function printListBackwards */

Error is marked with bold, but to specify, we have:

newPtr = malloc(sizeof(ListNode)); <---a value of type "void" cannot be assigned to an entity type of "ListNodePTR".

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

/* self-referential structure */

    struct listNode {  

       char data;

       struct listNode *nextPtr;

       struct listNode *prevPtr;

    };

typedef struct listNode listNode;

typedef listNode *listNodePtr;

/* function prototypes */

    void printQueue( listNodePtr currentPtr );

    int isEmpty( listNodePtr headPtr );

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

    void enqueue( listNodePtr *headPtr, listNodePtr *tailPtr,

       char value );

    void instructions( void );

/* function main begins program execution */

int main( void )

{

   listNodePtr headPtr = NULL; /* initialize headPtr */

   listNodePtr 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 );

            reverse( 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( listNodePtr *headPtr, listNodePtr *tailPtr,

   char value )

{

   listNodePtr newPtr; /* pointer to new node */

   newPtr = malloc( sizeof( listNode ) );

   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( listNodePtr *headPtr, listNodePtr *tailPtr )

{

   char value; /* node value */

   listNodePtr 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( listNodePtr headPtr )

{

   return headPtr == NULL;

} /* end function isEmpty */

/* Print the queue */

void printQueue( listNodePtr 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 */

void reverse( listNodePtr currentPtr)

{

   struct listNode *temp = NULL;  

     /* if queue is empty */

   if ( currentPtr == NULL ) {

      printf( "Queue is empty. " );

   } /* end if */

   else {

      printf( "The queue backwards is: " );

     /* swap next and prevPtr for all listNodes of

       doubly linked list */

     while (currentPtr != NULL)

     {

        temp = currentPtr->prevPtr;

        currentPtr->prevPtr = currentPtr->nextPtr;

        currentPtr->nextPtr = temp;

        currentPtr = currentPtr->prevPtr;

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

     }       

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote