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;
}
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++;
}
}
I have a problem with merging the two linked lists together. In the function AscendMerge, I...
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 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 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 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 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 =...
) Given the following unorderedLinkedList class, provide an implementation for the member function reverse. When the reverse function is called on an unorderedLinkedList object called mylist (e.g. mylist.reverse()), it will create a new unorderedLinkedList and insert the elements of mylist into the new list in reverse order and then return the new list. struct nodeType { int info; nodeType *link; }; class unorderedLinkedList { protected: nodeType *first; //pointer to the first node of the list public: ...
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 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)...
This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...
The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...