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; }
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps