Trying to make a function to put a linked list into an array

Hey guys.

I have a final wednesday and linked lists are on the test. So I am just making up things to do with my linked list class. He said we dont have to create it(though I did) we just need to know how to use it like avg of elements count etc.

So I thought of making a method that will take a linked list and put it into an array(im almost positive you cant use a regular array, and Id rather not use a vector). My question is can you return a pointer/array from a method.

I dont want the user to have to create the array I want the method to be like this

void List::List_To_Array(const List& list, int* pTr)

then inside of the method I will set pTr = new [list.count];
then i could go down the lest and set pTr[i] to the element at the current node etc

now after this function the user would be able to acces the pter they passed in as if it were an array.

Does the above question look correct?? Also who would be responsible for deleting the array. would i have to put that in the destructor?? Im unsure.


Also if you guys have any other ideas that i could cut me teeth on related to
Recursion, ptrs, inheritance, polymorphism, linkedlists,binary file processing, or exceptions

Thanks,
Brandon
If you take an int * as a parameter, the caller will have no way to access the "returned" pointer. You need to either a) return an int *, b) take an int **, or c) take an int *&. You'll also need some way to tell the caller the size of the array.
There are several ways to manage the memory returned by this function, but the simplest would be to just tell the user how to free the memory themselves (so that they know whether to use free() or delete[]).
i wanted to do c) but i didnt know you could reference it like that. I kinda thought that passing a pointer in and passing by reference were kinda the same thing.

The user would know the count because it would be the size of the array. but to make it easier on them i may be able to return an int (size of the array) but my linked list class has a count method. or they could pass in a size variable and could store it in there for them.

as for deleting my teacher always told us to never trust the user. But when he taught us pointers he said if you create it (new that is) it is your responsibiluty to delete it.

in that case would i have to delete it in my destructor for the list class. because if im thinking correctly if i delete it as the function goes out of scope the user wont have any numbers correct??

btw thanks a lot. You cleared up a lot of things
as for deleting my teacher always told us to never trust the user. But when he taught us pointers he said if you create it (new that is) it is your responsibiluty to delete it.
Your teacher is an idiot.
1. It's not your responsibility to protect stupid users from themselves. It's one thing to make members public private so that users can't (easily) screw up internal state. It's another to give the user a pointer and later free it for him. Memory management of things external to the class should be left to the user. Just to name two examples, users don't expect pointers returned by new to be automatically freed. std::vectors of pointers don't free the pointers upon destruction.
2. As a user, I'd find it rather irritating if a list gave me a pointer to a dynamic array that's tied to the lifespan of the object that allocated it. I'd prefer to decide when to free that memory myself.
3. Pointer ownership transfer is a very common operation, and quite often a preferable solution to copying and freeing. As long as there's exactly one owner at all times, it can work very well.

If you want to compromise, you could add a static function to the class that wraps delete[], but I recommend that you don't free the pointer for the user.
Last edited on
Ok thanks. Thats kinda what i was thinking. I leave that to the user then.

Another thing I just noticed is that I dont need List& list as a paramater because this is for the list class so we will already be inside of the list class.

Thanks a lot for the info helios.

P.S. do you have any other things I can try to do with linked lists for study purposes?
here is the finished code. It works just fine. Now ill just make one that does the reverse from an array, either dynamic or static and convert to a linked list

1
2
3
4
5
6
7
8
9
10
11
12
void List::List_To_Array(int*& pTr, int& size)
{
	int count = 0;
	pTr = new int[get_count()];
	size = get_count();
	ListNode* pTmp = m_first;
	for(int i = 0; i < size; i++)
	{
		pTr[i] = pTmp->m_element;
		pTmp = pTmp->m_next;
	}
}


i dont know about you guys. But damn this stuff is fun
P.S. do you have any other things I can try to do with linked lists for study purposes?
Do you have an "insert sorted" method?
no i dont. What would that do though??

Insert an element in the list based on a position the user gives?? Or insert a number then sort the least ??

Should I make a sort method first and then.

I wrote down a couple of things im going to try and do a little later
//list
copy constructor
= operator
sumof list
maybe a + operator that adds the two lists together making one long list
print method
maybe a istream method that lets the user just enter numbers and i form the list.


If you can explain what your method does I will definatley try it.
See the insertion sort Wikipedia article.
Topic archived. No new replies allowed.