Question

In: Computer Science

Given an array of Student type and size 10, create a linked list of students by...

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; }

Solutions

Expert Solution

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..


Related Solutions

Given an array of Student type and size 10, create a linked list of students by...
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<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;...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create...
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create a function that can put a name and a grade to the arrays. - Keep Read student name and grade until student name is “???”. And save the reading by using a function - Create another function to show all the grade in that object. - Create the third function that can display the maximum grade and the student’s name. - Create a sorting...
Directions of assignment: - Create an array of words of size 10. - Prompt the User...
Directions of assignment: - Create an array of words of size 10. - Prompt the User to enter the 10 integers. Populate the array with the integers as they are entered. - You MUST use indexed addressing to traverse through the array. - Determine the maximum and the minimum values contained within the array and print them out. Can you see what I am doing wrong here, the last part (to find the min and max) I can't seem to...
Create a Linked List and conduct the following operations. Portion of the program is given. The...
Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add 100 to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print it...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000....
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000. Time each sort. Merge sort Insertion Sort Selection Sort         Bubble Sort Quick Sort PLEASE DO IT IN C++
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT