Question

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. (Use C++ )

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

       }

      

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;         for(int i=0;i<population_directory.size();i++){                 if(population_directory[i].second>=1000000000){                        ...
The following program allows the user to enter the grades of 10 students in a class...
The following program allows the user to enter the grades of 10 students in a class in an array called grade. In a separate loop, you need to test if a grade is passing or failing, and copy the grade to an array to store passing or failing grades accordingly. A passing grade is a grade greater than or equal to 60. You can assume the use will enter a grade in the range of 0 to 100, inclusive. ...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login name extern string ASSIGNMENT; /** * Describe the purpose of your program here. * @return 0 for success. */ int run() { repeatChar(3, "abcde$"); return 0; } string repeatChar(int n, const string& pat) { string result; for (int i = 0; i < n; i++) { result += pat; } return result; } could you correct my code?? i got an error msg like...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...