Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

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

Computer Science Jan 05, 2021

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

#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) {

/* Your solution goes here */

currObj = IntNode_GetNext(currObj);
}
printf("Number of negatives: %d\n", negativeCntr);

return 0;
}

Expert Solution

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:

Program code screen shot:

Program code to copy:

#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;
}

please use this google drive link to download the answer file.

https://drive.google.com/file/d/1fSFnzbbUY7cfg7OFayXqeRVYbOJtEKuo/view?usp=sharing

note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-googledrive-link
 

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment