Question: Create a function compress, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a

Create a function compress, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a linked list in which repetition has been eliminated.

The linked list for this problem uses the following class declaration:

class node { public: string data; node* next; }; 

Example:

"green"->"green"->"blue"->"red"->"green"->NULL return "green"->"blue"->"red"->"green"->NULL 
"apple"->"apple"->NULL return "apple"->NULL 

Do this in C++. Code in bold CANNOT be changed.

#include #include

node* compress(node* head) { if (head == NULL) return NULL;

node *curr = head; node *aux;

while (curr->next != NULL) { if (curr->data == curr->next->data) { aux = curr->next->next; delete curr->next; curr->next = aux; } else { curr = curr->next; } } head = curr;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!