Sign in to follow this  
choboNo1

Help!!!! Linked list problems

Recommended Posts

I have a linked list(ItemList class) called "list" and another copy of it named "dupeList". When i do: list.getNext(); a boolean variable in my ItemList class called "getNextCalled" will turn to 'true'. Now... here is the problem: when i do list.getNext(), the boolean variable "getNextCalled" in "dupeList" also changes to 'true'. why??? Arent they different objects?? please help. im using java by the way

Share this post


Link to post
Share on other sites
well i could think of a couple of things ..

#1) your boolean variable is a static variable ... don't think it is though.

#2) the copy you made called "dupeList" is not a deep copy of the linked list it's a actual copy of the pointer. Basically both variables are referencing the same Linked List in memory. Create a new linked list and change the memory address the "dupeList" is pointing to.

Post some code too .. that way people can help more.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm an expert with this stuff, but one of the most common ways to do it is to override the method clone(). It's one of the methods in the Object class that every other class is inherited from. In this method you create a "deep copy", which is done by either clone() -ing all the objects in it, and / or by copying the values of the primitives. For my linked lists, I "deep copy" everything but the objects the the list is pointing to. Those I just do a regular assignment (this.pointedTo = copyFrom.pointedTo). That's what I understand, anybody please correct me if I'm wrong.

- Freaker

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Oh, I'm sorry! I completely forgot. In Object, clone() is declared as protected. You can override it to be public, and I haven't actually tried but I think you can make it package private or private.

- Freaker

Share this post


Link to post
Share on other sites
You could make a copy constructor:

ItemList::ItemList(const ItemList& copy)
{
m_field1 = copy.m_field1;
m_field2 = copy.m_field2;
// and so on
}

Then when you want a deep copy just do:

ItemList duplicateList = new ItemList(originalList);

Share this post


Link to post
Share on other sites
Quote:
Original post by ll0377
You could make a copy constructor:

ItemList::ItemList(const ItemList& copy)
{
m_field1 = copy.m_field1;
m_field2 = copy.m_field2;
// and so on
}

Then when you want a deep copy just do:

ItemList duplicateList = new ItemList(originalList);


Quote:
Original post by choboNo1
im using java by the way


Er... good job there sir.

To the OP: Unless you are doing this for learning purposes, please please use one of the library collections instead (LinkedList or ArrayList).

Of course, that still won't teach you about copying objects versus aliasing them, which you'll need to understand anyway... :) However, IMHO the Java (and Python, by extension... and I'm pretty sure they both got it from somewhere else, likely Smalltalk? :s) object model encourages you *not* to make separate copies of objects when possible. And with good reason: maintaining a greater objects of the same type and similar state is harder, just like maintaining copy-and-paste code is. In most cases, if you really need a separate object, you will be just as well off to make one "from scratch" with an existing constructor.

Of course, if you *do* have a need (and I assume it comes up for some people, but somehow I've never really run into it), you can override clone() (although there is some weirdness with how it behaves...), or indeed implement a "copy constructor" - for a linked list you may find it easier to use recursion, and of course in Java it looks more like:


public class ItemList {
private Object data;
private ItemList next;
// other stuff...
public ItemList(ItemList source) {
data = source.data; // notice I'm not "copying" this
if (source.next == null) next = null;
else next = new ItemList(source.next);
}
}



This assumes a list implementation where the data is not actually contained within the list, but just referenced from there. That means you can have multiple lists referring to (subsets of) the same data, and won't lose the data if you destroy a single list. In that context, you would usually not want to copy the data items when you copy a list.

Share this post


Link to post
Share on other sites
Whoops! Sorry for the confusion, apparently I totally missed the part about it being Java. But still, if you want a deep copy you can still make a copy constructor. I think that would be easier to understand than implementing clone().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this