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

I need some help making the modifications on this assignment in a C Programming

ID: 3567200 • Letter: I

Question

I need some help making the modifications on this assignment in a C Programming Class.

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.  

"Example Program Run":

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 the code from the book which you can cut and paste:

/* Fig. 12.3: fig12_03.c

   Operating and maintaining a list */

#include

#include

/* self-referential structure */

struct listNode {           

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

   struct listNode *nextPtr; /* pointer to next 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 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 */

   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. ", 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 '';

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

   } /* end if */

   else {

      printf( "The list is: " );

      /* while not the end of the list */

      while ( currentPtr != NULL ) {

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

         currentPtr = currentPtr->nextPtr;  

      } /* end while */

      printf( "NULL " );

   } /* end else */

} /* end function printList *

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

// A linked list node

struct node

{

int data;

struct node *next;

struct node *prev;

};

/* Given a reference (pointer to pointer) to the head of a list

and an int, inserts a new node on the front of the list. */

void push(struct node** head_ref, int new_data)

{

/* 1. allocate node */

struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data */

new_node->data = new_data;

/* 3. Make next of new node as head and previous as NULL */

new_node->next = (*head_ref);

new_node->prev = NULL;

/* 4. change prev of head node to new node */

if((*head_ref) != NULL)

(*head_ref)->prev = new_node ;

/* 5. move the head to point to the new node */

(*head_ref) = new_node;

}

/* Given a node as prev_node, insert a new node after the given node */

void insertAfter(struct node* prev_node, int new_data)

{

/*1. check if the given prev_node is NULL */

if (prev_node == NULL)

{

printf("the given previous node cannot be NULL");

return;

}

/* 2. allocate new node */

struct node* new_node =(struct node*) malloc(sizeof(struct node));

/* 3. put in the data */

new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */

new_node->next = prev_node->next;

/* 5. Make the next of prev_node as new_node */

prev_node->next = new_node;

/* 6. Make prev_node as previous of new_node */

new_node->prev = prev_node;

/* 7. Change previous of new_node's next node */

if (new_node->next != NULL)

new_node->next->prev = new_node;

}

/* Given a reference (pointer to pointer) to the head

of a DLL and an int, appends a new node at the end */

void append(struct node** head_ref, int new_data)

{

/* 1. allocate node */

struct node* new_node = (struct node*) malloc(sizeof(struct node));

struct node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */

new_node->data = new_data;

/* 3. This new node is going to be the last node, so

make next of it as NULL*/

new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new

node as head */

if (*head_ref == NULL)

{

new_node->prev = NULL;

*head_ref = new_node;

return;

}

/* 5. Else traverse till the last node */

while (last->next != NULL)

last = last->next;

/* 6. Change the next of last node */

last->next = new_node;

/* 7. Make last node as previous of new node */

new_node->prev = last;

return;

}

// This function prints contents of linked list starting from the given node

void printReverseList(struct node *node)

{

struct node *last;

while (node != NULL)

{

last = node;

node = node->next;

}

printf(" Traversal in reverse direction ");

while (last != NULL)

{

printf(" %d ", last->data);

last = last->prev;

}

}

/* Drier program to test above functions*/

int main()

{

/* Start with the empty list */

struct node* head = NULL;

// Insert 6. So linked list becomes 6->NULL

append(&head, 6);

// Insert 7 at the beginning. So linked list becomes 7->6->NULL

push(&head, 7);

// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL

push(&head, 1);

// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL

append(&head, 4);

// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL

insertAfter(head->next, 8);

  

printReverseList(head);

getchar();

return 0;

}