Can someone help me create a copy constructor or similar that can copy contents
ID: 3673633 • Letter: C
Question
Can someone help me create a copy constructor or similar that can copy contents from a singly linked list to a doubly linked list? i've attached the source code for both list.
source for singly linked list:
#pragma once
#include <cassert>
template <typename ELT>
class SinglyLinkedNode {
private:
ELT _element;
SinglyLinkedNode<ELT>* _next;
public:
SinglyLinkedNode(ELT element, SinglyLinkedNode<ELT>* next) {
_element = element;
_next = next;
}
ELT element() { return _element; }
void set_element(ELT element) { _element = element; }
SinglyLinkedNode<ELT>* next() { return _next; }
void set_next(SinglyLinkedNode<ELT>* next) { _next = next; }
};
template <typename ELT>
class SinglyLinkedListIterator;
template <typename ELT>
class SinglyLinkedList {
friend SinglyLinkedListIterator<ELT>;
private:
SinglyLinkedNode<ELT>* _head;
int _length;
public:
SinglyLinkedList() {
_head = nullptr;
_length = 0;
}
~SinglyLinkedList() { clear(); }
int length() { return _length; }
bool is_empty() { return (nullptr == _head); }
ELT front() {
assert(!is_empty());
return _head->element();
}
void add_front(ELT element) {
SinglyLinkedNode<ELT> *new_head = new SinglyLinkedNode<ELT>(element, _head);
_head = new_head;
_length++;
}
void remove_front() {
assert(!is_empty());
SinglyLinkedNode<ELT>* new_head = _head->next();
delete _head;
_head = new_head;
_length--;
}
void clear() {
while (!is_empty()) {
remove_front();
}
// double check invariants
assert(is_empty());
assert(0 == _length);
assert(nullptr == _head);
}
};
template <typename ELT>
class SinglyLinkedListIterator {
private:
SinglyLinkedNode<ELT>* _location;
public:
SinglyLinkedListIterator(SinglyLinkedList<ELT>* list) {
assert(list != nullptr);
_location = list->_head;
}
bool past_end() { return (nullptr == _location); }
ELT get() {
assert(!past_end());
return _location->element();
}
void advance() {
assert(!past_end());
_location = _location->next();
}
};
source for doubly linked list:
Explanation / Answer
Here i have given a module for copying the element from one linked list to another list.
DoublyLinkedNode *copyList (SinglyLinkedNode *L) //accepts the head of singly linked list as argument and // return the pointer of doublylinked list type.
{
SinglyLinkedNode *curr = L; // to keep track of "current node" of SinglyLinkedNode
DoublyLinkedNode *copy,*previous,*head; //copy pointer points to current node and previous points to 1 node previous.
DoublyLinkedNode *copy = new DoublyLinkedNode; //creating a new node of DoublyLinkedNode type(by run time memory allocation)
copy->element =curr->element; //copying the content of one list node to another list node
copy->next = NULL;
copy->prev=NULL;
// head pointer will be return in last
head = copy;
//moving 1 node further
curr = curr->next;
// Keep track of singly linked list add correspondingly add elemnt to doubly linked list
while (curr != NULL)
{
// Allocate the run time memory for new node of doubly lineked list
copy->next = new DoublyLinkedNode;
// so that we do not loose previous node of DoublyLinked list.
previous=copy;
// moving copy node further
copy=copy->next;
// Copying the content
copy->element = curr->element;
//maintaing the previous link of doublylinkedlist node
copy->prev=previous;
// maintaing the next link of node
copy->next = NULL;
// Advance 'current' to the next element
curr = curr->next;
}
// Return pointer to first node of doubly linked list
return head;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.