C program, Assign negativeCntr with the number of negative values in the linked list, including the list head.

C program, Assign negativeCntr with the number of negative values in the linked list, including the list head.

Answer

To get the count the negative values present in the linked list, the following steps:

In the main() function, at the last while loop

Use an if condition to check whether the value in the linked list is less than zero. If the value of the linked list is less than zero, then increment the negativeCntr by one. The statements that need to be added at the /* Your solution goes here */ are:

if (currObj->dataVal < 0)

negativeCntr++;

Here is the program code with added statement highlighted in the bold letters is given as follows:

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

typedef struct IntNode_struct {
   int dataVal;
   struct IntNode_struct* nextNodePtr;
} IntNode;

// Constructor
void IntNode_Create(IntNode* thisNode, int dataInit, IntNode* nextLoc) {
   thisNode->dataVal = dataInit;
   thisNode->nextNodePtr = nextLoc;
}

/* Insert newNode after node.
Before: thisNode -- next
After: thisNode -- newNode -- next
*/
void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode) {
   IntNode* tmpNext = NULL;

   tmpNext = thisNode->nextNodePtr; // Remember next
   thisNode->nextNodePtr = newNode; // this -- new -- ?
   newNode->nextNodePtr = tmpNext; // this -- new -- next
}

// Grab location pointed by nextNodePtr
IntNode* IntNode_GetNext(IntNode* thisNode) {
   return thisNode->nextNodePtr;
}

int IntNode_GetDataVal(IntNode* thisNode) {
   return thisNode->dataVal;
}

int main(void)
{
   IntNode* headObj = NULL; // Create intNode objects
   IntNode* currObj = NULL;
   IntNode* lastObj = NULL;
   int i; // Loop index
   int negativeCntr;

   negativeCntr = 0;

   headObj = (IntNode*)malloc(sizeof(IntNode)); // Front of nodes list
   IntNode_Create(headObj, -1, NULL);
   lastObj = headObj;

   for (i = 0; i < 10; ++i) { // Append 10 rand nums
       currObj = (IntNode*)malloc(sizeof(IntNode));
       IntNode_Create(currObj, (rand() % 21) - 10, NULL);
       IntNode_InsertAfter(lastObj, currObj); // Append curr
       lastObj = currObj; // Curr is the new last item
   }

   currObj = headObj; // Print the list
   while (currObj != NULL) {
       printf("%d, ", IntNode_GetDataVal(currObj));
       currObj = IntNode_GetNext(currObj);
   }
   printf("\n");

   currObj = headObj; // Count number of negative numbers
   while (currObj != NULL) {
       if (currObj->dataVal < 0)
           negativeCntr++;
       currObj = IntNode_GetNext(currObj);
   }
   printf("Number of negatives: %d\n", negativeCntr);  
   return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts