Hopefully the format stays. I have the following program written in C++, which i
ID: 3723930 • Letter: H
Question
Hopefully the format stays. I have the following program written in C++, which is designed to take two input files with integers, turn them into linked lists, and then combine the two lists maintaining ascending order. It works, but I need to instead use a CLASS for Node (ListNode) as well as LinkedList. Can somebody please show me how I would adjust the below code to accomplish this?? Thanks in advance! (Happy to answer questions via comment).
struct Node {
int data;
Node* next;
};
class LinkedList
{
private:
Node * head;
public:
LinkedList();
void display() const;
void push_back(int);
void pop_front();
bool empty()const;
void insert_from_file_to_list(LinkedList &list1, string input_file1);
void merge_list_order(LinkedList &list1, LinkedList &list2);
};
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::push_back(int addData)
{
Node* n = new Node; //create new node
n->data = addData; //input data
n->next = NULL; //set node to point to NULL
if (head == NULL) {
head = n;
return;
}
else{
Node *temp = head;
// Uses temp to find the last node
while (temp->next != NULL) {
temp = temp->next;
}
// Appends the last node with last
temp->next = n;
}
}
void LinkedList::pop_front()
{
if (head != NULL)
{
Node* n = head;
head = head->next;
delete n;
}
}
void LinkedList::display() const
{
Node *disNode;
disNode = head;
cout << "Display Node" << endl;
while (disNode != NULL)
{
cout << disNode->data << " ";;
disNode = disNode->next;
}
cout << endl << endl;
}
bool LinkedList::empty() const
{
return (head == NULL);
}
void insert_from_file_to_list(LinkedList &list, string input_file) {
ifstream ifsInputFile(input_file.c_str());
int number;
if (ifsInputFile.is_open()) {
while (ifsInputFile >> number) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
list.push_back(number);
}
}
}
void LinkedList::merge_list_order(LinkedList &list1, LinkedList &list2) {
Node *nxt_node, *pre_node, *headA, *headB;
int dat;
headA = list1.head;
headB = list2.head;
head = nxt_node = pre_node = NULL;
while (headA != NULL && headB != NULL)
{
if (headA->data <= headB->data)
{
dat = headA->data;
headA = headA->next;
}
else
{
dat = headB->data;
headB = headB->next;
}
nxt_node = new Node;
nxt_node->data = dat;
nxt_node->next = NULL;
if (head == NULL)
head = nxt_node;
else
pre_node->next = nxt_node;
pre_node = nxt_node;
}
if (headA == NULL)
{
while (headB != NULL)
{
nxt_node = new Node;
nxt_node->data = headB->data;
nxt_node->next = NULL;
if (head == NULL)
head = nxt_node;
else
pre_node->next = nxt_node;
pre_node = nxt_node;
headB = headB->next;
}
}
else if (headB == NULL)
{
while (headA != NULL)
{
nxt_node = new Node;
nxt_node->data = headA->data;
nxt_node->next = NULL;
if (head == NULL)
head = nxt_node;
else
pre_node->next = nxt_node;
pre_node = nxt_node;
headA = headA->next;
}
}
while (!list1.empty())
list1.pop_front();
while (!list2.empty())
list2.pop_front();
}
int main() {
//read from number
LinkedList list1;
LinkedList list2;
LinkedList list3;
string input_file1 = "numbers1.txt";
string input_file2 = "numbers2.txt";
insert_from_file_to_list(list1, input_file1);
insert_from_file_to_list(list2, input_file2);
list1.display();
list2.display();
list3.merge_list_order(list1, list2);
cout << "After Merge, LinkedList 3 (Combined) is " << endl;
list3.display();
return 0;
}
Explanation / Answer
Modified the code to use ListNode class. Since you have kept all code in a single file, I have put the ListNode class also in the same file. Otherwise you will have total 5 files ListNode.h and .cpp files and LinkedList.h and .cpp file and main.cpp file.
Hope it helps
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class ListNode {
private:
int data;
ListNode* next;
public:
ListNode(int val)
{
data = val;
next = NULL;
}
ListNode(int val, ListNode* n)
{
data = val;
next = n;
}
int getData()
{
return data;
}
void setData(int v)
{
data = v;
}
ListNode* getNext()
{
return next;
}
void setNext(ListNode* n)
{
next = n;
}
};
class LinkedList
{
private:
ListNode * head;
public:
LinkedList();
void display() const;
void push_back(int);
void pop_front();
bool empty()const;
void insert_from_file_to_list(LinkedList &list1, string input_file1);
void merge_list_order(LinkedList &list1, LinkedList &list2);
};
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::push_back(int addData)
{
ListNode* n = new ListNode(addData, NULL); //create new node
if (head == NULL) {
head = n;
return;
}
else{
ListNode *temp = head;
// Uses temp to find the last node
while (temp->getNext() != NULL) {
temp = temp->getNext();
}
// Appends the last node with last
temp->setNext(n);
}
}
void LinkedList::pop_front()
{
if (head != NULL)
{
ListNode* n = head;
head = head->getNext();
delete n;
}
}
void LinkedList::display() const
{
ListNode *disNode;
disNode = head;
cout << "Display Node" << endl;
while (disNode != NULL)
{
cout << disNode->getData() << " ";;
disNode = disNode->getNext();
}
cout << endl << endl;
}
bool LinkedList::empty() const
{
return (head == NULL);
}
void insert_from_file_to_list(LinkedList &list, string input_file) {
ifstream ifsInputFile(input_file.c_str());
int number;
if (ifsInputFile.is_open()) {
while (ifsInputFile >> number) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
list.push_back(number);
}
}
}
void LinkedList::merge_list_order(LinkedList &list1, LinkedList &list2) {
ListNode *nxt_node, *pre_node, *headA, *headB;
int dat;
headA = list1.head;
headB = list2.head;
head = nxt_node = pre_node = NULL;
while (headA != NULL && headB != NULL)
{
if (headA->getData() <= headB->getData())
{
dat = headA->getData();
headA = headA->getNext();
}
else
{
dat = headB->getData();
headB = headB->getNext();
}
nxt_node = new ListNode(dat, NULL);
if (head == NULL)
head = nxt_node;
else
pre_node->setNext(nxt_node);
pre_node = nxt_node;
}
if (headA == NULL)
{
while (headB != NULL)
{
nxt_node = new ListNode(headB->getData(), NULL);
if (head == NULL)
head = nxt_node;
else
pre_node->setNext(nxt_node);
pre_node = nxt_node;
headB = headB->getNext();
}
}
else if (headB == NULL)
{
while (headA != NULL)
{
nxt_node = new ListNode(headA->getData(), NULL);
if (head == NULL)
head = nxt_node;
else
pre_node->setNext(nxt_node);
pre_node = nxt_node;
headA = headA->getNext();
}
}
while (!list1.empty())
list1.pop_front();
while (!list2.empty())
list2.pop_front();
}
int main() {
//read from number
LinkedList list1;
LinkedList list2;
LinkedList list3;
string input_file1 = "numbers1.txt";
string input_file2 = "numbers2.txt";
insert_from_file_to_list(list1, input_file1);
insert_from_file_to_list(list2, input_file2);
list1.display();
list2.display();
list3.merge_list_order(list1, list2);
cout << "After Merge, LinkedList 3 (Combined) is " << endl;
list3.display();
return 0;
}
output
=====
Display Node
1 3 5 6
Display Node
2 4 5 8
After Merge, LinkedList 3 (Combined) is
Display Node
1 2 3 4 5 5 6 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.