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

Reconsider the implementation of your first programming assignment IN C++ by usi

ID: 3668370 • Letter: R

Question

Reconsider the implementation of your first programming assignment IN C++ by using the linked list. First, you need to build a linked list for holding these records. Then, sort them in ascending order using Student ID as a key. Display the sorted output. You are limited to use ONLY the concepts of Linked list. Data set is provided below: Name ID GPA Alice 8234 2.7 Mark 2672 3.0 Joanne 1234 3.7 John 3291 2.0 Tim 3212 3.8 Alford 1034 2.7 Benson 6290 2.5 Nancy 4234 3.7 Oscar 1672 1.0 Larry 1004 2.7

Explanation / Answer

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
typedef struct node node;

struct node
{
string name;
int id;
float gpa;
node * next;
};


void push(node **Head,string name,int id,float gpa )
{
node *newNode=new node();
newNode->name=name;
newNode->id=id;
newNode->gpa=gpa;
newNode->next=*Head;
*Head=newNode;
}





void PrintData(node *head)
{  
node *temp=new node();
temp=head;
while(temp!=NULL)
{
cout<<" Name is "<<temp->name<<" ID is "<<temp->id<<" Gpa is "<<temp->gpa<<" ";
temp=temp->next;
}
}




void findLength(node *head)
{
int length=0;
node *temp=new node();
temp=head;
while(temp!=NULL)
{
temp=temp->next;
length++;
}
cout<<"Total Length is %d "<<length;
}




void bubbleSort(node *head)
{
    int swapped, i;
   node *ptr1;
   node *lptr = NULL;

    /* Checking for empty list */
    if (ptr1 == NULL)
        return;

    do
    {
        swapped = 0;
        ptr1 = head;

        while (ptr1->next != lptr)
        {
            if (ptr1->id > ptr1->next->id)
            {
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}

/* function to swap data of two nodes a and b*/
void swap(node *a,node *b)
{
string temp2=a->name;
    a->name = b->name;
    b->name = temp2;
   
    int temp1 = a->id;
    a->id = b->id;
    b->id = temp1;

float temp3=a->gpa;
    a->gpa = b->gpa;
    b->gpa = temp3;

}

int main()
{

node *head=NULL;
push(&head,"Alice" ,8234 ,2.7);
push(&head,"Mark",2672 ,3.0);
push(&head,"Joanne", 1234, 3.7);
push(&head,"John" ,3291 ,2.0 );
push(&head,"Tim" ,3212 ,3.8);
push(&head,"Alford", 1034 ,2.7);
push(&head,"Benson", 6290 ,2.5);
push(&head,"Nancy" ,4234 ,3.7);
push(&head,"Oscar", 1672 ,1.0 );
push(&head,"Larry" ,1004 ,2.7);
PrintData(head);
bubbleSort(head);
PrintData(head);
cout<<"Before Declaration";
return 0;

}