You need to create new Book objects then put those in a new List:
List<Book> books_2 = books_1.Select(book => new Book(book.title)).ToList();
Update: Slightly simpler... List<T> has a method called ConvertAll that returns a new list:
List<Book> books_2 = books_1.ConvertAll(book => new Book(book.title));
Answer from Mark Byers on Stack Overflowc# - How create a new deep copy (clone) of a List<T>? - Stack Overflow
struct - C++ Deep Copying Linked List - Stack Overflow
How can I do a copy of a pointers list? C++
c - How to deep Copy this linked list - Stack Overflow
Hi I am having trouble figuring out how to create a deep code of this struct I implemented.
struct node_t {
int priority,moves;
int** data; //2D matrix of integers
struct node_t *prev_board;
};What I have so far is trying to use recursion but I just can't get it to work.
void deep_copy(struct node_t* src,struct node_t* dest, int len) {
if (src == NULL) {
dest = NULL;
return;
}
dest = malloc(sizeof(struct node_t));
dest->priority = src->priority;
dest->data = (int **)malloc(len * sizeof(int *));
int r = 0;
for (r = 0; r < len; ++r) {
dest->data[r] = (int *)malloc(len * sizeof(int ));
}
copy_v(src->data,dest->data,len); // This just copies the 2D Matrix values
deep_copy(src->prev_board,dest->prev_board,len);}
But I also think I don't want to use recursion since stackoverflow and I want to be memory efficient. Any help would be appreciated.
You need to create new Book objects then put those in a new List:
List<Book> books_2 = books_1.Select(book => new Book(book.title)).ToList();
Update: Slightly simpler... List<T> has a method called ConvertAll that returns a new list:
List<Book> books_2 = books_1.ConvertAll(book => new Book(book.title));
Create a generic ICloneable<T> interface which you implement in your Book class so that the class knows how to create a copy of itself.
public interface ICloneable<T>
{
T Clone();
}
public class Book : ICloneable<Book>
{
public Book Clone()
{
return new Book { /* set properties */ };
}
}
You can then use either the linq or ConvertAll methods that Mark mentioned.
List<Book> books_2 = books_1.Select(book => book.Clone()).ToList();
or
List<Book> books_2 = books_1.ConvertAll(book => book.Clone());
You need to allocate new memory or new list elements as you add them, change your code to do the following:
// LinkedList.cpp
LinkedList::LinkedList(const LinkedList & ll)
{
if (ll.head == nullptr)
return;
// Create a temp variable since ll.current doesn't move/change.
node* tmp = ll.head;
// Allocate a new node in memory.
head = new node;
// Copy over the value.
head->data = tmp->data;
// Set the 'next' value to null (the loop will fill this in).
head->next = nullptr;
// Point 'current' to 'head'.
current = head;
// Move to next item in ll's list.
tmp = tmp->next;
while (tmp != nullptr)
{
// Allocate new memory for a new 'node'.
current->next = new node;
// Point to this new 'node'.
current = current->next;
// Copy over the data.
current->data = tmp->data;
// By default set the 'next' to null.
current->next = nullptr;
// Move along ll's list.
tmp = tmp->next;
}
}
Also, in your class get rid of typedef node* nodePtr. There is no need for that, it's cleaner to simply use node* for head, current and temp. Lastly, don't forget in your class' destructor to clear out dynamically allocated memory:
LinkedList::~LinkedList()
{
current = head;
while(current != nullptr)
{
current = current->next;
delete head;
head = current;
}
}
This cannot work, as you never allocate new list elements for the actual list object (using the 'new' operator), but only reuse existing ones. Just think about what happens, if ll has more elements than the actual list?
I am fairly new to the Python realm. What is the difference between shallow and deep copy? I know the theoritical part of it but Im not actually able to understand while practicing it.
1)Does the list.copy() work on shallow copy technique? How does it work?
2)How does it differ from the other copying techniques like the = operator; slicing [:] ; list() constructor?
I have experimented it on the interactive python using two lists list1 and list2. When using "List2 = List1", I was able to see changes I made to the List2 on the List1. Whereas copying the contents of List1 to the List2 using " List2 = List1.copy()" , didn't do so.
Please enlighten me! Thanks in advance!
I'm still working on my Data Structure's homework and I'm hung up again, this time on overloading the = operator. I'm almost certain I'm deep copying, though I wouldn't be shocked if someone told me otherwise. The issue rises when I'm trying to clear the linked lists. The first clears fine, but the ones that the first was copied to result in "_CrtlsValidHeapPointer(block)".
Here's my overloaded =operator and clear, any help would be greatly appreciated: (overloaded =operator, is passed a LinkedList)
Node<T>* current = m_head;
Node<T>* eList = rhs.m_head;
m_head = new Node<T>(rhs.m_head->m_data, eList->m_next);
current = m_head;
eList = rhs.m_head->m_next;
while (eList != NULL)
{
current->m_next = new Node<T>(eList->m_data,eList->m_next);
eList = eList->m_next;
current = current->m_next;
}
m_size = rhs.m_size;(clear function)
Node<T>* ptr = m_head;
while (ptr != NULL)
{
m_head = ptr->m_next;
delete[] ptr;
ptr = m_head;
}
m_head = NULL;
m_size = 0;edit: I think what I'm doing has something to do with deleting a dangling pointer? If that's the case then my =operator is not implemented correctly, but I'm unsure how else I would do it.
This entirely depends on how you plan to use the copy.
If you do a shallow copy like
List<int> x = new List<int>() { 1, 2, 3, 4, 5 };
List<int> y = x;
y[2] = 4;
Then x will contain {1, 2, 4, 4, 5 }
If you do a deep copy of the list:
List<int> x = new List<int> { 1, 2, 3, 4, 5 };
List<int> y = new List<int>(x);
y[2] = 4;
Then x will contain { 1, 2, 3, 4, 5 } and y will contain { 1, 2, 4, 4, 5 }
How you plan on using the copy really determines whether you use shallow or deep copies.
If you have List<int> originalList = new List{1, 2}
then doing this:
List<int> newList = new List<int>();
foreach(int i in originalList)
newList.Add(i);
will get you a cloned list.
However, if you tried what I did above with a List generic on some reference type, then you would not succeed. After altering one of the objects in your list, you would see the altered version whether referencing it from the original list or the new list.