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

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.
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,...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT