classes and cons reference...

1

1337-chixor;)

Hi guys,
I'm dealing with, probably, easy problem, however I'm not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)

As far as I understand I should create a list of these objects, which
I know how to do. I have difficulties with the function reverse. What
does it realy mean that as parameter it takes ListOfElements& ? Is
this a reference to the first object, ie. a pointer to the first
object?

say we have the following:

class ListOfElements
{
private:
int data;
ListOfElements * next; //pointer to the next element on the
list
public:
ListOfElements();
~ListOfElements();
addNewElement (int new_data);
}

ListOfElements * head = NULL; pointer to the first element

How can I now implement reverse? The algorithm of the reversing is not
the problem, I don't know what to do with the constant reference and
returning of an object...
 
I

Ian Collins

1337-chixor;) said:
Hi guys,
I'm dealing with, probably, easy problem, however I'm not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)
Why not use a sorted container (std:map or std::set) and use a reverse
iterator to iterate through them, or copy them to another container?
 
J

Jim Langston

1337-chixor;) said:
Hi guys,
I'm dealing with, probably, easy problem, however I'm not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)

Your parameter is a reference to a ListOfElements which is constant. Which
means you are not to change the passed in list. You'll need to then create
a copy.

It sounds to me like an easy way would simply be to create a ListOfObjects
inside this function. Then start inserting from the passed in list
backwards. I.E. Start reading from the back and insert to the end. Then
return the ListOfObjects you created.

There is most likely a standard algorithm you can use. Copy or something.
I don't use them yet, so I would just use a for loop.

This question sounds suspiciously like homework (not sure if it is or not)
so I'm reluctant to show code.
As far as I understand I should create a list of these objects, which
I know how to do. I have difficulties with the function reverse. What
does it realy mean that as parameter it takes ListOfElements& ? Is
this a reference to the first object, ie. a pointer to the first
object?

say we have the following:

class ListOfElements
{
private:
int data;
ListOfElements * next; //pointer to the next element on the
list
public:
ListOfElements();
~ListOfElements();
addNewElement (int new_data);
}

ListOfElements * head = NULL; pointer to the first element

How can I now implement reverse? The algorithm of the reversing is not
the problem, I don't know what to do with the constant reference and
returning of an object...

The constant refernece means you can treat the paramter as the instance, you
just can't change it (or call non-const methods of the class I don't think).
 
C

CHAOS

The problem I see is that the head of the returned list must be
allocated on the stack, with the rest of the nodes allocated on the
heap, and if the destructor calls delete next then the assignment is
impossible. The return by value will invoke the default copy
constructor, so both list heads will point to the same tail list of
elements, and then as the local copy goes out of scope it will destroy
them all.
 
1

1337-chixor;)

Your parameter is a reference to a ListOfElements which is constant. Which
means you are not to change the passed in list. You'll need to then create
a copy.

It sounds to me like an easy way would simply be to create a ListOfObjects
inside this function. Then start inserting from the passed in list
backwards. I.E. Start reading from the back and insert to the end. Then
return the ListOfObjects you created.

Thanks for the answer:)
Okay, I think I know how to copy and reverse this list, but I'm not
really sure how to return the object? Does it mean to return just the
head of it?
And how would the call of this function look like? Just:
reverse(head) ?
 
1

1337-chixor;)

It sounds to me like an easy way would simply be to create a ListOfObjects
inside this function. Then start inserting from the passed in list
backwards. I.E. Start reading from the back and insert to the end. Then
return the ListOfObjects you created.

There is most likely a standard algorithm you can use. Copy or something.
I don't use them yet, so I would just use a for loop.


Thanks for answering.
I think I know how to copy and reverse the list, but I still don't
know to return and object. Does it mean to return just a head of it?
So it would look this

reverse (const ListOfElements&)
{
reversed_head * ListOfObjects;
reversed_head = head;
(some code here...)
return reversed_head;
}

and calling of this function:

reverse (head);

?
 
1

1337-chixor;)

It sounds to me like an easy way would simply be to create a ListOfObjects
inside this function. Then start inserting from the passed in list
backwards. I.E. Start reading from the back and insert to the end. Then
return the ListOfObjects you created.

There is most likely a standard algorithm you can use. Copy or something.
I don't use them yet, so I would just use a for loop.


Thanks for answering.
I think I know how to copy and reverse the list, but I still don't
know to return and object. Does it mean to return just a head of it?
So it would look this

reverse (const ListOfElements&)
{
reversed_head * ListOfObjects;
reversed_head = head;
(some code here...)
return reversed_head;
}

and calling of this function:

reverse (head);

?
 
1

1337-chixor;)

Oh I'm so sorry for multiplying the posts. A message that I post
cannot be published was shown, but it was published! sorry :(
 
J

Jim Langston

1337-chixor;) said:
Thanks for the answer:)
Okay, I think I know how to copy and reverse this list, but I'm not
really sure how to return the object? Does it mean to return just the
head of it?
And how would the call of this function look like? Just:
reverse(head) ?

ListOfElements* ReveredList = reverse( MyList );
 
C

CHAOS

ListOfElements* ReveredList = reverse( MyList );

Reverse returns a ListOfElements, not a pointer to one. Which is why
issues may exist that make this less trivial than it looks.
 
G

Greg Herlihy

Reverse returns a ListOfElements, not a pointer to one. Which is why
issues may exist that make this less trivial than it looks.


But the interface provides a handy routine addNewElement() that
encapsulates all worries about dynamic versus static allocations, free
ing reverse() to implement only the algorithm.

Note that ListOfElements's interface is either incomplete (it needs
public accessors for data and next, for example) or reverse has to be
a friend of ListOfElements's. For simplicity, the reverse() routine
assumes that the latter is the case:

ListOfElements reverse( const ListOfElements& inList)
{
ListOfElements outList = inList.next ?
reverse( *inList.next)
: inList;

outList.addNewElement( inList.data);
return outList;
}

Greg
 
J

Jim Langston

CHAOS said:
Reverse returns a ListOfElements, not a pointer to one. Which is why
issues may exist that make this less trivial than it looks.

You are right. After I posted that I thought I had it wrong and waited for
the message to propogate to correct myself if wrong, but got sidetracked.
 
1

1337-chixor;)

I realized I have to change my attitude.. now the class looks as
follows:

class ListOfElements{
private:
typedef struct element{
int number;
next * element;
}*pointer;
pointer head;
public:
//constructor, destructor etc.
void addNewElement();
}

And new problem is how to pass private data to function
Sequence reverse (const Sequence& )
Should I write a method for returning head and then use it somehow
(how?) or just copy the data from the field 'number' and in function
reverse call the constructor of a class Sequence with paramter
'number' ?
 
J

Jim Langston

1337-chixor;) said:
I realized I have to change my attitude.. now the class looks as
follows:

class ListOfElements{
private:
typedef struct element{
int number;
next * element;
}*pointer;
pointer head;
public:
//constructor, destructor etc.
void addNewElement();
}

And new problem is how to pass private data to function
Sequence reverse (const Sequence& )
Should I write a method for returning head and then use it somehow
(how?) or just copy the data from the field 'number' and in function
reverse call the constructor of a class Sequence with paramter
'number' ?

Using this class ListOfElements, you can now just pass ListOfElements. I
think that a reference would be best.

Incidently, lose the "typedef". Not needed in C++. It will work with it,
but there is no need for it, it's just fluff.

Following won't compile but should give you idea.

void Foo( ListOfElements& List );

int main()
{
ListOfElements MyList;
MyList.addNewElement( whatever );
Foo( MyList );
}

Foo( ListOfElements& List )
{
// use List here
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top