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

Given an array of Student type and size 10. Read data from a file to initialize

ID: 3590663 • Letter: G

Question

Given an array of Student type and size 10. Read data from a file to initialize the array. Create a linked list for the elements in the array. The order of the linked list should be the reverse order of the array. For example, the first element in the array should be the last element in the linked list, while the last element in the array should be the first element in the linked list. Finally, write a loop to print out the students in the linked list in the main function.

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

const int NUM = 10;

struct Student{

string fName;

string lName;

Student * next;

};

int main() {

       Student stuArr[NUM];

       ifstream myfile;

       myfile.open("Test.txt");

       for(int i = 0; i < NUM; i++)

       {

             myfile>>stuArr[i].fName;

             myfile>>stuArr[i].lName;

             stuArr[i].next = 0;

       }

      

..............................

}

Explanation / Answer

using namespace std;

struct Node

{

    int data;

    struct Node *next;

};

void reverseUtil(Node *curr, Node *prev, Node **head);

// This function mainly calls reverseUtil()

// with prev as NULL

void reverse(Node **head)

{

    if (!head)

        return;

    reverseUtil(*head, NULL, head);

}

// A simple and tail recursive function to reverse

// a linked list. prev is passed as NULL initially.

void reverseUtil(Node *curr, Node *prev, Node **head)

{

    /* If last node mark it head*/

    if (!curr->next)

    {

        *head = curr;

        /* Update next to prev node */

        curr->next = prev;

        return;

    }

    /* Save curr->next node for recursive call */

    node *next = curr->next;

    /* and update next ..*/

    curr->next = prev;

    reverseUtil(next, curr, head);

}

// A utility function to create a new node

Node *newNode(int key)

{

    Node *temp = new Node;

    temp->data = key;

    temp->next = NULL;

    return temp;

}

// A utility function to print a linked list

void printlist(Node *head)

{

    while(head != NULL)

    {

        cout << head->data << " ";

        head = head->next;

    }

    cout << endl;

}

// Driver program to test above functions

int main()

{

    Node *head1 = newNode(1);

    head1->next = newNode(2);

    head1->next->next = newNode(3);

    head1->next->next->next = newNode(4);

    head1->next->next->next->next = newNode(5);

    head1->next->next->next->next->next = newNode(6);

    head1->next->next->next->next->next->next = newNode(7);

    head1->next->next->next->next->next->next->next = newNode(8);

    cout << "Given linked list ";

    printlist(head1);

    reverse(&head1);

    cout << " Reversed linked list ";

    printlist(head1);

    return 0;

}

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