-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.cpp
159 lines (138 loc) · 3.08 KB
/
Library.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
/*
CSI-240-52
Joe Healy
Final Assignment
Library Class
*/
#include "Library.h"
using namespace std;
/*Default constructor
PRE: none
POST: head, current and tail nodes set to null
*/
Library::Library()
{
head = current = tail = NULL;
}
/*addSong - adds a song to the end of the library
PRE: s is a valid song object
POST: s is added to the end of the library.
if head is null, it is set to s
tail and current are set to s
*/
void Library::addSong(Song s)
{
if (head == NULL)
{
head = new LLNode{ s, NULL, NULL };
}
else
{
LLNode* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new LLNode{ s, temp, NULL };
tail = current = temp->next;
}
}
/*removeSong - removes a specific song from the library
PRE: s is a valid song object
POST: if s matches a song in the library, that song will be removed from the library
if not, nothing happens
if s is the tail, tail is set to the previous song
if s is current, current is set to next song, if next song is null current is set to previous song
*/
void Library::removeSong(Song s)
{
LLNode* temp = head;
LLNode* match = new LLNode;
while (temp->next != NULL)
{
if (temp->data == s)
{
match = temp;
}
temp = temp->next;
}
if (match != NULL)
{
if (match == tail)
{
tail = match->prev;
}
if (match == current)
{
if (match->next != NULL)
{
current = match->next;
}
else
{
current = match->prev;
}
}
LLNode* previous = match->prev;
LLNode* next = match->next;
delete match;
previous->next = next;
next->prev = previous;
}
}
/*sort - sorts the library alphabetically by song name
PRE: none
POST: library is sorted A-Z by songname
*/
void Library::sort()
{
bool swapped = true;
if (head == NULL)
{
return;
}
while (swapped)
{
current = head;
swapped = false;
while (current->next != NULL)
{
string currSong = current->data.getName();
string nextSong = current->next->data.getName();
//transform song titles to lowercase so that they can be compared, found this function at: https://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/
transform(currSong.begin(), currSong.end(), currSong.begin(), ::tolower);
transform(nextSong.begin(), nextSong.end(), nextSong.begin(), ::tolower);
if (currSong > nextSong)
{
swapNodes(current, current->next);
swapped = true;
}
current = current->next;
}
}
}
/*swapNodes - swaps 2 nodes in the library
PRE: a, and b are valid
POST: node a now contains the data from node b, and vice versa
*/
void Library::swapNodes(LLNode* a, LLNode* b)
{
Song junk = a->data;
a->data = b->data;
b->data = junk;
}
/*toString - returns a string representation of the library
PRE: none
POST: FCTVAL is a formatted string with data from every song in the library
*/
string Library::toString()
{
string ret;
LLNode* temp = head;
while (temp != NULL)
{
ret.append(temp->data.toString());
temp = temp->next;
}
return ret;
}