Question

// Please follow the instructions and solve it by C++. Please and thank you!! /// Complete...

// Please follow the instructions and solve it by C++. Please and thank you!!

/// Complete the implementation of these functions.
///
#include <iostream>

using namespace std;

struct Rectangle
{
    int height;
    int width;
};

struct ListNode

{
     Rectangle rect;
     ListNode *next = nullptr;
     ListNode(int h, int w)
    {
          /// TO DO: Add your code here

    };
};

void display(ListNode* head)
{
       /// TO DO: Add your code here

      /// If the list is empty, it should display a message to let the user knows about it.

      return;

}

/// Sort the list based on the size of the area (height * width)
void sortLL(ListNode* head)
{
       /// TO DO: Add your code here
      return;
}

/// Remove the duplicated nodes in the list

/// If two nodes have the same height and width, they are considered the same
void removeDuplicates(ListNode* head)
{
      /// TO DO: Add your code here
      return;
}

/// Reverse the nodes in the list
void reverseLL(ListNode* head)
{
     /// TO DO: Add your code here
     return;
}

int main()
{
            ListNode * head = nullptr;

/// Display "The list is empty."

display(head);


sortLL(head);
/// Display "The list is empty."
display(head);

ListNode node1(8, 9);
head = &node1;
sortLL(head);
/// Display the height and width of the node"
display(head);


ListNode node2(8, 2);
ListNode node3(2, 4);
ListNode node4(8, 9);
ListNode node5(2, 4);
ListNode node6(1, 4);
ListNode node7(8, 9);

node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;
node5.next = &node6;
node6.next = &node7;

/// Display the list like
/// (8, 9)->(8, 2)->(2, 4)(8, 9)->(2, 4)->(1, 4)->(8, 9)->@
display(head);

cout << "Sort the list" << endl;

sortLL(head);
/// Display the list like
/// (1, 4)->(2, 4)->(2, 4)->(8, 2)->(8, 9)->(8, 9)->(8, 9)->@
display(head);

cout << "Remove the duplicated items" << endl;

removeDuplicates(head);
/// Display the list like
/// (1, 4)->(2, 4)->(8, 2)->(8, 9)->@
display(head);

cout << "Reverse the list" << endl;

/// You can get extra 10% if the following function is completed
/// reverseLL(head);
/// Display the list like
/// (8, 9)->(8, 2)->(2, 4)->(1, 4)->@
display(head);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Please follow the instructions and solve it by C++. Please and thank you!!

/// Complete the implementation of these functions.

///

#include <iostream>

using namespace std;

struct Rectangle

{

int height;

int width;

};

struct ListNode

{

Rectangle rect;

ListNode *next = nullptr;

ListNode(int h, int w)

{

rect.height = h;

rect.width = w;

};

};

void display(ListNode* head)

{

/// TO DO: Add your code here

if(head==NULL){

cout<<"The list is empty."<<endl;

return;

}

ListNode *curr = head;



while(curr != NULL){

cout<<"("<<curr->rect.height<<", "<<curr->rect.width<<" )"<<"->";

curr= curr->next;

}

cout<<"@"<<endl;


return;

}

/// Sort the list based on the size of the area (height * width)

void sortLL(ListNode* head)

{

bool unsorted = true;

while(unsorted) {

unsorted = false;

ListNode *cur = head;

while(cur != nullptr) {

ListNode *next = cur->next;

if(next!= NULL && ((next->rect.height)*(next->rect.width)) < (cur->rect.height)*(cur->rect.width) ) {

Rectangle temp = cur->rect;

cur->rect = next->rect;

next->rect = temp;

unsorted = true;

}

cur = cur->next;

}

}

}

/// Remove the duplicated nodes in the list

/// If two nodes have the same height and width, they are considered the same

void removeDuplicates(ListNode* head)

{

ListNode* current = head;

ListNode* next_next;

if (current == NULL)

return;

while (current->next != nullptr)

{

if ( (current->rect.height == current->next->rect.height )

&& (current->rect.width == current->next->rect.width )

)

{

next_next = current->next->next;

// free(current->next);

current->next = next_next;

}

else

{

current = current->next;

}

}

}

/// Reverse the nodes in the list

void reverseLL(ListNode** head)

{

ListNode* previous = NULL;

ListNode* current = *head;

ListNode* next;

while (current != NULL)

{

next = current->next;

current->next = previous;

previous = current;

current = next;

}

*head = previous;

}

int main()

{

ListNode * head = nullptr;

/// Display "The list is empty."

display(head);


sortLL(head);

/// Display "The list is empty."

display(head);

ListNode node1(8, 9);

head = &node1;

sortLL(head);

/// Display the height and width of the node"

display(head);


ListNode node2(8, 2);

ListNode node3(2, 4);

ListNode node4(8, 9);

ListNode node5(2, 4);

ListNode node6(1, 4);

ListNode node7(8, 9);

node1.next = &node2;

node2.next = &node3;

node3.next = &node4;

node4.next = &node5;

node5.next = &node6;

node6.next = &node7;

/// Display the list like

/// (8, 9)->(8, 2)->(2, 4)(8, 9)->(2, 4)->(1, 4)->(8, 9)->@

display(head);

cout << "Sort the list" << endl;

sortLL(head);

/// Display the list like

/// (1, 4)->(2, 4)->(2, 4)->(8, 2)->(8, 9)->(8, 9)->(8, 9)->@

display(head);

cout << "Remove the duplicated items" << endl;

removeDuplicates(head);

/// Display the list like

/// (1, 4)->(2, 4)->(8, 2)->(8, 9)->@

display(head);

cout << "Reverse the list" << endl;

/// You can get extra 10% if the following function is completed

reverseLL(&head);

/// Display the list like

/// (8, 9)->(8, 2)->(2, 4)->(1, 4)->@

display(head);

return 0;

}

======================================
SEE OUTPUT


Thanks, PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
// Please follow the instructions and solve it by C++. Please and thank you!! /// Complete...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT