Question

I have a problem with merging the two linked lists together. In the function AscendMerge, I...

I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values.

#include <iostream>
#include <cassert>
using namespace std;
struct nodeType
{
   int info;
   nodeType *link;
   nodeType *current;
   nodeType *prev;
};

class linkedListType
{
public:

   nodeType *first;
   nodeType* head;
   nodeType * end;
   nodeType *last;
   int count;
   void print() const;
   void insertList1(const int newItem);
   void insertList2(const int newItem);
   void printList2()const;
   void AscendMerge();
   linkedListType();
};
linkedListType::linkedListType() //default constructor
{
   first = NULL;
   last = NULL;
   count = 0;
}

void linkedListType::print() const
{
   nodeType *current; //pointer to traverse the list

   current = first; //set current so that it points to
                       //the first node
   while (current != NULL) //while more data to print
   {
       cout << current->info << " ";
       current = current->link;
   }
}//end print

void linkedListType::AscendMerge()
{
   nodeType * c1;
   nodeType * c2;
   nodeType * p1;
   nodeType * p2;
   c1 = first;
   c2 = head;
   p1 = first;
   p2 = head;
   while (head->link&&first->link != NULL)
   {
       if (c2->info<c1->info)
       {
           c2->link = p1->link;
          
       }
       else
       {
           c1->link=p2->link;
       }
       p1 = p1->link;
       p2 = p2->link;
       c1 = c1->link;
       c2 = c2->link;

       cout << c2->info<< " ";
   }

}
void linkedListType::insertList1(const int newItem)
{
   int count = 0;
   if (first == NULL)
   {
       first = new nodeType;
       first->info = newItem;
       first->link = NULL;
       last = first;
       count++;
   }
   else {
       last->link = new nodeType;
       last->link->info = newItem;
       last->link->link = NULL;
       last = last->link;
       count++;
   }
}

void linkedListType::insertList2(const int newItem)
{
   int count = 0;
   if (head == NULL)
   {
       head = new nodeType;
       head->info = newItem;
       head->link = NULL;
       end = head;
       count++;
   }
   else {
       end->link = new nodeType;
       end->link->info = newItem;
       end->link->link = NULL;
       end = end->link;
       count++;
   }
}

void linkedListType::printList2()const
{
   nodeType *current; //pointer to traverse the list

   current = head; //set current so that it points to
                       //the first node
   while (current != NULL) //while more data to print
   {
       cout << current->info << " ";
       current = current->link;
   }
}

//Implementation//

#include "Merge.h"
#include <iostream>
using namespace std;

int main()
{
   linkedListType list;
   int num;
   bool found = false;

   cout << "Enter numbers ending with -999" << endl;
   cin >> num;

   while (num != -999)
   {
       list.insertList1(num);
       cin >> num;
   }

   cout << endl;
   cout << "Enter numbers for second list ending with -999" << endl;
   cin >> num;
   while (num != -999)
   {
       list.insertList2(num);
       cin >>num;
   }
   cout << "List1: ";
   list.print();
   cout << endl;
   cout << "List2: ";
   list.printList2();
   cout << endl;
   list.AscendMerge();
  
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

when you initialized first and head to both c1 and p2, the values are same. This is because, we just initialized one variable first and head in the class and hence when to assign that head or first same value is copied in both. If you want to get two lists then you need to pass one list as argument and then append the values based on the condition. I wrote the new code. You can check this:

//Main.cpp

#include "Merge.h"
#include <iostream>
using namespace std;

int main()
{
linkedListType list1,list2;
int num;
bool found = false;

cout << "Enter numbers ending with -999" << endl;
cin >> num;

while (num != -999)
{
list1.insertList(num);
cin >> num;
}

cout << endl;
cout << "Enter numbers for second list ending with -999" << endl;
cin >> num;
while (num != -999)
{
list2.insertList(num);
cin >>num;
}
cout << "List1: ";
list1.print();
cout << endl;
cout << "List2: ";
list2.print();
cout << endl;
list1.AscendMerge(list2);
return 0;
}

//Merge.h

#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
nodeType *current;
nodeType *prev;
};

class linkedListType
{
public:

nodeType *first;
nodeType* head;
nodeType * end;
nodeType *last;
int count;
void print() const;
void insertList(const int newItem);
void AscendMerge(linkedListType l);

linkedListType();
};
linkedListType::linkedListType() //default constructor
{
first = NULL;
last = NULL;
count = 0;
}

void linkedListType::print() const
{
nodeType *current; //pointer to traverse the list

current = first; //set current so that it points to
//the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print

void linkedListType::AscendMerge(linkedListType l)

{
this->print();
l.print();
nodeType * c2;
nodeType * p2;
linkedListType newList;
c2 = this->first;
p2 = l.first;
while (c2!=NULL && p2!=NULL)
{
if (c2->info < p2->info)
{
newList.insertList(c2->info);
c2 = c2->link;
}
else if(c2->info > p2->info)
{
newList.insertList(c2->info);
p2 = p2->link;
}
else
{
newList.insertList(c2->info);
newList.insertList(p2->info);
c2 = c2->link;
p2 = p2->link;
}
}
while(c2!=NULL)
{
newList.insertList(c2->info);
c2 = c2->link;
}
while(p2!=NULL)
{
newList.insertList(c2->info);
p2 = p2->link;
}
newList.print();
}
void linkedListType::insertList(const int newItem)
{
int count = 0;
if (first == NULL)
{
first = new nodeType;
first->info = newItem;
first->link = NULL;
last = first;
count++;
}
else {
last->link = new nodeType;
last->link->info = newItem;
last->link->link = NULL;
last = last->link;
count++;
}
}

Add a comment
Know the answer?
Add Answer to:
I have a problem with merging the two linked lists together. In the function AscendMerge, I...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would you get this lab to work with the numbers 37 14 68 47, the...

    How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question. Code: #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT