0

Attempting to write a constructor for LinkedList to be initialised with an array of integers.

The program would call linked(array); which will add all the values within the array in to a linkedlist.

LinkedList::LinkedList(int array[])
{
    headPtr->setData(array[0]); //setData method stores the integer at position 0 inside headPtr

    Node *currentPtr = headPtr;

    for (int i = 0; i < array.length(); ++i)    //for loop to add the integers to the next node
    {
        currentPtr->setNext(new Node(array[i])); //creates a new node with the integer value of array position i
    }
}

the trouble is the array.length (coming from Java) and I don't think the array length can be obtained this way?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Cheeseman
  • 27
  • 1
  • 7
  • 6
    Re: "coming from java" - **Stop.** Pick up a [good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read through it to learn proper, modern C++. C++ is not Java. Thinking in terms of Java when programming in C++ will give you nothing but nightmares. – In silico Sep 04 '11 at 11:14
  • 2
    You can't get the length of a C-array (unless it is terminated by something, like a C-string is). You can get it from an `std::vector`, for example. –  Sep 04 '11 at 11:15
  • @WTP: You can get the length of C-Array. However, you cannot get the length if the array has been decayed into pointer. The OP is facing the latter situation. – Nawaz Sep 04 '11 at 11:27
  • Note that since you never change `currentPtr` inside the for loop, the `currentPtr->setNext` repeatedly changes the first node. The other nodes are left untouched. This is probably not what you want. – fredoverflow Sep 04 '11 at 11:39
  • @FredOverflow - I believe I have fixed it with this. added currentPtr = currentPtr->getNextPtr(); havnt tested it yet, trying to fix the other errors :P – Cheeseman Sep 04 '11 at 12:17

3 Answers3

3

I would suggest you to use iterator idiom, and make the constructor a templated constructor as:

class LinkedList
{
    //...
    public:
    template<typename FwdIterator>
    LinkedList(FwdIterator begin, FwdIterator end)
    {
       for (;begin != end; ++begin) 
       {
          //treat begin as pointer, and *begin as dereferenced object
       }
    }
    //...
};

And then you can use it as:

int arr[] = {1,2,3,4,5,6,7,8,9,10};

LinkedList lnklist(arr, arr+10);

Not only that. If you've std::vector<int>, then you can also use it to construct the linked list, as:

std::vector<int> v;
//..
LinkedList lnklist(v.begin(), v.end());

So using iterator idiom gives you this much power and flexibility. :-)

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

As Nawaz explained, going with iterator solution is better. But if you want to go with array ( static one though), then compiler can automatically deduce the size for you.

template<size_t size>
LinkedList::LinkedList(int (&array)[size]) 
{ 
    headPtr->setData(array[0]); //setData method stores the integer at position 0 inside headPtr 

    Node *currentPtr = headPtr; 

    for (int i = 0; i < size++i)    //for loop to add the integers to the next node 
    { 
        currentPtr->setNext(new Node(array[i])); //creates a new node with the integer value of array position i 
    } 
} 

Can be called as shown below.

int arr[] = {1,2,3,4,5,6,7,8,9,10};   

LinkedList lnklist(arr);
Community
  • 1
  • 1
Jagannath
  • 3,995
  • 26
  • 30
0

Like others have said, it is not only important but vital that you get a good introductory C++ book and read it from front to back, simultaneously trying to forget what you know about Java while in C++ mode. They are not remotely similar.

Now to your problem, it can be solved by using std::vector and using its size method:

// put this with the other includes for your file
#include <vector>

LinkedList::LinkedList(const std::vector<int>& array)
{
    headPtr->setData(array[0]); //setData method stores the integer at position 0 inside headPtr

    Node *currentPtr = headPtr;

    for (int i = 0; i < array.size(); ++i)    //for loop to add the integers to the next node
    {
        currentPtr->setNext(new Node(array[i])); //creates a new node with the integer value of array position i
    }
}

If you don't want to use vector, you have to pass in the size of the array to the function:

LinkedList::LinkedList(int array[], int arrlen)
{
    headPtr->setData(array[0]); //setData method stores the integer at position 0 inside headPtr

    Node *currentPtr = headPtr;

    for (int i = 0; i < arrlen; ++i)    //for loop to add the integers to the next node
    {
        currentPtr->setNext(new Node(array[i])); //creates a new node with the integer value of array position i
    }
}

But it is recommended to use the vector version.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Thanks a lot, I have a book I am reading at the moment and it's the the tutorial exercise I am trying to do, their example is a bit different, might need to get a couple of different books :P I seem to have added your suggestion with the vector ok but i got a warning: comparison between signed and unsigned integer expressions. Not quite sure what it means with signed and unsigned... – Cheeseman Sep 04 '11 at 11:37
  • @Cheeseman that means you are comparing a number that _can_ be negative with a number that _cannot_ be negative. The number that _cannot_ be negative can hold an higher positive value because it's not wasting space that is reserved for negative values. This means that when you get up to a certain size, adding one to an _unsigned_ value will get you the next higher value, where adding one to a _signed_ value is undefined behaviour. `vector::size` returns an `unsigned int`. To fix the error, change `for (int i = 0; i < array.size(); ++i)` to `for (unsigned int i = 0; i < array.size(); ++i)` – Seth Carnegie Sep 04 '11 at 11:55
  • ah that makes sense, thanks Seth :) So to call it, LinkedList list2(arr); should be fine? – Cheeseman Sep 04 '11 at 12:00
  • @Cheeseman yes, that will be fine, if `arr` is a `vector`. If you decided to go with the second version I wrote, then you'd have to pass the length, like `LinkedList list(arr, 5)` or something (where `5` is the number of elements in the array). – Seth Carnegie Sep 04 '11 at 12:07