#include "node.h"
#include
using namespace std;
class LinkedList {
node* head;
node* tail;
int size;
node* playing = NULL;
 void appendNode(node *prev, node *n) {Â
 n->next = prev->next;Â
 prev->next = n;
 n->prev = prev;
 n->next->prev = n; }
public: LinkedList() {
head = new node;
tail = new node;
head->song = NULL;
tail->song = NULL;
head->next = tail;
tail->prev = head;
size = 0; }
void addSong(Song* s) {Â
node *n = new node;Â
n->song = s;Â
if (s->fave) {Â
node *cur = head->next;Â
while (cur != tail && cur->song->fave) {Â
cur = cur->next;Â }Â
appendNode(cur->prev, n);Â
} else {Â
appendNode(tail->prev, n);Â }Â
size++; }
Song* next() {Â
if (playing && playing->next != tail) {Â
playing = playing->next;Â
playing->song->play();Â
return playing->song;Â }Â
if (!playing && size && head->next != tail) {Â
playing = head->next;Â
playing->song->play();Â
return playing->song;Â }Â
return NULL; }
bool toggleFavorite() {Â
if (playing) {Â
playing->song->fave = !playing->song->fave;Â
return playing->song->fave;Â }Â
return false; }
Song* previous() {Â
if (playing && playing->prev != head) {Â
playing = playing->prev;Â
playing->song->play();Â
return playing->song;Â }Â
return NULL; }
Song* play() {Â
if (playing) {Â
playing->song->play();Â
return playing->song;Â }Â
return NULL; }
Song* skip() {Â
if (playing) {Â
playing->song->skip();Â
if (playing->next != tail) {Â
playing->song->skip();Â
playing = playing->next;Â
playing->song->play();Â
return playing->song;Â }Â }Â
return NULL; }
Song* skip(int item) {Â
if (!playing) return NULL;Â
playing->song->skip();Â
node *n = playing;Â
for (int i = 0; i < item && n != tail; i++) {Â
n = n->next;Â }Â
if (n != tail) {Â
playing = n;Â
playing->song->play();Â
return n->song;Â }Â
return NULL; }
Song* find(string art) {Â
node *cur = playing ? playing->next : head->next;Â while (cur != tail) {Â
if (cur->song->artist[0] == art || cur->song->artist[1] == art) {Â
playing = cur;Â
cur->song->play();Â
return cur->song;Â
}Â
cur = cur->next;Â
}
return NULL; }
Song* remove() {Â
if (playing) {Â
size--;Â
Song *ret = playing->song;Â
node *n = playing;Â
playing->prev->next = playing->next;Â
playing->next->prev = playing->prev;Â
if (playing->next != tail) {Â
playing = playing->next;Â
} else if (playing->prev != head) {Â
playing = playing->prev;Â
} else {Â
playing = NULL;Â
}Â
delete n;Â
return ret;Â
}Â
return NULL; }
void print() {
cout << "Song Count: " << size << endl;
node* c = head->next;
int i = 1;
while (c != tail) {
cout << i++ << ". "; c->song->print(); cout << endl; c = c->next;
}
}
};