Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: Use a linked list to store your favorite movies. Store the movie objects into a linked list, display the movie information by sorting with

C++:

Use a linked list to store your favorite movies.

Store the movie objects into a linked list, display the movie information by sorting with the title, viewed year, and rating.

You may need three more functions to display the sorted movie information by passing the pointer of the first movie object in the linked list:

image text in transcribed

I have already figured out most of the code, but I am having trouble fixing the bottom list "**Display Movies info sorted by viewed years**" to output correctly

image text in transcribed

Here is my code:

-------------------------

#include

#include

#include

using namespace std;

struct movie

{

string title;

int viewed_year;

int rating;

movie* next;

};

void list_movie(const movie* head)

{

cout

cout

int i = 1;

for (const movie* p = head; p; p = p->next)

{

cout

cout title;

cout viewed_year;

cout rating

}

}

void arrange_by_title(movie* &head)

{

movie* temp = head->next;

if (head->title.compare(head->next->title) > 0)

{

head->next = temp->next;

temp->next = head;

head = temp;

}

temp = head->next;

if (head->title.compare(temp->next->title) > 0)

{

temp->next->next = head;

head = temp->next;

temp->next = NULL;

}

else

{

head->next = temp->next;

head->next->next = temp;

temp->next = NULL;

}

}

void arrange_by_rating(movie* &head)

{

movie* temp = head->next;

if (head->rating > head->next->rating)

{

head->next = temp->next;

temp->next = head;

head = temp;

}

temp = head->next;

if (head->rating > temp->next->rating)

{

temp->next->next = head;

head = temp->next;

temp->next = NULL;

}

else

{

head->next = temp->next;

head->next->next = temp;

temp->next = NULL;

}

}

void arrange_by_viewed(movie* &head)

{

movie* temp = head->next;

if (head->viewed_year > head->next->viewed_year)

{

head->next = temp->next;

temp->next = head;

head = temp;

}

temp = head->next;

if (temp->next->viewed_year viewed_year)

{

temp->next->next = head;

head = temp->next;

temp->next = NULL;

}

else

{

head->next = temp->next;

head->next->next = temp;

temp->next = NULL;

}

}

int main()

{

movie* firstmovie = 0;

string title1, title2, title3; //3 Strings to store names of the movies

int viewed_year1, viewed_year2, viewed_year3; //3 ints's to store viewed year of the movies

int rating1, rating2, rating3; //3 ints's to store rating of the movies

cout

getline(cin, title1);

cout

cin >> viewed_year1;

cout

cin >> rating1;

cin.get();

cout

getline(cin, title2);

cout

cin >> viewed_year2;

cout

cin >> rating2;

cin.get();

cout

getline(cin, title3);

cout

cin >> viewed_year3;

cout

cin >> rating3;

// 3 movie objects to store the movies

movie a = { title1, viewed_year1, rating1 };

movie b = { title2, viewed_year2, rating2 };

movie c = { title3, viewed_year3, rating3 };

a.next = firstmovie;

firstmovie = &a;

b.next = firstmovie;

firstmovie = &b;

c.next = firstmovie;

firstmovie = &c;

// Function call to print the movies

// list_movie(firstmovie);

cout

arrange_by_title(firstmovie);

list_movie(firstmovie);

cout

arrange_by_rating(firstmovie);

list_movie(firstmovie);

cout

arrange_by_viewed(firstmovie);

list_movie(firstmovie);

}

-------------------------

I know the issue somewhere under my "void arrange_by_viewed(movie* &head)" function, but still having no luck. Any help is greatly appreciated, thank you.

void arrange_ by title(Movie) void arrange_by rating(Movie* void arrange_by viewed(Movie* - Sample output, Please enter the title of the movie1(within 30-character): Now You See Me Please enter the year you viewed Now You See Me [like 2017]:2014 Please your rating for Now You See Me [1, 2, 3, 4, 5]:4 Please enter the title of the movie2(within 30-character) :X-Men: First Cla Please enter the year you viewed X-Men: First Class like 2017]:2011 Please your rating for X-Men: First Class [1, 2, 3, 4, 5]:3 Please enter the title of the movie3(within 30-character):Hidden Figures Please enter the year you viewed Hidden Figures [like 2017]:2016 Please your rating for Hidden Figures [1, 2, 3, 4, 5]:5 *Display Movies info sorted by titles** Movie Title Viewed Rating 5 3 Viewed Rating 3 5 Viewed Rating 1 Hidden Figures 2 Now You See Me 2016 2014 2011 Display Movies info sorted by rating* X-Men: First Class Movie Title 1 X-Men: First Class 2 Now You See Me 3 Hidden Figures 2011 2014 2016 **Display Movies info sorted by viewed years* Movie Title 1 X-Men: First Class 2 Now You See Me 3 Hidden Figures 2011 2014 2016 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions