Using C++ language- Create an unsorted array of 15 student records (Each student
ID: 3890520 • Letter: U
Question
Using C++ language-
Create an unsorted array of 15 student records (Each student record consists of student ID, student name, Student address, and GPA.)
Create a Linked list to display the student records from the unsorted array above.
Create another linked list by inserting the student records from the unsorted array to a second linked list by using insert function. (Insert Function)
(If you insert correctly, the resulting linked list is sorted.)
Execution and output
Display the student records from the unsorted linked list
Display the sorted student records from the 2nd linked list.
The output of the 2nd linked list should be sorted by using “insert” function from beginning of the list.
Do not use SORT Program. Using it will get penalty.
Delete the nth record from the middle of the sorted list and display the resulting list. (Delete function.)
Insert a mth record to the middle and display the resulting sorted list.
a class must be used but no limitations
Explanation / Answer
linked.cpp
#include<iostream>
using namespace std;
struct Student{
int studentId;
string name;
string address;
int gpa;
};
struct node{
Student *stud;
node *next;
};
class List{
private:
node *head, *tail;
public:
List(){
head = NULL;
tail = NULL;
}
void createNode(struct Student *stud){
node *temp = new node;
temp->stud = stud;
temp->next = NULL;
if ( head == NULL ){
head = temp;
tail = temp;
temp = NULL;
}
else{
tail->next = temp;
tail = temp;
}
}
void display(){
node *temp = new node;
temp = head;
while(temp != NULL){
cout<<temp->stud->studentId<<endl;
temp = temp->next;
}
}
void insertAtBeg(struct Student *stud){
node *temp = new node;
temp->next = head;
temp->stud = stud;
head = temp;
}
void insert(struct Student *stud){
node *temp = new node;
tail->next = temp;
temp->next = NULL;
temp->stud = stud;
tail = temp;
}
void insertAtPos(int position, struct Student *stud){
node *pehle = new node;
node *current = new node;
node *temp = new node;
current = head;
for (int i=1;i<=position;i++){
pehle = current;
current = current->next;
}
temp->stud = stud;
pehle->next = temp;
temp->next = current;
}
};
int main(){
struct Student student[15], stud;
int rndum;
for(int i=0;i<13;i++){
// cin>>student1.studentId = 10;
// cin>>student1.name = "Rambo";
// cin>>student1.address="LA , sesame street";
// cin>>student1.gpa = 4.5;
rndum = 2*i+2;
student[i].studentId = rndum;
student[i].name = "Rambo";
student[i].address="LA , sesame street";
student[i].gpa = 4.5;
}
student[13].studentId = 1;
student[13].name = "Rambo";
student[13].address="LA , sesame street";
student[13].gpa = 4.5;
student[14].studentId = 13;
student[14].name = "Rambo";
student[14].address="LA , sesame street";
student[14].gpa = 4.5;
List l1, l2;
l1.createNode(&student[0]);
for (int i = 1;i<15;i++){
l1.insert(&student[i]);
}
l1.display();
// Sorting and inserting in second linked list , sorting with student id
// int min = student[0].studentId ,pos;
// for (int i = 0;i<15; i++){
// if ( student[i].studentId < min){
// min = student[i].studentId;
// stud = student[i];
// pos = i;
// }
// }
// l2.createNode(&stud);
// student[pos].studentId = 655555565;
// min = 6555565;
// for(int j=1;j<15;j++){
// for (int i = 0;i<15; i++){
// if ( student[i].studentId < min ){
// min = student[i].studentId;
// stud = student[i];
// pos = i;
// }
// }
// l2.insert(&stud);
// student[pos].studentId = 655555565;
// }
// cout<<"secdsfsd"<<endl;
// l2.display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.