Write the reverse print function using a recursive method. Coding in C++ Overvie
ID: 3736463 • Letter: W
Question
Write the reverse print function using a recursive method. Coding in C++
Overview 1. A Cryptocurrency wallet is composed of a user ID (String: UID e.g. “A84671", "A49583"), a USD value ranging from 0 to $10,000 (Int: VALUE), and a small hash number (Int: HASH) which is always 5 digits long. 2. Write a program which creates a linked list of 30 nodes, where each node holds a wallet. Generate the inputs randomly (UID can be all ints or a mix or ints and chars), and output the list to a file “walletLinkedlist.txt" in the following format: UID HASH VALUE 3. Implement the function "returnMiddle” which prints out the item in the middle of the list and print it to the screen. 4. Finally, implement a function which splits the list in half and outputs the result in a second file "secondFile.txt" in the following format UID HASH VALUE list one * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * UID HASH VALUE list twoExplanation / Answer
Hello, I have already answered the same question once. So I’m referring my own solution here. I assure you this will work perfectly.
//main.cpp
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<sstream>
#include<fstream>
using namespace std;
struct Wallet{
/**a data structure to represent one Wallet */
string UID;
int value;
int hash;
};
struct node{
/**a data structure to represent one node in the list */
Wallet data;
node *next;
};
class LinkedList{
node *head,*tail;
int count; //keeps the number of elements currently in the list
public:
LinkedList(){
head=NULL;
tail=NULL;
count=0;
}
/*method to add a wallet to the list*/
void insert(Wallet w){
node *nod = new node;
nod->data = w;
nod->next = NULL;
if(head == NULL){
head = nod;
tail = nod;
}
else{
tail->next = nod;
tail = tail->next;
}
count++; //incrementing the count
}
//method to display the list and save data to a file
void displayAndSaveToFile(){
/*defining the output file*/
fstream outputFile("walletLinkedList.txt",ios::out);
outputFile<<"UID USD HASH ";
cout<<"List:"<<endl;
node *nod=head;
/*loops until all nodes have been traversed*/
while(nod!=NULL){
Wallet w=nod->data;
//displaying on the screen
cout<<w.UID<<" "<<w.value<<" "<<w.hash<<endl;
//saving to the file
outputFile<<w.UID<<" "<<w.value<<" "<<w.hash<<endl;
nod=nod->next;
}
//closing the file
outputFile.close();
cout<<"Saved data to a file walletLinkedList.txt successfully!"<<endl;
}
/*method to display the wallet at the middle*/
void returnMiddle(){
int middle=count/2;
node *nod=head;
int i=1;
while(i<middle){
nod=nod->next;
i++;
}
Wallet w=nod->data;
cout<<"Wallet at the middle: ";
cout<<w.UID<<" "<<w.value<<" "<<w.hash<<endl;
}
/*method to split the list into two and save to a file*/
void splitAndSave(){
int middle=count/2;
int i=1;
fstream outputFile("secondFile.txt",ios::out);
outputFile<<"UID USD HASH list one ";
node *nod=head;
while(nod!=NULL){
Wallet w=nod->data;
outputFile<<w.UID<<" "<<w.value<<" "<<w.hash<<endl;
nod=nod->next;
if(i==middle){
/*middle has been reached, adding a seperator*/
outputFile<<"*************************"<<endl;
outputFile<<"UID USD HASH list two ";
}
i++;
}
outputFile.close();
cout<<"Divided the list into two and saved to a file secondFile.txt successfully!"<<endl;
}
};
int main(){
//seeding the random number generator
srand(time(NULL));
//creating a Linked list
LinkedList list;
/**looping for 30 times, creating a wallet with random values, adding to the list*/
for(int i=0;i<30;i++){
Wallet w;
stringstream uid;
//generating a random letter
char c=(char)'A'+rand()%25;
uid<<c;
//generating a 5 digit number
int id=(10000+rand()%90000);
uid<<id;
//mixed the letter and 5 digit number to form a UID
w.UID=uid.str();
//generating a value between 0-10000
w.value=rand() % 10000;
//generating a 5 digit hash
w.hash=10000+rand()%90000;
//adding to the list
list.insert(w);
}
//displaying the list and saving to a file
list.displayAndSaveToFile();
cout<<endl;
//displaying the wallet at the middle
list.returnMiddle();
//dividing the list into two and saving to a file
list.splitAndSave();
return 0;
}
/*OUTPUT*/
List:
P39690 1534 40630
A38552 852 27093
P22510 1425 21961
Q42008 8971 29103
Q12743 1780 19792
B20400 7470 29349
N18991 721 39067
V21552 6188 29459
D30543 6472 17453
U20286 611 37574
M37991 3836 10815
Y14803 9338 18304
Y41547 9233 38717
L12477 1159 27000
L14305 2612 15944
N36123 7321 36824
L17060 675 32178
K19685 5728 15678
N38691 9745 29573
O19297 8624 23611
J20866 1652 25085
Y18863 1602 35799
C34296 2022 25609
A18238 7331 15371
R16720 2663 29134
C10836 8290 39493
P22889 2505 13604
L14474 6998 19944
M36277 5410 11799
W24397 1398 33993
Saved data to a file walletLinkedList.txt successfully!
Wallet at the middle: L14305 2612 15944
Divided the list into two and saved to a file secondFile.txt successfully!
// walletLinkedList.txt after running program
UID USD HASH
P39690 1534 40630
A38552 852 27093
P22510 1425 21961
Q42008 8971 29103
Q12743 1780 19792
B20400 7470 29349
N18991 721 39067
V21552 6188 29459
D30543 6472 17453
U20286 611 37574
M37991 3836 10815
Y14803 9338 18304
Y41547 9233 38717
L12477 1159 27000
L14305 2612 15944
N36123 7321 36824
L17060 675 32178
K19685 5728 15678
N38691 9745 29573
O19297 8624 23611
J20866 1652 25085
Y18863 1602 35799
C34296 2022 25609
A18238 7331 15371
R16720 2663 29134
C10836 8290 39493
P22889 2505 13604
L14474 6998 19944
M36277 5410 11799
W24397 1398 33993
// secondFile.txt after running program
UID USD HASH list one
P39690 1534 40630
A38552 852 27093
P22510 1425 21961
Q42008 8971 29103
Q12743 1780 19792
B20400 7470 29349
N18991 721 39067
V21552 6188 29459
D30543 6472 17453
U20286 611 37574
M37991 3836 10815
Y14803 9338 18304
Y41547 9233 38717
L12477 1159 27000
L14305 2612 15944
*************************
UID USD HASH list two
N36123 7321 36824
L17060 675 32178
K19685 5728 15678
N38691 9745 29573
O19297 8624 23611
J20866 1652 25085
Y18863 1602 35799
C34296 2022 25609
A18238 7331 15371
R16720 2663 29134
C10836 8290 39493
P22889 2505 13604
L14474 6998 19944
M36277 5410 11799
W24397 1398 33993
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.