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.
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...
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...
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...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT