If anyone could help me, that would be great. We just went over classes and things recently and our teacher didn't really cover things very well.

This is the assignment:

/* **********************************************

Below is the code for an implementation of a linked list. The implementation has one missing method and one flaw.

Implement the missing size() method, which returns the length of the list.

Then, fix the insertAt() method so that it does not crash when first is NULL (when all items have been removed from the list).

********************************************** */

Here is the different code files: ( I will separate them with "*")

**********************************************

Main:

**********************************************Code:#include <iostream> using namespace std; #include "linkedlist.hpp" int main () { LinkedList *list = new LinkedList(3); // Append the values 8, 12, and 2 list->insertAt(8, 1); list->insertAt(12, 2); list->insertAt(2, 3); // Print the list for (int i = 0; i < 4; i++) { cout << list->at(i) << " "; } cout << endl; list->removeAt(0); list->removeAt(0); list->removeAt(0); list->removeAt(0); list->insertAt(50, 0); // Print the list cout << list->at(0) << endl; cout << endl; // Free the list delete list; }

linkedList.hpp

**********************************************Code:#pragma once struct ListNode { int value; ListNode *next; }; // Singly linked list of integers class LinkedList { public: LinkedList (int value); ~LinkedList (); int& at (int index); void insertAt (int value, int index); void removeAt (int index); int size (); private: ListNode *first; };

linkedList.cpp

Code:#include <cstddef> #include <iostream> using namespace std; #include "linkedlist.hpp" LinkedList::LinkedList(int initialValue) { first = new ListNode; first->value = initialValue; first->next = NULL; } LinkedList::~LinkedList () { ListNode *node = first; while (node != NULL) { ListNode *nodeToDelete = node; node = node->next; delete nodeToDelete; } } int& LinkedList::at (int index) { // Find the node with the given index ListNode *searchNode = first; for (int i = 0; i < index; i++) { searchNode = searchNode->next; } // Return a reference to the value for that node return searchNode->value; } void LinkedList::insertAt (int value, int index) { ListNode *node = new ListNode; node->value = value; if (index == 0) { // Replace first node->next = first->next; first = node; } else { // Find the node immediately before and insert after it ListNode *searchNode = first; for (int i = 0; i < index - 1; i++) { searchNode = searchNode->next; } node->next = searchNode->next; searchNode->next = node; } } void LinkedList::removeAt (int index) { if (index == 0) { // Remove first ListNode *newFirst = first->next; delete first; first = newFirst; } else { // Find the node immediately before and delete after it ListNode *searchNode = first; for (int i = 0; i < index - 1; i++) { searchNode = searchNode->next; } ListNode *newNext = searchNode->next->next; delete searchNode->next; searchNode->next = newNext; } }

agrees : Much better than the reply I wrote, then deleted before posting it :)