The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help!
Objective:
Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8, 8, 5, 4, 8, 5, 9}$. In other words, to create the interleaved list, first add a node from L1, then one from L2, and then repeat as many times as necessary. If there are any nodes left over in L1 or L2 exclusively, append them to the end of the list.
DoublyLinkedList.hpp:
#ifndef DOUBLY_LINKED_LIST_HPP #define DOUBLY_LINKED_LIST_HPP #include "DoubleNode.hpp" #include <string> template <typename ItemType> class DoublyLinkedList { public: DoublyLinkedList(); DoublyLinkedList(const DoublyLinkedList<ItemType>& a_bag); virtual ~DoublyLinkedList(); bool insert(const ItemType &item, const int &position); //inserts item at position in caller list bool remove(const int &position); //removes the node at position int getSize() const; // returns the number of the nodes in the calling list DoubleNode<ItemType> *getHeadPtr() const; // returns a copy of the headPtr DoubleNode<ItemType> *getAtPos(const int &pos) const; // returns a pointer to the node located at pos bool isEmpty() const; // returns whether the calling list is empty void clear(); // clears the list int getIndexOf(const ItemType &item) const; // returns the position of the given item in the list, -1 otherwise void display() const; // prints the contents of the calling list in order void displayBackwards() const; // prints the contents of the calling list in reverse order DoublyLinkedList<ItemType> interleave(const DoublyLinkedList<ItemType> &a_list); // returns the interleaved list of the calling and parameter lists protected: DoubleNode<ItemType>* head_ptr_; int item_count_; }; #include "DoublyLinkedList.cpp" #endif
Code (C++):
template DoublyLinkedList DoublyLinkedList:: interleave(const DoublyLinkedList &a_list) // returns the interleaved list of the calling and parameter lists { // create a new list to return DoublyLinkedList* output = new DoublyLinkedList(); int index = 1; // index of node to be added at position in new list // add each node from both list at interval of 1 // create iterators to loop though lists DoubleNode* list1 = head_ptr_; DoubleNode* list2 = a_list.head_ptr_; // loop till any list have node left to add while (list1 != nullptr && list2 != nullptr) { // if first list is not done adding add a node from it if (list1 != nullptr) { output->insert(list1->getItem(),index); list1 = list1->getNext(); index++; } // if second list is not done adding add a node from it if (list2 != nullptr) { output->insert(list2->getItem(),index); list2 = list2->getNext(); index++; } } return* output; }
This is the code for your query. You were increasing index straightaway with each link. You had to do it at the end of loop
DoublyLinkedList DoublyLinkedList:: interleave(const DoublyLinkedList &a_list)
// returns the interleaved list of the calling and parameter lists
{
// create a new list to return
DoublyLinkedList* output = new DoublyLinkedList();
int index = 1; // index of node to be added at position in new list
// add each node from both list at interval of 1
// create iterators to loop though lists
DoubleNode* list1 = head_ptr_;
DoubleNode* list2 = a_list.head_ptr_;
// loop till any list have node left to add
while (list1 != nullptr && list2 != nullptr) {
//if first list is not done adding add a node from it
output->insert(list1->getItem(),index);
list1 = list1->getNext();
// if second list is not done adding add a node from it
output->insert(list2->getItem(),index);
list2 = list2->getNext();
index++;
}
// when items are remaining in list 2
if(list1 ==nullptr && list2!=nullptr) {
while(list2!=nullptr){
output->insert(list2->getItem(),index);
list2 = list2->getNext();
index++;
}
}
// when items are remaining in list 1
if(list1!=nullptr && list2==nullptr){
while(list1!=nullptr){
output->insert(list1->getItem(),index);
list1 = list1->getNext();
index++;
}
}
return* output;
}
KINDLY UPVOTE. ? IT'D REALLY MEAN A LOT
Get Answers For Free
Most questions answered within 1 hours.