Question

The output only produces the values from the first linkedlist and not the second. Please fix...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT