0

I have an assignment which involves different linked list operations. One of them involves overloading the square bracket operator to be able to print the ith element of the linked list. I have everything else done but I am really lost on this. This is what I am working with. The list class is as follows:

class List {
public:
// Creates a default empty list
List();

// Simple destructor
~List();

// Insert "data" at the very end of the list
void AddToFront(int data);

// Remove and return the first data item from the list.
int deleteFront();

// Prints the list
void Print() ;

// Returns the size of the list
unsigned int Size() const;


//overloaded assignment operator
Node operator[](unsigned int i) ;



private:

Node *m_head;

};

Also, here is my node class:

class Node {
public:
    Node();
    ~Node();
    Node(int data);

    int m_data;
    Node *m_next;
};

Any help on the overloading [] operator would be greatly appreciated.

Matthew Hanson
  • 321
  • 5
  • 7
  • 15

1 Answers1

2
Node* operator [] (int value) {
    Node *temp = this->m_head;
    for(int i = 0; i < value && temp!=NULL; i++) {
        temp = temp->m_next;
    }
    return temp;
}

I assume that you want to return the node corresponding to the value specified, in the square brackets. You overload any operator using the operator keyword followed by the operator, and then the parameters passed.

For more info check this :: Operator overloading

EDIT ::

As pointed by erip and Lajos there should be a way, in case the (value > size_of_list), in that case, a possible solution would be throw an exception, which you can later catch in your program to show that value was out of bound. Or considering the current implementation, if value > size_of_list in that case temp would become NULL, so during your execution you can check if value of Node * returned is NULL or not.

A further more optimized way would be to keep a variable size_of_list in the class List, and then we can simply add an if condition to the function like this ::

if(value >= size_of_list) // equal to sign is put, considering your `size_of_list` starts from 1
    return NULL; 

This, would be more optimized in case of large Lists, which would avoid wasteful execution of the for loop!

Community
  • 1
  • 1
user007
  • 2,156
  • 2
  • 20
  • 35
  • It would make more sense to return `m_data` for the ith `Node`, but that doesn't change much. Nice answer. – erip Dec 07 '15 at 23:32
  • You should probably do something else besides `temp!=NULL`, too. For example, `if(size_of_list < value) /* throw exception */` – erip Dec 07 '15 at 23:33
  • @erip True, but I don't think the OP wants to keep an extra variable in the `list` class, moreover I intended to check what I return, if I return `NULL` from the function that would mean `size_of_list < value` I have always had more love for `if else` than exception :P – user007 Dec 07 '15 at 23:40
  • If the element is not found, then you might consider throwing an exception. Also, you might consider using a currentElement Node member for List along with a currentIndex, because then, by any chance currentIndex <= value, you can use a shortcut. This would be useful for very long lists. – Lajos Arpad Dec 07 '15 at 23:40
  • 2
    @user007 OP has a `Size` method, implying he has a size attribute in the `List` class. Your current implementation returns the last element if `size < value`, so if you have list [1, 2, 3] and `value` is 100, you return the `3`, which is unexpected. – erip Dec 07 '15 at 23:42
  • Unless OP calculates the size of the list every time `Size` is called. That wouldn't be good. – erip Dec 07 '15 at 23:43
  • @erip It won't return the last element, I checking for `temp` and not `temp->next`, so in case value `val > size` I return `NULL` I don't understand where it is wrong. – user007 Dec 08 '15 at 00:04