-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtPod.cpp
More file actions
191 lines (167 loc) · 4.42 KB
/
UtPod.cpp
File metadata and controls
191 lines (167 loc) · 4.42 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Created by willworthington on 10/28/2018.
//
#include <iostream>
#include <string>
#include <time.h>
#include "UtPod.h"
#include "Song.h"
using namespace std;
UtPod::UtPod() {
songs = NULL;
memSize = MAX_MEMORY;
}
UtPod::UtPod(int size) {
songs = NULL;
if ((size<=0)||(size>512)){
memSize = MAX_MEMORY;
}
else{
memSize = size;
}
}
int UtPod::addSong(Song const &s){
if ((getRemainingMemory()-s.getSize())<=0){
return NO_MEMORY;
}
else {
SongNode *tmp = new SongNode;
tmp->next=songs;
tmp->s = s;
songs=tmp;
return SUCCESS;
}
}
int UtPod::removeSong(Song const &s){
SongNode *tmp = songs;
//if the first node holds the song we're looking for
if (songs->s==s){
if (songs->next==NULL){
songs = NULL;
}
else{
SongNode *found = songs;
songs=songs->next;
delete found;
}
return SUCCESS;
}
//if any node after the first holds the song we're looking for
else {
while (tmp->next != NULL){
if (tmp->next->s==s){
SongNode *found = tmp->next;
tmp->next=tmp->next->next;
delete found;
return SUCCESS;
}
else{
tmp = tmp->next;
}
}
}
//if the song is never found in UtPod
return NOT_FOUND;
}
void UtPod::shuffle(){
int length = getNumSongs();
if (length < 2){
//Don't do shit :)
}
else {
srand(time(NULL));
int s1, s2;
SongNode *tmp1, *tmp2;
//int s2;
//SongNode *tmp2;
for (int iter=0; iter<2*length; iter++){
tmp1 = songs;
tmp2 = songs;
s1 = rand() % length;
s2 = rand() % length;
for (int i=0; i<s1; i++){
tmp1=tmp1->next;
}
for (int i=0; i<s2; i++){
tmp2=tmp2->next;
}
tmp1->s.swap(tmp2->s);
}
//cout << "Index1: " << s1 << ", Song1: " << tmp1->s.getTitle() << endl;
//cout << "Index2: " << s2 << ", Song2: " << tmp2->s.getTitle() << endl;
}
}
void UtPod::showSongList(){
SongNode *tmp = songs;
while (tmp != NULL){
cout << tmp->s.getTitle() << ", "
<< tmp->s.getArtist() << ", "
<< tmp->s.getSize() << " MB" << endl;
tmp=tmp->next;
}
}
void UtPod::sortSongList(){
if (getNumSongs()<2){
//Does not need to be sorted
//Don't need to do anything :)
}
else{
while (bubbleSort());
}
//songs->s.swap(songs->next->s);
}
bool UtPod::bubbleSort(){
bool swapped = false;
SongNode *tmp = songs;
while (tmp->next != NULL){
if (tmp->s>tmp->next->s){
tmp->s.swap(tmp->next->s);
swapped = true;
tmp=tmp->next;
}
else {
tmp=tmp->next;
}
}
return swapped;
//return false;
}
void UtPod::clearMemory(){
if (songs==NULL){
//UtPod is already empty
//Don't need to do anything :)
}
else{
while (songs->next != NULL){
removeSong(songs->next->s);
}
removeSong(songs->s);
}
}
int UtPod::getNumSongs(){
int length = 0;
if (songs==NULL){
return 0;
}
else {
SongNode *tmp = songs;
while (tmp!=NULL){
length++;
tmp = tmp->next;
}
return length;
}
}
//return memSize minus sum of size of all songs in memory
int UtPod::getRemainingMemory(){
int remaining = memSize;
SongNode *tmp = songs;
while (tmp != NULL){
remaining-=tmp->s.getSize();
tmp = tmp->next;
}
return remaining;
}
UtPod::~UtPod(){
clearMemory();
}