0

I've been expected to create a linked list in C++ for an assignment, but I've been having a lot of trouble with one particular method.

#include "myLList.h"
#include "Datum.h"
#include <stddef.h>

myLList::myLList()
{
    head = curr = tail = NULL;
}

myLList::myLList(float arr[])
{
    if(arr[0] = NULL);
        exit(0);
    int arrayLength = sizeof(arr)/sizeof(arr[0]);

    Datum *temp = new Datum(arr[0]);
    head = temp;
    tail = temp;

    for(int i = 1; i < arrayLength; i++)
    {
        insertAtEnd(arr[i]);
    }

}

void myLList::insertAtEnd(float value)
{
    Datum *temp = new Datum(value);
    if(head == NULL)
    {
        head = temp;
        tail = head;
        temp = NULL;
    }
    else
    {
        tail->setNext(*temp);
        tail = temp;
    }
}

Datum myLList::operator[](int index)
{
    Datum *temp = head;
    for(int i = 0; i++; i < index)
    {
        temp = temp->getNext();
    }
    return temp->getData();
}


void myLList::insert(int index, float value)
{
    Datum *temp = new Datum(value);
    curr = head;
    Datum *prev = curr;

    for(int i = 1; i < index; i++)
    {
        prev = curr;
        curr = curr->getNext();
    }

    prev->setNext(*temp);
    temp->setNext(*curr);
}


myLList::~myLList()
{
    //dtor
}

When testing the insertAtEnd and overloaded [] method using the following main.cpp:

#include <iostream>
#include "Datum.h"
#include "myLList.h"
#include <stddef.h>


using namespace std;

int main()
{
 myLList linkedList = myLList();

    linkedList.insertAtEnd(1);
    linkedList.insertAtEnd(2);
    linkedList.insertAtEnd(3);

    cout << "third element: " << linkedList[2].getData() << endl;
}

The output is simply the value at the head of the linkedList no matter what index I push into the overloaded [] operator. Any help would be greatly appreciated

2
  • Did you try to debug your code? At what line in the code does the expected value differ from the actual value? Commented Oct 30, 2019 at 0:07
  • 1
    Please provide a minimal reproducible example containing the definition of setNext. I think you should pointers to setNext, not values. Commented Oct 30, 2019 at 0:12

1 Answer 1

2

Inside your function overloading the [] operator:

Change this:

for(int i = 0; i++; i < index)
{
    temp = temp->getNext();
}

To this:

for(int i = 0; i < index; i++)
{
    temp = temp->getNext();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Happy to help. Consider accepting my answer please :).
@anteater7645 warning: If index is greater than the length of the list, the program will wander into the magical realm of Undefined Behaviour. After that anything can happen, with the worst being the program looking like it works. Probably want to test for temp == NULL and throw an informative exception if it happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.