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


Asked by maham237 @ in Computers And Technology viewed by 906 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;


Similar Questions

Why is the absolute value of a number never negative

Asked by maham237 @ in Mathematics viewed by 282 persons

What is the meaning of absolute value of a number

Asked by admin @ in Mathematics viewed by 295 persons

What is absolute value?

List 5 other negative effects not diseases of using tobacco

Asked by admin @ in Health viewed by 271 persons

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

Can you take the square root of a negative number

Asked by admin @ in Mathematics viewed by 326 persons

Why can't we take a square root of a negative number?

List five rational numbers between 2 and 0

Asked by admin @ in Math viewed by 305 persons

List any five rational numbers between -2 and 0

Rational number that is equal to its negative

Asked by admin @ in Math viewed by 317 persons

The rational numbers that is equal to its negative is

List five rational numbers between and 2 3

Asked by admin @ in Math viewed by 330 persons

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

When included with list suppress record number display

Asked by admin @ in Computer Science viewed by 1145 persons

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

Multiplicative inverse of a negative rational number is

Asked by admin @ in Math viewed by 296 persons

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

List five rational numbers between 2 and 1

Asked by admin @ in Math viewed by 338 persons

List five rational numbers between -2 and -1

List of prime numbers from 1 to 100

Asked by admin @ in Math viewed by 332 persons

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

The reciprocal of a negative rational number is

Asked by admin @ in Math viewed by 301 persons

The reciprocal of a negative rational number _____

Which tag is used to create a numbered list

Asked by admin @ in Computer Science viewed by 340 persons

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

The product of two negative rational number is always

Asked by admin @ in Math viewed by 322 persons

The product of two negative rational numbers is always.......

List all the prime numbers from 1 to 100

Asked by admin @ in Math viewed by 370 persons

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

Most viewed questions in Computers And Technology


Codehs python answer key

Asked by maham237 @ in Computers And Technology viewed by 2062 persons


3.4 6 t shirt shop codehs answers

Asked by maham237 @ in Computers And Technology viewed by 1829 persons



Security infrastructure design document

Asked by wiki @ in Computers And Technology viewed by 1781 persons


Codehs python answers

Asked by maham237 @ in Computers And Technology viewed by 1146 persons


In the business world people are often measured by their

Asked by maham237 @ in Computers And Technology viewed by 1079 persons




Banana fee fi fo fana song

Asked by maham237 @ in Computers And Technology viewed by 784 persons


List 10 www standards that the w3c develops and maintains

Asked by maham237 @ in Computers And Technology viewed by 759 persons


Open the excel workbook revenue xls from the default directory

Asked by maham237 @ in Computers And Technology viewed by 725 persons



In order to protect your computer from the newest viruses

Asked by maham237 @ in Computers And Technology viewed by 715 persons