Before you read this, I am new to c++, any feedback is appreciated just don't be harsh please:(
So basically I am adding elements to a doubly linked list and in the function below my aim is to take its elements and add them into my singly linked list and return it, however I am just returning a pointer to the list.
I am just stuck on this little problem,
When I return it, it gives me an address, I am doing something wrong in the function, I just can put my finger on it, the rest of the code works with me fine. I hope I delivered my question correctly, ask me other wise. Any help is very appreciated.
template <class Type>
SinglyLinkedList<Type> *DoublyLinkedList<Type>:: Function() {
SinglyLinkedList<Type>* newList = new SinglyLinkedList<Type>();
nodeDLL<Type>* p = head2;
//I added the values from the doubly linked list into the singly linked list
while (p !=NULL) {
newList->addToHead(p->value2);
p=p->next2;
}
// I used the print function to make sure the above works and it works the way I want,
newList->print();
// but when I return it here it gives me an address?
return newList;
For reference here is the rest of the code:
class for the Singly linked list node
template <class Type>
class nodeSLL {
public:
nodeSLL* next;
Type value;
nodeSLL(Type v, nodeSLL* n) {
value = v;
next = n;
}
~nodeSLL() {
cout << "node destroyed" << endl;
}
};
Singly linked list class
template <class Type>
class SinglyLinkedList {
public:
nodeSLL<Type>* head;
nodeSLL<Type>* tail;
SingyLinkedList();
SinglyLinkedList(SinglyLinkedList<Type> &obj);
void addToHead(Type v);
void addToTail(Type v);
void print();
~SinglyLinkedList();
};
template <class Type>
SinglyLinkedList<Type>::SinglyLinkedList(const SinglyLinkedList &obj) // Copy Constructor
{
head = obj.head;
tail = obj.tail;
}
template <class Type>
SinglyLinkedList<Type>:: SinglyLinkedList() {
head = tail = NULL;
}
template <class Type>
SinglyLinkedList<Type>:: ~SinglyLinkedList() {
nodeSLL<Type> *p = head;
while (p != NULL) {
head = head->next;
delete p;
p = head;
}
}
print function
template <class Type>
void SinglyLinkedList<Type>:: print() {
nodeSLL<Type> * t = head;
while (t != NULL) {
cout << t->value << " => ";
t = t->next;
}
cout << " NULL"<< endl;
}
add to head function
template <class Type>
void SinglyLinkedList<Type>:: addToHead(Type v) {
nodeLL<Type> *newNode = new nodeSLL<Type>(v, head);
if (head == NULL) {
head = tail = newNode;
} else {
head = newNode;
}
Doubly linked list node
template <class Type>
class nodeDLL {
public:
nodeDLL* next2;
nodeDLL* prev2;
Type value2;
nodeDLL<Type>(nodeDLL<Type>* prv, Type v, nodeDLL<Type>* n) {
value2 = v;
next2 = n;
prev2 = prv;
}
~nodeDLL() {
prev2 = next2 = NULL;
cout << "node destroyed" << endl;
}
};
Doubly linked list class
template <class Type>
class DoublyLinkedList {
public:
nodeDLL<Type>*head2;
nodeDLL<Type>*tail2;
DounlyLinkedList();
void addToHead2(Type v);
void Clear();
~DoublyLinkedList();
};
Add to head doubly linked list
template <class Type>
void DoublyLinkedList<T>:: addToHead2(Type v) {
nodeDLL<T>* newNode = new nodeDLL<Type>(NULL, v, head2);
if (head2 == NULL) {
head2 = tail2 = newNode;
}
else {
head2->prev2 = newNode;
head2 = newNode;
}
}
Main function
int main()
{
SinglyLinkedList<int>* list1;
DoublyLinkedList<int> lists;
lists.addToHead2(55);
lists.addToHead2(87);
lists.addToHead2(2);
//list1 = lists.Function();
cout <<lists.Function();
return 0;
}