assign negativecntr with the number of negative values in the linked list.


questioned by maham237 @ in Computers & Technology viewed by 587 People


C program, Assign negativeCntr with the number of negative values in the linked list, including the list head.#include
#include
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;
}

Answered by maham237 @



Answer:

if(currObj->GetDataVal() < 0)

{

negativeCntr++;

}

Explanation:

This is the solution in C++ try in C see if it works (I'm more knowledgeable on C++)

You have to use the function GetDataVal() in order to receive the value attached to the currObj you are looking for. The while loop used above it goes through each node to see if it's not null, or in other words, if the memory location has a value. If the location has a value then the value itself is checked and if the value is less than 0, negativeCntr = negativeCntr + 1;


Do you know the better answer?

Latest questions in Computers & Technology

what is the meaning of absolute value of a number

Questioned by administrator @ in Mathematics viewed by 42 persons

What is absolute value?
View 1 answer

list 5 other negative effects not diseases of using tobacco

Questioned by administrator @ in Health viewed by 54 persons

List 5 other negative effects (not diseases) of using tobacco.
View 1 answer

the average of a list of 4 numbers is 90.0

Questioned by administrator @ in Mathematics viewed by 59 persons

The average of a list of 4 numbers is 90.0. A new list of 4 numbers has the same first 3 numbers as the original list, but the fourth number …
View 1 answer

can you take a cube root of a negative number

Questioned by administrator @ in Mathematics viewed by 65 persons

3. Why do cube roots of negative numbers exist in the set of real numbers, but square roots of negative numbers do not? A. The square root of a number, …
View 1 answer

list five rational numbers between 2 and 0

Questioned by administrator @ in Math viewed by 77 persons

List any five rational numbers between -2 and 0
View 1 answer

rational number that is equal to its negative

Questioned by administrator @ in Math viewed by 62 persons

The rational numbers that is equal to its negative is
View 1 answer

list five rational numbers between and 2 3

Questioned by administrator @ in Math viewed by 59 persons

List five rational numbers between -2/3 and -5/4​
View 1 answer

when included with list suppress record number display

Questioned by administrator @ in Computer Science viewed by 827 persons

When included with list suppress record number display. जब ITST के साथ शामिल किया जाता है रिकॉर्ड संख्या काप्रदर्शन दबा देते हैंSelect one:१. norecb. RecnoC.Of …
View 1 answer

what is the value of a digit in a number

Questioned by administrator @ in Mathematics viewed by 47 persons

What is the value of the digit 5 in the number 75?
View 1 answer

multiplicative inverse of a negative rational number is

Questioned by administrator @ in Math viewed by 40 persons

Multiplicative inverse of a negative rational number is (a) a positive rational number. (b) a negative rational number. (c) 0 (d) 1
View 1 answer

list five rational numbers between 2 and 1

Questioned by administrator @ in Math viewed by 50 persons

List five rational numbers between -2 and -1
View 1 answer

list of prime numbers from 1 to 100

Questioned by administrator @ in Math viewed by 52 persons

A List all the prime numbers from 1 to 100.​
View 1 answer

the reciprocal of a negative rational number is

Questioned by administrator @ in Math viewed by 44 persons

The reciprocal of a negative rational number _____
View 1 answer

can the square of an integer be a negative number

Questioned by administrator @ in Mathematics viewed by 45 persons

Can the square of an integer be a negative number
View 1 answer

which tag is used to create a numbered list

Questioned by administrator @ in Computer Science viewed by 42 persons

In html, which of the tag is used to creates a number list?
View 1 answer

Most viewed questions in Computers & Technology


3.4 6 t shirt shop codehs answers

Questioned by maham237 @ in Computers & Technology viewed by 3747 persons


security infrastructure design document

Questioned by administrator @ in Computers & Technology viewed by 3028 persons



real world examples of problems whose solutions do scale well

Questioned by maham237 @ in Computers & Technology viewed by 2965 persons


codehs python answers

Questioned by maham237 @ in Computers & Technology viewed by 2528 persons


codehs python answer key

Questioned by maham237 @ in Computers & Technology viewed by 1972 persons



edhesive assignment 3: chatbot 1

Questioned by maham237 @ in Computers & Technology viewed by 1192 persons


open the excel workbook revenue xls from the default directory

Questioned by maham237 @ in Computers & Technology viewed by 951 persons


banana fee fi fo fana song

Questioned by maham237 @ in Computers & Technology viewed by 882 persons



phone number breakdown zybooks

Questioned by maham237 @ in Computers & Technology viewed by 624 persons



assign negativecntr with the number of negative values in the linked list.

Questioned by maham237 @ in Computers & Technology viewed by 587 persons


in cell j4 calculate the actual increase

Questioned by maham237 @ in Computers & Technology viewed by 546 persons


kay fox and the magic sword cheats

Questioned by administrator @ in Computers & Technology viewed by 520 persons