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,...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
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...
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...
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...