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

       }

Solutions

Expert Solution

//cpp program for implementing linked list
#include<bits/stdc++.h>
using namespace std;

const int NUM = 10;
typedef struct Student{
string fName;
string lName;
struct Student *next;
}Student;

// declaring getnode funtion to create a Student type node to add to the linked list.
Student* gettingnode(){
    Student* node = NULL;
    node = new Student();;
    node->fName;
    node->lName;
    node->next = NULL;
    return node;
}
//main function is started from here
int main()
{
    Student stuArr[NUM];
    ifstream myfile;
    myfile.open("C:/Users/Desktop/linkedtest.txt"); //location of text file 
    for(int i = 0; i < NUM; i++)
    {
         myfile>>stuArr[i].fName;
         myfile>>stuArr[i].lName;
         stuArr[i].next = 0;
    }

    Student *head = gettingnode();
    // 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 indexes first.
    while(idx < 10){
        temp = gettingnode();
        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 indexes finally.
    while(idx < 10){
        temp = gettingnode();
        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;
}

Here is the output screen:

linkedtest.txt file


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 #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++)...
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.
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
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...
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.
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++
**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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT