-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtPod.cpp
268 lines (201 loc) · 7.1 KB
/
UtPod.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// UtPod.cpp
//
//
// Created by David Fernandez and Adrian Melo on 10/28/18.
//
#include <stdio.h>
#include "UtPod.h"
#include "song.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
UtPod::UtPod(){
memSize = MAX_MEMORY; //Not sure if we should be using memSize
}
UtPod:: UtPod(int size){
if(size>MAX_MEMORY || size<=0){
memSize = MAX_MEMORY;
}else{
memSize = size;
}
}
/* FUNCTION - int addSong
* attempts to add a new song to the UtPod
o returns a 0 if successful
o returns -1 if not enough memory to add the song
precondition: s is a valid Song
input parms - Song s
output parms - 0 for a successful add, -1 for unsuccessful add
*/
int UtPod::addSong(Song const &s2) {
SongNode *temp = songs;
if ((UtPod::getRemainingMemory()) >= s2.getSize()) {
//add song to beginning;
SongNode *newSong = new SongNode; //create newSong node
newSong->s = s2;
if(temp == NULL){
songs = newSong;
songs->next = NULL;
return SUCCESS;
}else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newSong;
newSong->next = NULL;
return SUCCESS;
}
}
return NO_MEMORY;
}
/* FUNCTION - int removeSong
* attempts to remove a song from the UtPod
* removes the first instance of a song that is in the the UtPod multiple times
o returns 0 if successful
o returns -1 if nothing is removed
input parms - Song s
output parms - 0 for a successful remove, -1 for unsuccessful remove -2 for Song Not found
*/
int UtPod::removeSong(Song const &s){
SongNode *prev;
SongNode *temp;
temp = songs;
prev = songs;
while(temp != NULL){
if(temp->s == s) {
if (temp == songs) {
songs = temp->next;
}
prev->next = temp->next;
delete (temp);
return SUCCESS;
}
prev = temp;
temp = temp->next;
}
return NOT_FOUND;
}
/* FUNCTION - void shuffle
* shuffles the songs into random order
o will do nothing if there are less than two songs in the current list
input parms - None
output parms - None
*/
void UtPod::shuffle(){
Song songTemp("","", 0);
SongNode *point1;
SongNode *point2;
SongNode *temp = songs;
int count =0;
while(temp != NULL){
temp = temp->next;
count++;
}
//Condition for if there are less than two songs in the list
if(count > 2){
unsigned int currentTime = (unsigned)time(0);
srand(currentTime); //seed random time to rand number generator
for(int i=0; i< 2*count ; i++) {
int rndBound1 = (rand() % count); //see random numbers
int rndBound2 = (rand() % count);
point1 = songs; //set pointers to the beginning of the song list
point2 = songs;
if(point1->next == NULL || point1->next->next == NULL){
//don't shuffle
}else {
for (int j = 0; j < rndBound1; j++) { //find a random node
point1 = point1->next;
}
for (int k = 0; k < rndBound2; k++) { //find a random node to swap with.
point2 = point2->next;
}
songTemp = point1->s;
point1->s = point2->s;
point2->s = songTemp;
}
}
}
}
/* FUNCTION - void showSongList
* prints the current list of songs in order from first to last to standard output
* format - Title, Artist, size in MB (one song per line)
input parms - None
output parms - None
*/
void UtPod::showSongList(){
SongNode *temp;
temp = songs;
while(temp != NULL){
cout << temp->s.getTitle()<< ", " << temp->s.getArtist()<< ", "<< temp->s.getSize() << "MB" << endl;
temp = temp->next;
}
}
/* FUNCTION - void sortSongList
* sorts the songs in ascending order
o will do nothing if there are less than two songs in the current list
input parms - NONE
output parms - NONE
*/
void UtPod::sortSongList() {
Song temp;
SongNode *point1;
SongNode *point2;
bool swap;
// Bubble Sort Algorithm //
if(getRemainingMemory() != getTotalMemory()){
do {
swap = false; //NO swap has been made ... yet
point1 = songs;
point2 = point1->next; //set pointers to beginning nodes of the list
while (point2 != NULL) {
if (point1->s < point2->s) { //if the first song is higher than the second song
temp = point1->s; //SWAP
point1->s = point2->s;
point2->s = temp;
swap = true; //set SWAP to true
point1 = point2;
point2 = point2->next;
} else {
point1 = point1->next; //otherwise don't swap and move to next 2 nodes
point2 = point2->next;
}
}
} while (swap); //While a swap has been made, the list may not be completely sorted so keep going
}}
/* FUNCTION - void clearMemory
* clears all the songs from memory
input parms - None
output parms - None
*/
void UtPod::clearMemory(){
SongNode *temp;
SongNode *next;
temp = songs;
while(temp != NULL){ //Loop through the whole linked list deleting one node at a time
next = temp->next;
delete(temp);
temp = next;
}
songs = temp; //Comment out this and show, is MEMORY leaking or does
}
/* FUNCTION - int getRemainingMemory
* returns the amount of memory available for adding new songs
input parms - None
output parms - Outputs the amount of memory remaining in the UtPod
*/
int UtPod::getRemainingMemory(){
int usedMem = 0;
SongNode *temp = songs;
while(temp != NULL){
usedMem += temp->s.getSize();
temp = temp->next;
}
return ((UtPod::getTotalMemory()) - usedMem); //return the total mem - used mem
}
//Destructor for the UtPod.
// * Clears the whole UtPod
UtPod::~UtPod(){
clearMemory();
}