In: Computer Science
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list.
#include #include #include 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; }
Below is the program in C++ for the requirements given in the question. We have created a getnode() function to create a Student type node to make a linked list. Initially we start by making the first index not zero of the array point to the head of the linked list then incrementing by 2 every time on the array given so that to make the linked list first of all the odd indices elements like 1, 3, 5, 7, 9. Then we again start the loop to add the 0th index element of the array at the end of the linked list we have created till now and then once zero index element is added then again incrementing by 2 on the array to add elements with indices 2, 4, 6, 8. So in this way we can have the desired linked list. Sample output and the sample file for which output is generated has been attahced at the end.
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int NUM = 10;
typedef struct Student{
string fName;
string lName;
struct Student *next;
}Student;
// getnode funtion to create a Student type node to add to the
linked list.
Student* getNode(){
Student* node = NULL;
node = new Student();;
node->fName;
node->lName;
node->next = NULL;
return node;
}
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;
}
Student *head = getNode();
// making index 1 of array element as the head of the linked
list.
head->fName = stuArr[1].fName;
head->lName = stuArr[1].lName;
Student *temp = NULL;
Student *temp1 = head;
int idx = 3;
// looping on all the odd indices first.
while(idx < 10){
temp = getNode();
temp->fName = stuArr[idx].fName;
temp->lName = stuArr[idx].lName;
temp1->next = temp;
temp1 = temp;
idx = idx + 2;
}
idx = 0;
// looping on all the even indices finally.
while(idx < 10){
temp = getNode();
temp->fName = stuArr[idx].fName;
temp->lName = stuArr[idx].lName;
temp1->next = temp;
temp1 = temp;
idx = idx + 2;
}
Student *iter = head;
// printing the entire list.
while(iter){
cout<< iter->fName<<"
"<<iter->lName<<endl;
iter = iter->next;
}
return 0;
}
Sample Output:-
From the output below, we can see odd indexed elements are placed
first and then all even indexed elements. This output is generated
for the text file below.
Sample text file:-I hope this answer is helpful to you, please upvote my answer. I'm in need of it.. Thank you..