Please help me fix the code in the image for the code below. Errors are in lines 35, 37, 47, and 51. .cpp File: #include #include #include #include "ArrayList.h" using namespace std; // Exception class definition class ArrayListException { private: string message; public: ArrayListException(const string& message) : message(message) {} string getMessage() const { return message; } }; // Node class definition template class ArrayList::Node { public: T data; Node* next; Node(const T& data, Node* next = nullptr) : data(data), next(next) {} }; // ArrayList class definitions template ArrayList::ArrayList() : rear(nullptr), length(0) {} template ArrayList::~ArrayList() { Node* current = rear->next; while (current != rear) { Node* temp = current; current = current->next; delete temp; } delete rear; } template bool ArrayList::isEmpty() const { return (rear == nullptr); } template int ArrayList::size() const { return length; } template void ArrayList::insert(const T& item) { Node* newNode = new Node(item); if (rear == nullptr) { rear = newNode; rear->next = rear; } else { newNode->next = rear->next; rear->next = newNode; rear = newNode; } length++; } template void ArrayList::remove(const T& item) { if (rear == nullptr) { throw ArrayListException("Cannot remove item from empty ArrayList"); } Node* current = rear->next; Node* prev = rear; bool found = false; do { if (current->data == item) { found = true; break; } prev = current; current = current->next; } while (current != rear->next); if (found) { if (rear->next == rear) { rear = nullptr; } else if (current == rear) { rear = prev; } prev->next = current->next; delete current; length--; } else { throw ArrayListException("Item not found in ArrayList"); } } template void ArrayList::print() const { if (rear == nullptr) { cout << "[]"; return; } cout << "["; Node* current = rear->next; while (current != rear) { cout << current->data << ", "; current = current->next; } cout << current->data << "]"; } .h File #include #include #ifndef ARRAYLIST_H #define ARRAYLIST_H using namespace std; class ArrayListException : public exception { private: string msg; public: ArrayListException(string message) : msg(message) {} virtual const char* what() const throw() { return msg.c_str(); } }; template class ArrayList { private: class Node { public: T data; Node* next; Node(T item) : data(item), next(nullptr) {} }; Node* rear; int length; public: ArrayList() : rear(nullptr), length(0) {} ~ArrayList() { while (rear != nullptr) { Node* temp = rear->next; delete rear; rear = temp; } } void insert(T item) { Node* newNode = new Node(item); if (rear == nullptr) { newNode->next = newNode; } else { newNode->next = rear->next; rear->next = newNode; } rear = newNode; length++; } void remove(T item) { if (rear == nullptr) { throw ArrayListException("Error: Cannot remove item from empty list."); } Node* current = rear; Node* previous = nullptr; bool found = false; do { if (current->data == item) { found = true; break; } previous = current; current = current->next; } while (current != rear->next); if (!found) { throw ArrayListException("Error: Item not found in list."); } if (previous == nullptr) { rear = nullptr; } else { previous->next = current->next; if (current == rear) { rear = previous; } } delete current; length--; } T get(int index) const { if (index < 0 || index >= length) { throw ArrayListException("Error: Index out of range."); } Node* current = rear->next; for (int i = 0; i < index; i++) { current = current->next; } return current->data; } int size() const { return length; } }; #endif
Please help me fix the code in the image for the code below. Errors are in lines 35, 37, 47, and 51.
.cpp File:
#include <iostream>
#include <cassert>
#include <string>
#include "ArrayList.h"
using namespace std;
// Exception class definition
class ArrayListException {
private:
string message;
public:
ArrayListException(const string& message) : message(message) {}
string getMessage() const { return message; }
};
// Node class definition
template<typename T>
class ArrayList<T>::Node {
public:
T data;
Node<T>* next;
Node<T>(const T& data, Node<T>* next = nullptr) : data(data), next(next) {}
};
// ArrayList class definitions
template<typename T>
ArrayList<T>::ArrayList() : rear(nullptr), length(0) {}
template<typename T>
ArrayList<T>::~ArrayList() {
Node<T>* current = rear->next;
while (current != rear) {
Node<T>* temp = current;
current = current->next;
delete temp;
}
delete rear;
}
template<typename T>
bool ArrayList<T>::isEmpty() const {
return (rear == nullptr);
}
template<typename T>
int ArrayList<T>::size() const {
return length;
}
template<typename T>
void ArrayList<T>::insert(const T& item) {
Node<T>* newNode = new Node<T>(item);
if (rear == nullptr) {
rear = newNode;
rear->next = rear;
} else {
newNode->next = rear->next;
rear->next = newNode;
rear = newNode;
}
length++;
}
template<typename T>
void ArrayList<T>::remove(const T& item) {
if (rear == nullptr) {
throw ArrayListException("Cannot remove item from empty ArrayList");
}
Node<T>* current = rear->next;
Node<T>* prev = rear;
bool found = false;
do {
if (current->data == item) {
found = true;
break;
}
prev = current;
current = current->next;
} while (current != rear->next);
if (found) {
if (rear->next == rear) {
rear = nullptr;
} else if (current == rear) {
rear = prev;
}
prev->next = current->next;
delete current;
length--;
} else {
throw ArrayListException("Item not found in ArrayList");
}
}
template<typename T>
void ArrayList<T>::print() const {
if (rear == nullptr) {
cout << "[]";
return;
}
cout << "[";
Node<T>* current = rear->next;
while (current != rear) {
cout << current->data << ", ";
current = current->next;
}
cout << current->data << "]";
}
.h File
#include <string>
#include <iostream>
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
using namespace std;
class ArrayListException : public exception {
private:
string msg;
public:
ArrayListException(string message) : msg(message) {}
virtual const char* what() const throw() {
return msg.c_str();
}
};
template <typename T>
class ArrayList {
private:
class Node {
public:
T data;
Node* next;
Node(T item) : data(item), next(nullptr) {}
};
Node* rear;
int length;
public:
ArrayList() : rear(nullptr), length(0) {}
~ArrayList() {
while (rear != nullptr) {
Node* temp = rear->next;
delete rear;
rear = temp;
}
}
void insert(T item) {
Node* newNode = new Node(item);
if (rear == nullptr) {
newNode->next = newNode;
}
else {
newNode->next = rear->next;
rear->next = newNode;
}
rear = newNode;
length++;
}
void remove(T item) {
if (rear == nullptr) {
throw ArrayListException("Error: Cannot remove item from empty list.");
}
Node* current = rear;
Node* previous = nullptr;
bool found = false;
do {
if (current->data == item) {
found = true;
break;
}
previous = current;
current = current->next;
} while (current != rear->next);
if (!found) {
throw ArrayListException("Error: Item not found in list.");
}
if (previous == nullptr) {
rear = nullptr;
}
else {
previous->next = current->next;
if (current == rear) {
rear = previous;
}
}
delete current;
length--;
}
T get(int index) const {
if (index < 0 || index >= length) {
throw ArrayListException("Error: Index out of range.");
}
Node* current = rear->next;
for (int i = 0; i < index; i++) {
current = current->next;
}
return current->data;
}
int size() const {
return length;
}
};
#endif
Step by step
Solved in 3 steps