C Program – Assign negativeCntr with the number of negative values in the linked list

C Program - Assign negativeCntr with the number of negative values in the linked list, including the list head

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct IntNode_struct {
  4. int dataVal;
  5. struct IntNode_struct* nextNodePtr;
  6. } IntNode;
  7. // Constructor
  8. void IntNode_Create(IntNode* thisNode, int dataInit, IntNode* nextLoc) {
  9. thisNode->dataVal = dataInit;
  10. thisNode->nextNodePtr = nextLoc;
  11. }
  12. /* Insert newNode after node.
  13. Before: thisNode -- next
  14. After: thisNode -- newNode -- next
  15. */
  16. void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode) {
  17. IntNode* tmpNext = NULL;
  18. tmpNext = thisNode->nextNodePtr; // Remember next
  19. thisNode->nextNodePtr = newNode; // this -- new -- ?
  20. newNode->nextNodePtr = tmpNext; // this -- new -- next
  21. }
  22. // Grab location pointed by nextNodePtr
  23. IntNode* IntNode_GetNext(IntNode* thisNode) {
  24. return thisNode->nextNodePtr;
  25. }
  26. int IntNode_GetDataVal(IntNode* thisNode) {
  27. return thisNode->dataVal;
  28. }
  29. int main(void) {
  30. IntNode* headObj = NULL; // Create intNode objects
  31. IntNode* currObj = NULL;
  32. IntNode* lastObj = NULL;
  33. int i; // Loop index
  34. int negativeCntr;
  35. negativeCntr = 0;
  36. headObj = (IntNode*)malloc(sizeof(IntNode)); // Front of nodes list
  37. IntNode_Create(headObj, -1, NULL);
  38. lastObj = headObj;
  39. for (i = 0; i < 10; ++i) { // Append 10 rand nums
  40. currObj = (IntNode*)malloc(sizeof(IntNode));
  41. IntNode_Create(currObj, (rand() % 21) - 10, NULL);
  42. IntNode_InsertAfter(lastObj, currObj); // Append curr
  43. lastObj = currObj; // Curr is the new last item
  44. }
  45. currObj = headObj; // Print the list
  46. while (currObj != NULL) {
  47. printf("%d, ", IntNode_GetDataVal(currObj));
  48. currObj = IntNode_GetNext(currObj);
  49. }
  50. printf("\n");
  51. currObj = headObj; // Count number of negative numbers
  52. while (currObj != NULL) {
  53. /* Your solution goes here */
  54. currObj = IntNode_GetNext(currObj);
  55. }
  56. printf("Number of negatives: %d\n", negativeCntr);
  57. return 0;
  58. }

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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct IntNode_struct {
  4. int dataVal;
  5. struct IntNode_struct* nextNodePtr;
  6. } IntNode;
  7. // Constructor
  8. void IntNode_Create(IntNode* thisNode, int dataInit, IntNode* nextLoc) {
  9. thisNode->dataVal = dataInit;
  10. thisNode->nextNodePtr = nextLoc;
  11. }
  12. /* Insert newNode after node.
  13. Before: thisNode -- next
  14. After: thisNode -- newNode -- next
  15. */
  16. void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode) {
  17. IntNode* tmpNext = NULL;
  18. tmpNext = thisNode->nextNodePtr; // Remember next
  19. thisNode->nextNodePtr = newNode; // this -- new -- ?
  20. newNode->nextNodePtr = tmpNext; // this -- new -- next
  21. }
  22. // Grab location pointed by nextNodePtr
  23. IntNode* IntNode_GetNext(IntNode* thisNode) {
  24. return thisNode->nextNodePtr;
  25. }
  26. int IntNode_GetDataVal(IntNode* thisNode) {
  27. return thisNode->dataVal;
  28. }
  29. int main(void)
  30. {
  31. IntNode* headObj = NULL; // Create intNode objects
  32. IntNode* currObj = NULL;
  33. IntNode* lastObj = NULL;
  34. int i; // Loop index
  35. int negativeCntr;
  36. negativeCntr = 0;
  37. headObj = (IntNode*)malloc(sizeof(IntNode)); // Front of nodes list
  38. IntNode_Create(headObj, -1, NULL);
  39. lastObj = headObj;
  40. for (i = 0; i < 10; ++i) { // Append 10 rand nums
  41. currObj = (IntNode*)malloc(sizeof(IntNode));
  42. IntNode_Create(currObj, (rand() % 21) - 10, NULL);
  43. IntNode_InsertAfter(lastObj, currObj); // Append curr
  44. lastObj = currObj; // Curr is the new last item
  45. }
  46. currObj = headObj; // Print the list
  47. while (currObj != NULL) {
  48. printf("%d, ", IntNode_GetDataVal(currObj));
  49. currObj = IntNode_GetNext(currObj);
  50. }
  51. printf("\n");
  52. currObj = headObj; // Count number of negative numbers
  53. while (currObj != NULL) {
  54. if (currObj->dataVal < 0)
  55. negativeCntr++;
  56. currObj = IntNode_GetNext(currObj);
  57. }
  58. printf("Number of negatives: %d\n", negativeCntr);
  59. return 0;
  60. }

Leave a Reply

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

Related Posts