I am trying to insert student records into a linked list ordered by student id.
ID: 3859895 • Letter: I
Question
I am trying to insert student records into a linked list ordered by student id. What is wrong with my insert function??? My whole code is given below.
main cpp:
#include "DynStudentStack.h"
#include "student.h"
#include "LinkedList.h"
#include
using namespace std;
int main() {
student studentRec[15];
DynStudentStack studentInfo;
LinkedList linkedStudentRec;
student s1("Johnny", s1.getTestavg(), "123 WonderWorld", s1.getId());
student s2("Chris",s2.setTestavg(), "123 Craddock Ave",s2.getId());
student s3("Marc",s3.getTestavg(),"123 Aquarena Springs Dr.", s3.getId());
student s4("Collin",s4.getTestavg(),"123 Ranch Road",s4.getId());
student s5("Tim", s5.getTestavg(),"123 Post Road",s5.getId());
student s6("Tate",s6.getTestavg(),"123 Thorpe Ln",s6.getId());
student s7("Ashley",s7.getTestavg(),"123 Hopkins St.", s7.getId());
student s8("Morgan",s8.getTestavg(),"123 Hunter Rd",s8.getId());
student s9("Phillip",s9.getTestavg(),"123 Holland St",s9.getId());
student s10("Carol",s10.getTestavg(),"123 N. LBJ Dr.",s10.getId());
student s11("Sarah",s11.getTestavg(),"123 University Dr.",s11.getId());
student s12("Stephanie",s12.getTestavg(),"123 Guadalupe St.",s12.getId());
student s13("Logan",s13.getTestavg(),"123 Mathews St.",s13.getId());
student s14("Steve",s14.getTestavg(),"123 Comanche St.",s14.getId());
student s15("Brandon",s15.getTestavg(),"123 Uhland Rd.",s15.getId());
studentRec[0]=s1;
studentRec[1]=s2;
studentRec[2]=s3;
studentRec[3]=s4;
studentRec[4]=s5;
studentRec[5]=s6;
studentRec[6]=s7;
studentRec[7]=s8;
studentRec[8]=s9;
studentRec[9]=s10;
studentRec[10]=s11;
studentRec[11]=s12;
studentRec[12]=s13;
studentRec[13]=s14;
studentRec[14]=s15;
for(int i = 0; i<15; i++){
linkedStudentRec.insertNode(studentRec[i]);
}
linkedStudentRec.displayList();
return 0;
}
//////////////////////////////////////////////
student.h:
#ifndef STUDENT_H_
#define STUDENT_H_
#include
#include //For rand and srand
#include //For the time function
using namespace std;
class student {
private:
string sName;
int sTestavg;
string sAddress;
int sId;
void getInfo(){
cout << sName << " Test Average: " << sTestavg << " Address: "<< sAddress<< " Student Id: " <
}
public:
//constructor
student(){
}
//overloaded constructor
student(string name, int testavg, string address, int id ){
sName = name;
sTestavg = testavg;
sAddress = address;
sId = id;
}
//defined setter methods
void setName(string name){
sName = name;
}
int setTestavg(){
int *testScores; //pointer for 10 test grades
int testGrades[10];
int totalGrade;
int grade;
testScores = testGrades;
int MAX_VALUE = 100;
int MIN_VALUE = 60;
for (int i = 0; i < 10; i++){
grade = ((rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE);
testGrades[i]=grade;
}
for( int i = 0; i < 10; i++){
totalGrade += *(testScores + i);
}
return sTestavg = totalGrade/10;
}
void setAdress(string address){
sAddress = address;
}
int setId(){
int MAX_VALUE = 9999;
int MIN_VALUE = 1000;
return sId = ((rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE);
}
//defined getter methods
string getName(){
return sName;
}
int getTestavg(){
return setTestavg();
}
string getAddress(){
return sAddress;
}
int getId(){
return setId();
}
//display function
void display(){
getInfo();
}
};
#endif /* STUDENT_H_ */
/////////////////////////////////////////////
dynamicstack.h
#ifndef DYNSTUDENTSTACK_H_
#define DYNSTUDENTSTACK_H_
#include "student.h"
#include
using namespace std;
class DynStudentStack{
private:
//structure for stack nodes
struct StackNode{
student value; //value in the node
StackNode *next;//pointer to the next node
};
StackNode *top; //pointer to the stack top
public:
//constructor
DynStudentStack(){
top = nullptr;
}
//destructor
~DynStudentStack();
//stack operations
void push(student);
void pop(student &);
bool isEmpty();
};
DynStudentStack::~DynStudentStack(){
StackNode *nodePtr = nullptr, *nextNode = nullptr;
//position nodePtr at the top of the stack
nodePtr = top;
//traverse the list deleting each node
while(nodePtr != nullptr){
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
void DynStudentStack::push(student x){
StackNode *newNode = nullptr;
//allocate a new node and store the student there
newNode = new StackNode;
newNode->value = x;
//If there are no nodes in the list
//make newNode the first node
if(isEmpty()){
top = newNode;
newNode->next=nullptr;
}
else{//otherwise insert newnode before top
newNode->next = top;
top = newNode;
}
}
void DynStudentStack::pop(student &x){
StackNode *temp = nullptr; //Temporary pointer
if(isEmpty()){
cout << "The stack is empty. ";
}else{
x = top->value;
temp = top->next;
delete top;
top = temp;
}
}
bool DynStudentStack::isEmpty(){
bool status;
if(!top){
status = true;
}else{
status = false;
}
return status;
}
#endif /* DYNSTUDENTSTACK_H_ */
//////////////////////////////////////////////////////////////////
Linkedlist.h
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
using namespace std;
class LinkedList {
private:
struct ListNode {
int sTestAvg;
int sId;
string sName;
string sAddress;
int count; //keep count of order
struct ListNode *next; //to point to the next node
};
ListNode *head; //List head pointer
public:
//Constructor
LinkedList() {
head = nullptr;
}
//Destructor
~LinkedList();
//Linked list operations
void displayList() const;
void insertNode(student);
void deleteNode(int);
};
void LinkedList::deleteNode(int position) {
ListNode *nodePtr;
ListNode *previousNode;
if (!head) {
cout << "Student Records are empty." << endl;
return;
}
if (position == 1) { //if first node is the target
nodePtr = head->next;
delete head;
head = nodePtr;
cout << "Deleted the " << position << "position from record.";
} else {
nodePtr = head;
for (int i = 1; i < position; i++) {
if (nodePtr != nullptr) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr == nullptr) {
cout << "No such position";
return;
}
}
previousNode->next = nodePtr->next;
delete nodePtr;
cout << " " << endl;
cout << " Deleted position " << position << " from record." << endl;
cout << " " << endl;
}
}
void LinkedList::insertNode(student x) {
ListNode *newNode; //a new node
ListNode *nodePtr;
ListNode *previousNode = nullptr; //previous node
newNode = new ListNode;
newNode->sName = x.getName();
newNode->sTestAvg = x.getTestavg();
newNode->sAddress = x.getAddress();
newNode->sId = x.getId();
//if there are no nodes in the list make newNode first node
if (!head) {
head = newNode;
newNode->next = nullptr;
} else {
nodePtr = head;
previousNode = nullptr;
while (nodePtr != nullptr && nodePtr->sId < x.getId()) { //skips all nodes whos sId is less than id
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == nullptr) { // if the new node is to be the first in list
head = newNode;
newNode->next = nodePtr;
} else { //insert after the previous node
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void LinkedList::displayList() const {
ListNode *nodePtr; //to move through the list
//Position nodePtr at the head of the list
nodePtr = head;
//while nodePtr points to a node, traverse the list
while (nodePtr) {
//display the information
cout << "Student Name: " << nodePtr->sName << " Average Test Grade: " << nodePtr->sTestAvg
<< " Address: " << nodePtr->sAddress << " ID: " << nodePtr->sId
<< endl;
//Move to the next node
nodePtr = nodePtr->next;
}
}
LinkedList::~LinkedList() {
ListNode *nodePtr; //traverse the list
ListNode *nextNode; //to point to the next node
nodePtr = head; //starts at begining of list
while (nodePtr != nullptr) { //while the nodePtr is not at the end of list
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
#endif /* LINKEDLIST_H_ */
results:
2 5 Student Name: Tate Average Test Grade: 86 Address: 123 Thorpe Ln ID: 2009 Student Name: Carol Average Test Grade: 81 Address: 123 N. LBJ Dr. ID: 2617 Student Name: Steve Average Test Grade: 78 Address: 123 Comanche St. ID: 2321 Student Name: Ashley Average Test Grade: 74 Address: 123 Hopkins St. ID: 2815 Student Name: Brandon Average Test Grade: 80 Address: 123 Uhland Rd. ID: 4044 Student Name: Logan Average Test Grade: 86 Address: 123 Mathews St. ID: 3701 Student Name: Stephanie Average Test Grade: 73 Address: 123 Guadalupe St. ID: 5549 Student Name: Sarah Average Test Grade: 79 Address: 123 University Dr. ID: 2555 Student Name: Tim Average Test Grade: 84 Address: 123 Post Road ID: 3314 Student Name: Morgan Average Test Grade: 76 Address: 123 Hunter Rd ID: 2433 Student Name: Collin Average Test Grade: 78 Address: 123 Ranch Road ID: 7224 Student Name: Marc Average Test Grade: 72 Address: 123 Aquarena Springs Dr . ID: 5813 Student Name: Phillip Average Test Grade: 83 Address: 123 Holland St ID: 6075 Student Name: Chris Average Test Grade: 80 Address: 123 Craddock Ave ID: 7900 Student Name: Johnny Average Test Grade: 76 Address: 123 Wonderworld ID: 702 8 2 5 5 7 8Explanation / Answer
//you can use this code, it is working #include #include #include #include #include #include #include #include using namespace std; bool check = true; struct node //structure of node // { char name[20]; char discipline[20]; int rollNo; char section; node *next; }*head,*lastptr; void add() //Adds record of student// { node *p; p=new node; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.