-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeating-playlist.cpp
More file actions
85 lines (74 loc) · 1.78 KB
/
repeating-playlist.cpp
File metadata and controls
85 lines (74 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file repeating-playlist.cpp
* @author nirmeet baweja
* @brief A playlist is considered a repeating playlist if any of the songs
* contain a pointer to a previous song in the playlist. Otherwise, the
* playlist will end with the last song which points to NULL.
*
* Implement a function isRepeatingPlaylist that, efficiently with respect to
* time used, returns true if a playlist is repeating or false if it is not.
*
* @version 0.1
* @date 2022-02-16
*
* @copyright Copyright (c) 2022
*
*/
#include <stdexcept>
#include <iostream>
#include <string>
#include <vector>
bool vector_includes_name(const std::vector<std::string> &names, std::string name)
{
for (std::string i : names)
{
if (i == name)
{
return true;
}
}
return false;
}
class Song
{
public:
Song(std::string name) : name(name), nextSong(NULL) {}
void next(Song *song)
{
this->nextSong = song;
}
bool isRepeatingPlaylist()
{
std::cout << "Checking is repeating\n";
std::cout << this->name << '\n';
static std::vector<std::string> previousSongs;
Song *currentSong = this;
while (currentSong->nextSong != NULL)
{
std::cout << "current song: " << currentSong->name << '\n';
if (vector_includes_name(previousSongs, currentSong->name))
{
return true;
}
previousSongs.push_back(currentSong->name);
currentSong = currentSong->nextSong;
}
return false;
}
private:
const std::string name;
Song *nextSong;
Song *lastSong;
};
#ifndef RunTests
int main()
{
Song *first = new Song("Hello");
Song *second = new Song("Eye of the tiger");
Song *third = new Song("Some song");
first->next(second);
second->next(third);
third->next(first);
std::cout << std::boolalpha << first->isRepeatingPlaylist();
}
#endif