Question: [I need help fixing this C++ program assignment. Apparently, my add, remove, and find methods aren't working and failed the unit tests when submitting. The
[I need help fixing this C++ program assignment. Apparently, my add, remove, and find methods aren't working and failed the unit tests when submitting. The count and len methods are working. My results after submitting:
Index of 3 in list: 0
count of 9:
2 length: 4)
Directions: The objective of this assignment is to implement several basic methods for a singly linked list. I have provided declaration files for the Node and LinkedList classes. ( see node.h and linkedlist.h ).(You can only modify the main.cpp and linkedlist.cpp files.)
Implement the following LinkedList methods in linkedlist.cpp:
void add ( int N ) Add N to the front of the linked list. bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false. int find ( int N ) Find the first instance of N in the list and return its index. Return -1 if N was not found. int count ( int N ) Return a count of the instances of N in the list. int at ( int N ) Return the value stored in the node at index N. int len() Return the current length of the list.
You can use main.cpp for your own internal testing. Make sure you do not alter the order of elements in the list.
//node.h
class Node { public: Node * next = nullptr; int value = 0; };
//linkedlist.h
#include "node.h" using namespace std;
class LinkedList { Node * head = nullptr; int length = 0; public: void add( int ); bool remove( int ); int find( int ); int count( int ); int at( int ); int len(); };
//linkedlist.cpp
#include "linkedlist.h"
void LinkedList::add(int val) { Node* n = new Node; n->value = val; n->next = nullptr; if(head == nullptr){ head = n; } else{ Node* temp = head; while(temp->next!=nullptr){ temp = temp->next; } temp->next = n; } ++length; } bool LinkedList::remove(int val) { if(head == nullptr) return false;
Node* temp = head; Node* prev = nullptr;
// Iterate till there is any node in the list while(temp != nullptr){ if(temp->value == val){ if(prev == nullptr){ head = head->next; } else{ prev->next = temp->next; } length--; return true; } prev = temp; // adjust prev temp = temp->next; // move forward temp }
return false; }
int LinkedList::find(int val) { Node* temp = head; int index = 0; while(temp!=nullptr){ if(temp->value==val){ return index; } index++; temp = temp->next; } return -1; } int LinkedList::count(int val) { Node* temp = head; int c = 0; while(temp!= nullptr){ if(temp->value == val){ c++; } temp = temp->next; } return c; } int LinkedList::at(int index) { Node* temp = head; int ind = 0; while(temp!= nullptr){ if(ind == index){ return temp->value; } ind++; temp = temp->next; } return -1; } int LinkedList::len() { return length; }
//main.cpp
#include
int main () {
LinkedList l;
l.add(6);
l.add(9);
l.add(3);
l.add(1);
l.add(9);
l.remove(1);
cout << "Index of 3 in list: " << l.find(6) << endl;
cout << "count of 9: " << l.count(9) << endl;
cout << "length: " << l.len() << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
