Write the code in c++ and show the output. Singly Linked Lists and Recursion 1.
ID: 3700420 • Letter: W
Question
Write the code in c++ and show the output.
Singly Linked Lists and Recursion
1. Create a class 'node' which is composed of two members: a letter and a number.
2. Build a singly-linked list by reading from 'fileA'. A node should be or contain
a node object, the implementation is up to you.
3. Build a separate linked list by reading from 'fileB'.
4. Create a recursive print function which prints your lists in the following format:
10A -> 9A -> 8A ->... -> 1A
and then
10B -> 9B -> 8B ->... -> 1B
Meaning that your function will print the list in reverse.
Note: Your recursive function must not alter the list in any way.
5. Merge the two lists in the following format and then display the full list
using the print function you already made:
10A -> 10B-> 9A -> 9B -> 8A -> 8B ->... -> 1A -> 1B
You may choose to receive 16/20 by only merging the two lists in the
following format:
10A -> 9A -> 8A ->...1A -> 10B -> 9B -> 8B ->...->1B
FileA-1.txt
1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A
FileB-1.txt
1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B
Explanation / Answer
EXECUTABLE CODE
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Node{
int number;
char letter;
Node *next;
};
void print(Node *node)
{
if(node == NULL)
return;
print(node->next);
if(node->next == NULL)
cout<<node->number<<node->letter;
else
cout<<"->"<<node->number<<node->letter;
}
Node insert(int num,char letter,Node head)
{
Node *newNode = new Node;
newNode->number =num;
newNode->letter = letter;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
Node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
return head;
}
Node mergeList(Node headA, Node*headB)
{
Node *merge =NULL;
Node *tempA = headA;
Node *tempB = headB;
while(tempA != NULL && tempB != NULL)
{
if(tempA->number < tempB->number && tempA->letter < tempB->letter)
{
merge = insert(tempA->number,tempA->letter,merge);
tempA = tempA->next;
}
else{
merge = insert(tempB->number,tempB->letter,merge);
tempB = tempB->next;
}
}
while(tempA != NULL)
{
merge = insert(tempA->number,tempA->letter,merge);
tempA = tempA->next;
}
while(tempB != NULL)
{
merge = insert(tempB->number,tempB->letter,merge);
tempB = tempB->next;
}
return merge;
}
int main() {
ifstream fileA("fileA.txt");
ifstream fileB("fileB.txt");
Node *headA = NULL;
Node *headB = NULL;
int num;
char letter;
if(fileA.is_open() && fileB.is_open())
{
while(!fileA.eof())
{
fileA>>num>>letter;
if(fileA.eof())
break;
headA=insert(num,letter,headA);
}
print(headA);
while(!fileB.eof())
{
fileB>>num>>letter;
if(fileB.eof())
break;
headB=insert(num,letter,headB);
}
cout<<endl;
print(headB);
cout<<endl;
Node *merge = mergeList(headA,headB);
print(merge);
}else
cout<<" Unable to open file(s)";
fileA.close();
fileB.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.