The program requires me to remove all item for a list, but when doing the test case for it, it only removes one. What would be the problem in this code? Given code: #include "MovieList.h" #include using namespace std; // Constructor function MovieList::MovieList() {     top = nullptr; } // Destructor function MovieList::~MovieList() {     MovieNode *p = nullptr;     while (top->next != nullptr) {         p = top;         top = top->next;         delete p;     }     delete top;     p = nullptr;     top = nullptr; } void MovieList::display(ostream &out) {     MovieNode *t = top;     int pos = 0;     // Function to display the movie list     while (t != nullptr) {         out << pos << ": " << t->title << endl;         pos++;         t = t->next;     } } int MovieList::count() {     MovieNode *t = top;     int pos = 0;     // Counting based off the movie list     while (t != nullptr) {         pos++;         t = t->next;     }    return pos; } void MovieList::addToTop(string title) {     // New node creation     MovieNode *p = new MovieNode();     p->title = title;     p->next = nullptr;     // Relocating the new node to the top of the list     p->next = top;     top = p; } void MovieList::addToBottom(string title) {     // Creating a new node     MovieNode *p = new MovieNode();     p->title = title;     p->next = nullptr;     // What to do if list is empty     if (top == nullptr) {         top = p;         return;     }     // Relocating newly added node to the bottom of the list     MovieNode *t = top;     while(t->next != nullptr)   // End node is traversing     {         t = t->next;     }     t->next = p; } bool MovieList::moveToTop(string title) {     // What to do if the given title is already at the top of list     if (top->title == title) {         return true;     }     // Searching for the new node and putting it at the top of list if found     MovieNode *t = top->next;     MovieNode *p = top;     while(t != nullptr) {         if (t->title == title) {             p->next = t->next;             t->next = top;             top = t;             return true;         }         p = t;         t = t->next;     }     return false; } bool MovieList::remove(int n) {     MovieNode *t = top;     // If n == 1 i.e. top node     if (n == 1) {         top = top->next;         t->next = nullptr;         delete t;     }     // To remove any other necessary node     else {         MovieNode *p = nullptr;         while(n++) {             if ( t == nullptr ) {                 return false;             }             if (n == 1) {                 p->next = t->next;                 t->next = nullptr;                 delete t;             }             else {                 p = t;                 t = t->next;             }         }     }     return true; } string MovieList::nextLarger(string title) {     string nextLarger = "";     MovieNode *t = top;     while(t != nullptr) {         if (t->title > title) {             nextLarger = t->title;             break;         }         t = t->next;     }     t = top;     // Finding the next largest element in list     while(t != nullptr) {         if (t->title < nextLarger && t->title > title) {             nextLarger = t->title;         }         t = t->next;     }     return nextLarger; } void MovieList::displaySorted(ostream& out) {     string temp = "";     MovieNode *t = top;     out << "Sorted List: " << endl;     while(t != nullptr) {         temp = this->nextLarger(temp);         out << temp << " ";         t = t->next;     }     out << endl; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

The program requires me to remove all item for a list, but when doing the test case for it, it only removes one. What would be the problem in this code?

Given code:

#include "MovieList.h"
#include <iostream>

using namespace std;
// Constructor function
MovieList::MovieList() {

    top = nullptr;

}

// Destructor function
MovieList::~MovieList() {

    MovieNode *p = nullptr;

    while (top->next != nullptr) {
        p = top;
        top = top->next;
        delete p;
    }

    delete top;
    p = nullptr;
    top = nullptr;

}

void MovieList::display(ostream &out) {

    MovieNode *t = top;
    int pos = 0;
    // Function to display the movie list
    while (t != nullptr) {
        out << pos << ": " << t->title << endl;
        pos++;
        t = t->next;
    }


}

int MovieList::count() {

    MovieNode *t = top;
    int pos = 0;
    // Counting based off the movie list
    while (t != nullptr) {
        pos++;
        t = t->next;
    }

   return pos;

}

void MovieList::addToTop(string title) {

    // New node creation
    MovieNode *p = new MovieNode();
    p->title = title;
    p->next = nullptr;

    // Relocating the new node to the top of the list
    p->next = top;
    top = p;

}

void MovieList::addToBottom(string title) {
    // Creating a new node
    MovieNode *p = new MovieNode();
    p->title = title;
    p->next = nullptr;

    // What to do if list is empty
    if (top == nullptr) {
        top = p;
        return;
    }

    // Relocating newly added node to the bottom of the list
    MovieNode *t = top;
    while(t->next != nullptr)   // End node is traversing
    {
        t = t->next;
    }
    t->next = p;

}

bool MovieList::moveToTop(string title) {
    // What to do if the given title is already at the top of list
    if (top->title == title) {
        return true;
    }

    // Searching for the new node and putting it at the top of list if found
    MovieNode *t = top->next;
    MovieNode *p = top;
    while(t != nullptr) {
        if (t->title == title) {
            p->next = t->next;
            t->next = top;
            top = t;
            return true;
        }
        p = t;
        t = t->next;
    }

    return false;
}

bool MovieList::remove(int n) {

    MovieNode *t = top;

    // If n == 1 i.e. top node
    if (n == 1) {
        top = top->next;
        t->next = nullptr;
        delete t;
    }
    // To remove any other necessary node
    else {
        MovieNode *p = nullptr;

        while(n++) {
            if ( t == nullptr ) {
                return false;
            }
            if (n == 1) {
                p->next = t->next;
                t->next = nullptr;
                delete t;
            }
            else {
                p = t;
                t = t->next;
            }
        }

    }
    return true;

}

string MovieList::nextLarger(string title) {
    string nextLarger = "";
    MovieNode *t = top;

    while(t != nullptr) {
        if (t->title > title) {
            nextLarger = t->title;
            break;
        }
        t = t->next;
    }

    t = top;
    // Finding the next largest element in list
    while(t != nullptr) {
        if (t->title < nextLarger && t->title > title) {
            nextLarger = t->title;
        }
        t = t->next;
    }

    return nextLarger;
}

void MovieList::displaySorted(ostream& out) {

    string temp = "";
    MovieNode *t = top;

    out << "Sorted List: " << endl;
    while(t != nullptr) {
        temp = this->nextLarger(temp);
        out << temp << " ";
        t = t->next;
    }
    out << endl;
}

Problem: Implement a part of functionality for the Netflix DVD queue. It's a service that allows a user to create a list of desired movies and
then sends DVDS with movies on top of this list to the subscriber one at a a time. A subscriber should be able to create a list of desired
movies and manipulate the order in a movie queue in their account. Your program will implement some of the desired functionality by
storing the list of movie titles in a linked list. You are provided with the following files available in "Downloadable files" section:
• MovieList.h contains a class declaration for the class that represents a list of movies.
• Driver.cpp contains a main function you can use to test your implementation.
You will be responsible for providing the MovieList.cpp file, including the implementation of the MovieList member functions (described
below):
• MovieList and ~MovieList: creates an empty list, and deallocates all the nodes in the list, respectively.
display(ostream& out) Print movie titles from top to bottom, with positions numbered (put a colon and space between the number
and the movie title) one movie per line. Use out << instead of cout <.
count() Returns the number of movie titles in the list
addToTop(string title) Add a movie to the top of the list
addToBottom(string title) Add a movie to the bottom of the list
• moveToTop(string title) Move a movie with given title to position 0 (top)
• remove(int n) Remove a movie at the given position. Return true if successful, false if there is no movie at position n.
nextLarger(string title) Returns the move title (string t) that would come next after the title t in alphabetical order. Hint: use the
algorithm to find the minimum string in a list, but ignore any movie with title t, and any that come before it in alphabetical order.
displaySorted() Use (i.e. call) nextLarger(string) to output the titles in sorted order with positions numbered. Do NOT sort the linked
list, it should remain in the same order. Call nextLarger on the empty string to find the first one.
Input/Output:
Use the provided Driver.cpp file to test your code. I recommend trying to implement one or two functions at a time, and testing them, rather
than implementing all the functions and then trying to debug them all at once.
NOTES:
• Put your code in a file named MovieList.cpp. This is the only file you need to submit for grading.
• Your MovieList.cpp file must compile with the (unchanged) provided files, and pass at least one test case. You may need to use -
std=c++11 to get the code to compile.
• You may re-use code from the NumberList class (available on Canvas under files, and discussed in class).
Transcribed Image Text:Problem: Implement a part of functionality for the Netflix DVD queue. It's a service that allows a user to create a list of desired movies and then sends DVDS with movies on top of this list to the subscriber one at a a time. A subscriber should be able to create a list of desired movies and manipulate the order in a movie queue in their account. Your program will implement some of the desired functionality by storing the list of movie titles in a linked list. You are provided with the following files available in "Downloadable files" section: • MovieList.h contains a class declaration for the class that represents a list of movies. • Driver.cpp contains a main function you can use to test your implementation. You will be responsible for providing the MovieList.cpp file, including the implementation of the MovieList member functions (described below): • MovieList and ~MovieList: creates an empty list, and deallocates all the nodes in the list, respectively. display(ostream& out) Print movie titles from top to bottom, with positions numbered (put a colon and space between the number and the movie title) one movie per line. Use out << instead of cout <. count() Returns the number of movie titles in the list addToTop(string title) Add a movie to the top of the list addToBottom(string title) Add a movie to the bottom of the list • moveToTop(string title) Move a movie with given title to position 0 (top) • remove(int n) Remove a movie at the given position. Return true if successful, false if there is no movie at position n. nextLarger(string title) Returns the move title (string t) that would come next after the title t in alphabetical order. Hint: use the algorithm to find the minimum string in a list, but ignore any movie with title t, and any that come before it in alphabetical order. displaySorted() Use (i.e. call) nextLarger(string) to output the titles in sorted order with positions numbered. Do NOT sort the linked list, it should remain in the same order. Call nextLarger on the empty string to find the first one. Input/Output: Use the provided Driver.cpp file to test your code. I recommend trying to implement one or two functions at a time, and testing them, rather than implementing all the functions and then trying to debug them all at once. NOTES: • Put your code in a file named MovieList.cpp. This is the only file you need to submit for grading. • Your MovieList.cpp file must compile with the (unchanged) provided files, and pass at least one test case. You may need to use - std=c++11 to get the code to compile. • You may re-use code from the NumberList class (available on Canvas under files, and discussed in class).
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY