Question

In: Computer Science

Code in C++ Assign negativeCntr with the number of negative values in the linked list. #include...

Code in C++

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

#include <iostream>
#include <cstdlib>
using namespace std;

class IntNode {
public:
IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);
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;
}

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

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

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

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

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

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 != nullptr) {
cout << currObj->GetDataVal() << ", ";
currObj = currObj->GetNext();
}
cout << endl;

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

/* Your solution goes here */

currObj = currObj->GetNext();
}
cout << "Number of negatives: " << negativeCntr << endl;

return 0;
}

Solutions

Expert Solution

LinkedList.cpp

#include <iostream>
#include <cstdlib>
#include<stdlib.h>
using namespace std;

class IntNode {
public:
IntNode(int dataInit = 0, IntNode* nextLoc = NULL);
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;
}

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

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

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

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

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

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 != NULL) {
cout << currObj->GetDataVal() << ", ";
currObj = currObj->GetNext();
}
cout << endl;

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

/* Your solution goes here */
if(currObj->GetDataVal()<0)
negativeCntr++; // count negetive numbers in list
currObj = currObj->GetNext();
}
cout << "Number of negatives: " << negativeCntr << endl;

return 0;
}

Output


Related Solutions

write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND THANKS!!This assignment covers recursion and linked list...
PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND THANKS!!This assignment covers recursion and linked list which include the following tasks: 2. a. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then print the list from the first node to the last. Finally, free all memories of the linked list. b. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT