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

You have been provided with a linked list of several nodes, each containing two

ID: 3919377 • Letter: Y

Question

You have been provided with a linked list of several nodes, each containing two types of data: a name of type string, and a grade of type double.

First, display the list to the user as is (this is already finished for you).

Then, using any method of your choice (push onto stacks, put into vectors or arrays and sort via bubble, binary, etc.) reorder and display the linked list (display both name and grade) to the user in:

1. Alphabetical order, by name

2. Descending order (highest to lowest), by grade

Explanation / Answer

Hi,

Please find the c++ program below:
///////////////////////////////////////

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* List Node
*/
struct node
{
    string data;
    double grade;
    struct node *next;
}*start;

/*
* List Class
*/
class singleList
{
    public:
        node* create_node(string,double);
        void insert_begin();
        void sortByGrade();
        void sortByData();
        void displayList();
        singleList()
        {
            start = NULL;
        }
};

/*
* Main method
*/
main()
{
    int choice, nodes, element;
    singleList sList;
    start = NULL;
    while (1)
    {
        cout<<endl<<"---------------------------------"<<endl;
        cout<<endl<<"List Operations"<<endl;
        cout<<endl<<"---------------------------------"<<endl;
        cout<<"1.Insert Node at beginning"<<endl;
        cout<<"2.Sort Link List By Grade"<<endl;
        cout<<"3.Sort Link List By Data"<<endl;
        cout<<"4.Display Linked List"<<endl;
        cout<<"5.Exit "<<endl;
        cout<<"Enter your choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Insert Node at Begin: "<<endl;
            sList.insert_begin();
            cout<<endl;
            break;
       case 2:
            cout<<"Sorting List By Grade: "<<endl;
            sList.sortByGrade();
            sList.displayList();
            cout<<endl;
            break;
       case 3:
            cout<<"Sorting List By Data: "<<endl;
            sList.sortByData();
            sList.displayList();
            cout<<endl;
            break;
       case 4:
            cout<<"Display List: "<<endl;
            sList.displayList();
            cout<<endl;
            break;
        case 5:
            cout<<"Exit."<<endl;
            exit(1);
            break;
        default:
            cout<<"Please enter valid choice"<<endl;
        }
    }
}

/*
* Create List Node
*/
node *singleList::create_node(string data,double grade)
{
    struct node *temp, *s;
    temp = new(struct node);
    if (temp == NULL)
    {
        cout<<"Memory error"<<endl;
        return 0;
    }
    else
    {
        temp->data=data;
        temp->grade=grade;
        temp->next = NULL;
        return temp;
    }
}

/*
* Insert node at begin
*/
void singleList::insert_begin()
{
    double value;
    string data;
    cout<<"Enter the data: ";
    cin>>data;
    cout<<"Enter the grade: ";
    cin>>value;
    struct node *temp, *p;
    temp = create_node(data,value);
    if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
    else
    {
        p = start;
        start = temp;
        start->next = p;
    }
    cout<<"Node inserted"<<endl;
}


/*
* Sort List by Grade
*/
void singleList::sortByGrade()
{
    struct node *ptr, *s;
    string data;
    double value;
    if (start == NULL)
    {
        cout<<"List empty"<<endl;
        return;
    }
    ptr = start;
    while (ptr != NULL)
    {
        for (s = ptr->next;s !=NULL;s = s->next)
        {
            if (ptr->grade > s->grade)
            {
                value = ptr->grade;
                data = ptr->data;
                ptr->grade = s->grade;
                ptr->data = s->data;
                s->grade = value;
                s->data = data;
            }
        }
        ptr = ptr->next;
    }
}

/*
* Sort List by Data
*/
void singleList::sortByData()
{
    struct node *ptr, *s;
    string data;
    double value;
    if (start == NULL)
    {
        cout<<"The List is empty"<<endl;
        return;
    }
    ptr = start;
    while (ptr != NULL)
    {
        for (s = ptr->next;s !=NULL;s = s->next)
        {
            if (ptr->data > s->data)
            {
                value = ptr->grade;
                data = ptr->data;
                ptr->grade = s->grade;
                ptr->data = s->data;
                s->grade = value;
                s->data = data;
            }
        }
        ptr = ptr->next;
    }
}


/*
* Display list
*/
void singleList::displayList()
{
    struct node *temp;
    if (start == NULL)
    {
        cout<<"The List is Empty"<<endl;
        return;
    }
    temp = start;
    cout<<"Elements of list are: "<<endl;
    while (temp != NULL)
    {
        cout<< "(" << temp->data << "," << temp->grade << ") ->";
        temp = temp->next;
    }
    cout<<"NULL"<<endl;
}


//////////////////////////////////////////////////////////

Sample Program Run Output:

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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 1
Insert Node at Begin:
Enter the data: John
Enter the grade: 8.5
Node inserted


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 1
Insert Node at Begin:
Enter the data: Mark
Enter the grade: 2.2
Node inserted


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 1
Insert Node at Begin:
Enter the data: Bonnie
Enter the grade: 9.2
Node inserted


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 1
Insert Node at Begin:
Enter the data: Sarah
Enter the grade: 6.5
Node inserted


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 1
Insert Node at Begin:
Enter the data: Mary
Enter the grade: 7.2
Node inserted


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 4
Display List:
Elements of list are:
(Mary,7.2) ->(Sarah,6.5) ->(Bonnie,9.2) ->(Mark,2.2) ->(John,8.5) ->NULL


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 2
Sorting List By Grade:
Elements of list are:
(Mark,2.2) ->(Sarah,6.5) ->(Mary,7.2) ->(John,8.5) ->(Bonnie,9.2) ->NULL


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 3
Sorting List By Data:
Elements of list are:
(Bonnie,9.2) ->(John,8.5) ->(Mark,2.2) ->(Mary,7.2) ->(Sarah,6.5) ->NULL


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

List Operations

---------------------------------
1.Insert Node at beginning
2.Sort Link List By Grade
3.Sort Link List By Data
4.Display Linked List
5.Exit
Enter your choice: 5
Exit.

Process returned 1 (0x1)   execution time : 127.011 s
Press any key to continue.


Hope this helps . Let me know if you need more information.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote