//How do I make the program recursive instead of iterative? My input file (for recursive) and (iterative) code are below. This is my first time using this website so I don't know how I am supposed to ask the questions. I'm also very beginner in code, Sorry.
BTS, DYNAMITE, 3.43, 41000000
NF, CLOUDS, 1.14, 869000
HARRY, FALLING, 3.27, 30000000
RACHEL, FIGHT SONG, 3.23, 166000
RUTH, LOST BOY, 4.33, 65000
JAMES, SAY YOU WONT LET GO, 3.27 213000
HARRY, SIGN OF THE TIMES, 5.41, 63000000
IDENA, LET IT GO, 3.21, 21000000
BTS, BLACK SWAN, 3.43, 23000000
PASSENGER, LET HER GO, 4.20, 97000
KATIE, DAISIES, 2.53, 12000
BEBE, SACRIFICES, 2.40, 300000
ANDY, DONT GIVE UP ON ME, 3.16, 5600000
JUSTIN, BIGGER THAN, 3.28, 21000
SAM, IM READY, 3.20, 30500
ALEC, OH MY GOD, 3.07, 1000000
ANDY, GOOD EXAMPLE, 2.29, 15000
GREYSON, GOOD AS GOLD, 3.25, 78000
BRYNN, TELL ME IM PRETTY, 3.08, 29000
GRYFFIN, CRY, 3.38, 3000000
KHALID, FREE SPIRIT, 3.02, 55000
KYGO, LIKE IT IS, 3.03, 890000
CHAINSMOKERS, THIS FEELING, 3.17, 760000
KASKADE, WITH YOU, 3.00, 2000
LAUV, LIKE ME BETTER, 3.17, 5000000
AVA, SALT, 3.00, 56900
LEWIS, BEFORE YOU GO, 3.50, 89000
LAUV, NEVER NOT, 4.02, 76000
JEREMY, ALWAYS ILL CARE, 2.40, 68000
JAMES, IMPOSSIBLE, 4.07, 49000
GMILLER, NO TEARS LEFT TO CRY, 3.10, 770000
<pre>
#pragma once
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
using namespace std;
class Song
{
char *title;
char *artist;
char *album;
char *duration;
public:
Song();
Song(char *t, char *a, char *du, char *al);
char *getTitle();
char *getArtist();
char *getAlbum();
char *getDuration();
void setTitle(char *);
void setArtist(char *);
void setDuration(char *du);
void setAlbum(char *);
~Song();
};
#include"Song.h"
Song::Song()
{
title = NULL;
artist = NULL;
album = NULL;
duration = NULL;
}
Song::Song(char *t, char *a, char *du, char *al)
{
title = new char[strlen(t)+1];
artist = new char[strlen(a)+1];
album = new char[strlen(al)+1];
duration = new char[strlen(du)+1];
strcpy(title, t);
strcpy(artist, a);
strcpy(album, al);
strcpy(duration, du);
}
Song::~Song()
{
delete title;
delete artist;
delete album;
delete duration;
}
char *Song::getTitle()
{
return title;
}
char *Song::getArtist()
{
return artist;
}
char *Song::getAlbum()
{
return album;
}
char *Song::getDuration()
{
return duration;
}
void Song::setArtist(char *a)
{
if(artist != NULL)
delete artist;
artist = new char[strlen(a) + 1];
strcpy(artist, a);
}
void Song::setTitle(char *t)
{
if(title != NULL)
delete title;
title = new char[strlen(t) + 1];
strcpy(title, t);
}
void Song::setDuration(char *du)
{
if(duration != NULL)
delete duration;
duration = new char[strlen(du) + 1];
strcpy(duration, du);
}
void Song::setAlbum(char *al)
{
if(album != NULL)
delete album;
album = new char[strlen(al) + 1];
strcpy(album, al);
}
#pragma once
#define _CRT_SECURE_NO_DEPRECATE
#include "Song.h"
struct Node
{
Song *song;
Node *next, *prev;
};
class SongList
{
private:
Node *head, *tail;
public:
SongList();
void insert(Song s);
void display();
~SongList();
};
#include "SongList.h"
SongList::SongList()
{
head = NULL;
tail = NULL;
}
void SongList::insert(Song s)
{
Node *node = new Node;
node->song = new Song(s.getTitle(), s.getArtist(), s.getDuration(), s.getAlbum());
node->next = NULL;
node->prev = NULL;
if(head == NULL)
{
head = node;
tail = node;
}
else
{
Node *curr = head;
while(curr != NULL)
{
if(strcmp(curr->song->getTitle(), s.getTitle()) > 0)
break;
curr = curr->next;
}
if(curr == head)
{
node->next = head;
head->prev = node;
head = node;
}
else if(curr == NULL)
{
tail->next = node;
node->prev = tail;
tail = node;
}
else
{
node->next = curr;
node->prev = curr->prev;
curr->prev->next = node;
curr->prev = node;
}
}
}
void SongList::display()
{
if(head == NULL)
{
cout<<"Empty List"<
}else
{
cout<<"Songs: "<
Node *curr = head;
while(curr != NULL)
{
cout<<"Title: "getTitle()<
cout<<"Artist: "getArtist()<
cout<<"Album: "getAlbum()<
cout<<"Duration: "getDuration()<
curr = curr->next;
}
}
}
SongList::~SongList()
{
Node *temp = NULL;
while(head != NULL)
{
temp = head;
head = head->next;
temp->next = NULL;
temp->prev = NULL;
delete(temp->song);
delete(temp);
temp = NULL;
}
head = NULL;
tail = NULL;
}
#include
#include
#include
#include "SongList.h"
using namespace std;
int main()
{
ifstream in;
SongList songList;
in.open("songs.txt");
if(in.fail())
{
cout << "Not able to open the input file songs.txt" << endl;
return -1;
}
string t, a, al,du;
char *tl,*ar,*alm,*d;
while (!in.eof())
{
getline(in, t, ';');
getline(in, a, ';');
getline(in, du, ';');
getline(in, al, '\n');
tl = new char[t.length() + 1];
ar = new char[a.length() + 1];
alm = new char[al.length() + 1];
d = new char[du.length() + 1];
strcpy(tl, t.c_str());
strcpy(ar, a.c_str());
strcpy(alm, al.c_str());
strcpy(d, du.c_str());
songList.insert(Song(tl, ar, d, alm));
}
songList.display();
return 0;
}
What I have tried:
I read the book and I wrote the iterative code above but I'm confused on how to make it recursive. I feel extremely dumb and I have no idea what I'm supposed to do.