Deep copy of a list?

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
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.

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;
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.
How does class BookList object point to (the first) BookListNode?


Why doesn't the BookListNode have a custom constructor?
Last edited on
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.
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?
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: .....
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.
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?
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).
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.
};
Where in the list do you want to add? To begin, end, or somewhere in the middle (to keep list sorted)?

What is 'length'?
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.