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...
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,...
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...
Create a method that gets the difference for a linked bag and a resizable array: Also,...
Create a method that gets the difference for a linked bag and a resizable array: Also, implement the methods in the interface and test out that the program works in the main class //please comment what you are doing for the difference method for the linked bag and the array bag (also explain its time complexity in terms of Big O EX: Bag 1 has : ABC EX: Bag 2 has : ARC the difference is "R" BagInterface public boolean...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT