Linked list negative values counting.

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to counting negative values in a linked list implemented in C++. Participants are examining the logic of the counting mechanism within the provided code, focusing on how to correctly tally the number of negative integers stored in the linked list nodes.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the variable negativeCntr and seeks guidance on what might be wrong with their implementation.
  • Another participant questions the absence of actual counting logic in the code, implying that the current implementation does not perform the intended task of counting negative numbers.
  • A third participant reiterates the concern about counting, suggesting that the current assignment to negativeCntr does not accumulate counts but rather assigns a value from the linked list.
  • One participant points out that the code is missing critical logic for checking if the current node's value is negative before counting it, indicating a need for a conditional check.
  • Further, they suggest that understanding the relationship between the output and the linked list contents could help clarify the issue.
  • There is also a prompt to consider what the GetDataVal function returns and what the participant expects it to return, hinting at potential misunderstandings about the function's purpose.

Areas of Agreement / Disagreement

Participants generally agree that the current implementation does not correctly count negative values, but there is no consensus on the exact solution or the best approach to rectify the issue. Multiple viewpoints on how to implement the counting logic are present.

Contextual Notes

Participants have not resolved the specific logic needed to count negative values, and there are unresolved questions about the expected behavior of the GetDataVal function and its implications for the counting process.

Teh
Messages
47
Reaction score
0
I am having trouble with this program with negativeCntr . Any one explain what i am doing wrong and guide me please.

Code:
[CODE]#include <iostream>
#include <cstdlib>
using namespace std;

class IntNode {
public:
   IntNode(int dataInit = 0, IntNode* nextLoc = 0);
   void InsertAfter(IntNode* nodePtr);
   IntNode* GetNext();
   int GetDataVal();
private:
   int dataVal;
   IntNode* nextNodePtr;
};

// Constructor
IntNode::IntNode(int dataInit, IntNode* nextLoc) {
   this->dataVal = dataInit;
   this->nextNodePtr = nextLoc;
   return;
}

/* Insert node after this node.
 * Before: this -- next
 * After:  this -- node -- next
 */
void IntNode::InsertAfter(IntNode* nodeLoc) {
   IntNode* tmpNext = 0;

   tmpNext = this->nextNodePtr;    // Remember next
   this->nextNodePtr = nodeLoc;    // this -- node -- ?
   nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
   return;
}

// Grab location pointed by nextNodePtr
IntNode* IntNode::GetNext() {
   return this->nextNodePtr;
}

int IntNode::GetDataVal() {
   return this->dataVal;
}

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

   headObj = new IntNode(-1);        // Front of nodes list
   lastObj = headObj;

   for (i = 0; i < 10; ++i) {        // Append 10 rand nums
      currObj = new IntNode((rand() % 21) - 10);
      lastObj->InsertAfter(currObj); // Append curr
      lastObj = currObj;             // Curr is the new last item
   }

   currObj = headObj;                // Print the list
   while (currObj != 0) {
      cout << currObj->GetDataVal() << ", ";
      currObj = currObj->GetNext();
   }
   cout << endl;

   currObj = headObj;                // Count number of negative numbers
   while (currObj != 0) {

      /* Your solution goes here  */
   
      negativeCntr = currObj->GetDataVal();
      
      currObj = currObj->GetNext();
   }
   cout << "Number of negatives: " << negativeCntr << endl;

   return 0;
Testing with 6 negatives
Expected output: Number of negatives: 6
Your output: Number of negatives: -8
 
Technology news on Phys.org
If you're supposed to be counting the number of negatives, where's your code that is doing the counting?
 
squidsk said:
If you're supposed to be counting the number of negatives, where's your code that is doing the counting?

This is my code for counting the number of negatives but I am not sure if it it right.

Code:
negativeCntr = currObj->GetDataVal();
 
If that code is supposed to count (i.e. do addition) I think you're missing something critical on the line. Also if you're supposed to count only the negative numbers you need to figure out whether the number at that spot in the linked list is negative. Two other questions for you to ponder. First how does your output relate to the contents of the list (make changes to the list to get a better sense of this). Two what does the function GetDataVal return and what are you currently expecting it to return.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K