In: Computer Science
Using doubly linked list in c++ with class constructor:
DNode(){
song = Song();
prev = NULL;
next = NULL;
}
DNode(string s, string a, int lenmin, int lensec){
song = Song(s,a,lenmin,lensec);
prev = NULL;
next = NULL;
}
for each node. Write the method:
void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following:
Punching in a Dream, The Naked And Famous................3:58
Harder To Breather, Maroon 5................2:52
Where the Rivers Flow, Sons of Maria................3:O3
Still Not a Player, Big Pun Joe................3:56
Train, Brick+Mortar................3:O5
And the song was “Still Not a Player”, the result would be:
Punching in a Dream, The Naked And Famous................3:58
Harder To Breather, Maroon 5................2:52
Still Not a Player, Big Pun Joe................3:56
Where the Rivers Flow, Sons of Maria................3:O3
Train, Brick+Mortar................3:O5
If you move the first song up, it will be moved to the end of the list as follows:
Punching in a Dream, The Naked And Famous................3:58
Harder To Breather, Maroon 5................2:52
Still Not a Player, Big Pun Joe................3:56
Where the Rivers Flow, Sons of Maria................3:O3
Train, Brick+Mortar................3:O5
Moving “Punching in a Dream” up one would result in:
Harder To Breather, Maroon 5................2:52
Still Not a Player, Big Pun Joe................3:56
Where the Rivers Flow, Sons of Maria................3:O3
Train, Brick+Mortar................3:O5
Punching in a Dream, The Naked And Famous................3:58
#include<bits/stdc++.h>
using namespace std;
class Song {
public:
string songname, album;
int len_in_min, len_in_sec;
Song() {}
Song(string s, string a, int lenmin, int lensec) {
songname = s;
album = a;
len_in_min = lenmin;
len_in_sec = lensec;
}
void printSong() {
cout << songname << " " << album << " " << len_in_min << ":" << len_in_sec << endl;
}
};
class DNode {
public:
Song song;
DNode* next;
DNode* prev;
DNode() {
song = Song();
prev = NULL;
next = NULL;
}
DNode(string s, string a, int lenmin, int lensec) {
song = Song(s, a, lenmin, lensec);
prev = NULL;
next = NULL;
}
};
class DLinkedList {
public:
DNode* head = NULL;
DNode* tail = NULL;
void add(string s, string a, int lenmin, int lensec) {
if (head == NULL) {
head = new DNode(s, a, lenmin, lensec);
// (head->song).printSong();
tail = head;
} else {
tail->next = new DNode(s, a, lenmin, lensec);
tail->next->prev = tail;
tail = tail->next;
}
}
void MoveUp(string s) {
DNode* curr = head;
while ((curr->song).songname != s && curr != NULL) {
curr = curr->next;
}
DNode* a = curr->prev->prev;
DNode* b = curr->prev;
DNode* c = curr;
DNode* d = curr->next;
a->next = c;
c->prev = a;
c->next = b;
b->prev = c;
b->next = d;
d->prev = b;
}
void printList() {
DNode* curr = head;
while (curr != NULL) {
(curr->song).printSong();
curr = curr->next;
}
}
};
int main() {
DLinkedList D;
D.add("Punching in a Dream", "The Naked And Famous", 3, 58);
D.add("Harder To Breather", "Maroon 5", 2, 52);
D.add("Where the Rivers Flow", " Sons of Maria", 3, 3);
D.add("Still Not a Player", "Big Pun Joe", 3, 56);
D.add("Train", " Brick+Mortar", 3, 5);
D.printList();
D.MoveUp("Still Not a Player");
cout << "\nSelected Song: Still Not a Player\n" << endl;
D.printList();
return 0;
}
OUTPUT: