Question

In: Computer Science

Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...

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

Solutions

Expert Solution

#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:


Related Solutions

Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT