Deep copy of a list?

Mar 25, 2016 at 2:32pm
I need to perform a deep copy of a list, but I'm not sure how to continue. Here's my struct declaration, which is set to private:

1
2
3
4
5
  struct BookListNode
		{
			Book bookData;
			BookListNode *next;
		};


And here's the function, otherList being the variable I have to make a deep copy of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BookList::BookList(const BookList& otherList)
{
BookListNode *current = otherList;  

	BookListNode *copy = new BookListNode;
	copy->next = NULL;

	while (current != NULL) {
		(copy->bookData) = (current->bookData);
		*(copy->next) = *(current->next);

		copy = copy->next;
		current = current->next;
	}
	return copy;
Last edited on Mar 25, 2016 at 2:32pm
Mar 25, 2016 at 2:39pm
Line 12: Problem here is you now have two lists pointing to the the same BookListNode.
You're going to have a problem, when you try to delete a node from your BookList. Deleting an entry from a BookList should delete the associated BookListNode. Once you do that, the other BookList is going to point to a BookListNode that no longer exists.

You should either use smart pointers, or create a copy of the BookListNode.

Mar 25, 2016 at 5:32pm
Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BookListNode *current = otherList;  

	BookListNode *copy = new BookListNode;
	BookListNode *secondCopy = new BookListNode;

	copy->next = NULL;

	//traverses the list
	while (current != NULL) {
		(copy->bookData) = (current->bookData);
		*(copy->next) = *(current->next);

		secondCopy = copy->next;
		current = current->next;
	}
	return secondCopy;
Mar 25, 2016 at 7:35pm
But now it says that for otherList, "no suitable conversion function from 'const BookList' to 'BookList::BookListNode* exists", which I understand how it's incorrect, but I'm not sure how else to work otherList into the code.
Mar 25, 2016 at 7:50pm
How does class BookList object point to (the first) BookListNode?


Why doesn't the BookListNode have a custom constructor?
Last edited on Mar 25, 2016 at 7:52pm
Mar 26, 2016 at 2:20am
The BookListNode struct is declared as a private member of the BookList class. I'm supposed to code this without giving BookListNode its own constructor.
Mar 26, 2016 at 3:14pm
The BookListNode struct is declared as a private member of the BookList class.
struct BookListNode is a type.
class BookList is a type. BookList has member variables and member methods.

What is the name and exact type of the member variable of BookList that is or points to an instance of BookListNode?
Mar 26, 2016 at 3:39pm
I believe it's "first."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class BookList
{
	private:
		struct BookListNode
		{
			Book bookData;
			BookListNode *next;
		};

		int length;

		//Create a sample first item of the list
		BookListNode *first;

       public: .....
Mar 26, 2016 at 3:51pm
How we are talking. That line 12 comment, however, is way off.
If every List has at least one node, what is the value hold in that node?

A list can be empty, with no nodes at all. The first is a pointer, not a node. The pointer on empty list should have value nullptr.

1
2
3
BookList::BookList( const BookList& otherList ) {
  BookListNode * other = otherList.first;
  BookListNode * * tail = &first;


Does your BookList have a "append to tail" method? If yes, show its code.


The last shown line in your constructors has had a return statement. Constructors don't "return" a value.
Mar 26, 2016 at 4:09pm
It will have an append method, but I haven't coded it yet, and I already deleted the return statement. Could you explain what those double asterisks do exactly?
Mar 26, 2016 at 6:53pm
Variable declarations are best to read from right to left. Those say:

Name 'other' is a pointer variable to type BookListNode object(s).
Name 'tail' is a pointer variable to a pointer to type BookListNode object(s).
Mar 26, 2016 at 7:53pm
Got it, thanks for all the help! One more thing, this is what I have for my Add method (which is separate from the "append to tail" method). This is supposed to add a new Book to the list (Book being another class I imported). This isn't correct, but am I in the ballpark?

1
2
3
4
5
6
7
8
9
10
11
void BookList::Add(const Book e)
{
	struct BookListNode newNode;

	for (int i = 0; i < length; i++)
	{
		newNote.bookData->next = e;
	}

	length++; //I set the length declaration to be equal to 0.
};
Mar 27, 2016 at 8:27am
Where in the list do you want to add? To begin, end, or somewhere in the middle (to keep list sorted)?

What is 'length'?
Mar 27, 2016 at 12:43pm
It doesn't matter where I add it, so the end would be fine. Length is just an ongoing count of how many entries are in the list.
Topic archived. No new replies allowed.