Question: hello, i need help with my linked list C++ code. please i want use this oppurtunity to learn this code so please leave comments to
hello, i need help with my linked list C++ code. please i want use this oppurtunity to learn this code so please leave comments to help me understand. here i did the preappend, the append, the insertA and other. please help me fill out the ones i need. code and instructions attached.
full main is given in the instructions.
#include
#include
#include
class student {
public:
student(const std::string& lname = "",const std::string& fname = "",
double gpa = 0.0, int cwid = 0)
: fname_(fname), lname_(lname), gpa_(gpa), cwid_(cwid) {}
std::string fname() const { return fname_; }
std::string lname() const { return lname_; }
double gpa() const { return gpa_; }
int cwid() const { return cwid_; }
void gpa(double gpa) { gpa_ = gpa; }
friend std::ostream& operator
os
return os
}
private:
std::string fname_;
std::string lname_;
double gpa_;
int cwid_;
};
//===================================================================================
// singly-linked list studentNode
struct studentNode {
studentNode(const std::string& lname, const std::string& fname,
double gpa, int cwid, studentNode* next=nullptr)
: studentNode(student(lname,fname,gpa, cwid),next) {}
studentNode(const student& st, studentNode* next=nullptr)
: st_(st), next_(next) {}
friend std::ostream& operator
return os "
}
student st_;
studentNode* next_;
};
//=================================================================================== // singly-linked list
struct studentList {
studentList() : head_(nullptr), size_(0) {}
studentNode* head_;
size_t size_;
};
//===================================================================================
// singly-linked list functions
//
//===================================================================================
void sli_prepend(studentList& sli, studentNode* node){
node->next_=sli.head_;
sli.head_=node;
++sli.size_;
}
void sli_prepend(studentList& sli, const std::string& lname, const std::string& fname,
double gpa, int cwid){
sli_prepend(sli,new studentNode(lname,fname,gpa,cwid,sli.head_));
}
//===================================================================================
void sli_append(studentList& sli, studentNode* node)
{
studentNode* temp= sli.head_;
while(temp->next_ !=nullptr){
temp=temp->next_;
}
temp->next_=node;
}
void sli_append(studentList& sli, const std::string& lname, const std::string& fname,
double gpa, int cwid){
sli_append(sli,new studentNode(lname,fname,gpa,cwid,sli.head_));
}
//===================================================================================
void sli_insertAt(studentList& sli, size_t index, studentNode* node){
if(index==0){
sli_prepend(sli, node);
}else if (index sli_append(sli, sli.head_); }else{ studentNode* p=sli.head_; studentNode* prev=p; for(int i=0;i prev=p; p=p->next_; } prev->next_=node; } ++sli.size_; } void sli_insertAt(studentList& sli, size_t index, const std::string& lname, const std::string& fname, double gpa, int cwid) { sli_insertAt(sli,index, new studentNode(lname,fname,gpa,cwid,sli.head_)); } //=================================================================================== void sli_deleteAt(studentList& sli, size_t index); //=================================================================================== void sli_display(studentList& sli){ studentNode* p=sli.head_; while(p != nullptr){ std::cout"; p=p->next_; } } //=================================================================================== //=================================================================================== // doubly-linked list studentNode struct dstudentNode { dstudentNode(const std::string& lname, const std::string& fname, double gpa, int cwid, dstudentNode* next=nullptr, dstudentNode* prev=nullptr); dstudentNode(const student& st, dstudentNode* next=nullptr, dstudentNode* prev=nullptr); friend std::ostream& operator student st_; dstudentNode* next_; dstudentNode* prev_; }; //=================================================================================== //=================================================================================== // doubly-linked list // struct dstudentList { dstudentList(); dstudentNode* head_; dstudentNode* tail_; size_t size_; }; //=================================================================================== // doubly-linked list functions //=================================================================================== void dli_prepend(dstudentList& dli, dstudentNode* node); void dli_prepend(dstudentList& dli, const std::string& lname, const std::string& fname, double gpa, int cwid); //=================================================================================== void dli_append(dstudentList& dli, dstudentNode* node); void dli_append(dstudentList& dli, const std::string& lname, const std::string& fname, double gpa, int cwid); //=================================================================================== void dli_insertAt(dstudentList& dli, size_t index, dstudentNode* node); void dli_insertAt(dstudentList& dli, size_t index, const std::string& lname, const std::string& fname, double gpa, int cwid); //=================================================================================== void dli_delete(dstudentList& dli, size_t index); //=================================================================================== void dli_display(dstudentList& dli); //=================================================================================== // int main() { std::cout std::cout std::cout student anon; student al("Einstein", "Albert", 2.5, 12345); std::cout std::cout // make new studentList, add new students and one existing studentList sli; sli_prepend(sli, "Newton", "Isaac", 4.0, 54321); } 




Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
