Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**PLEASE USE C++** ** THE FOLLOWING IS CODE FROM LAB 9.2** 2. (5 points) Impleme

ID: 3708671 • Letter: #

Question

**PLEASE USE C++**

**THE FOLLOWING IS CODE FROM LAB 9.2**

2. (5 points) Implement the sort member function with prototype void sort (bool flag) for the SList class (done in lab 9.2), which sorts the list in ascending order if the flag is 1 otherwise in the descending order class Node int data; Node *next; public: Node ( ) {data 0; next 0;} void setData(int d) {data d;} void setNext (Node ) fnext - n;) int getDatafreturn data;) Node* getNext )return next;) J; class SListt public: Node *head; SList) head0;) void printSListO; // prints all elements of the list void Insert (int d); // inserts a new integer d in the list void Delete(int d); // deletes all element whose value is d from the list void sort (bool flag); /* sorts the list in ascending order if the flag is 1, otherwise in descending order*/ J; Demonstrate the class in the main program as follows SList 1ist1; for (int i=1; í?6; i++){ list1.Insert (i) list1.printSList O; list1.sort (1); list1.printSList O; list1.sort (0); list1.printSList O

Explanation / Answer

void sort(bool flag)

{

    int len = 0;

   

    // point trav to the head of the list

    Node *trav = this->head;

   

    // calculate the length of the list

    // traverse through the list

    while(trav)

    {

        len++;

       

        // go to next node

        trav = trav->getNext();

    }

   

    // create an array of length len

    int *arr = new int[len];

   

    trav = this->head;

   

    int index = 0;

   

    // fill arr with the contents of list

    // traverse through the list

    while(trav)

    {

        arr[index++] = trav->getData();

       

        // go to next node

        trav = trav->getNext();

    }

   

    int i, j;

       

    // sort in ascending order

    if( flag == 1 )

    {

        // sort using bubble sort

        for (i = 0; i < len - 1; i++)

        {

            // the previous i elements are placed

            for (j = 0; j < len - i - 1; j++)

            {

                // swap the two elements

                if (arr[j] > arr[j+1])

                {

                    int temp = arr[j];

                    arr[j] = arr[j + 1];

                    arr[j + 1] = temp;

                }

            }

        }

    }

    // sort in descending order

    else

    {

        // sort using bubble sort

        for (i = 0; i < len - 1; i++)

        {

            // the previous i elements are placed

            for (j = 0; j < len - i - 1; j++)

            {

                // swap the two elements

                if (arr[j] < arr[j+1])

                {

                    int temp = arr[j];

                    arr[j] = arr[j + 1];

                    arr[j + 1] = temp;

                }

            }

        }

    }

   

    trav = this->head;

   

    index = 0;

   

    // fill list with the contents of arr

    // traverse through the list

    while(trav)

    {

        trav->setData(arr[index++]);

       

        // go to next node

        trav = trav->getNext();

    }

}