Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please leave comments. c++ Hello can someone check to see why my code is not run

ID: 3594134 • Letter: P

Question

Please leave comments.

c++

Hello can someone check to see why my code is not running well. i am doing single and double linked list. the code is suppose to output this.

#include<iostream>

#include <iomanip>

#include <string>

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<<(std::ostream& os, const student& st) {

os << std::setprecision(2) << std::fixed;

return os << "student[" << std::setw(18) << (st.lname_ + "," + st.fname_)

<< ",gpa=" << std::setw(4) << st.gpa_ << ",cwid=" << st.cwid_ << "]";

}

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<<(std::ostream& os, const studentNode& node){

  

return os <<"[" <<node.st_<< "--->" << node.next_<<"]";

  

  

}

  

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.size_){

  

  

sli_append(sli, sli.head_);

  

  

  

  

  

  

}else{

studentNode* p=sli.head_;

studentNode* prev=p;

  

for(int i=0;i<index;++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=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(student(lname,fname,gpa, cwid),next) {}

  

  

  

  

dstudentNode(const student& st, dstudentNode* next=nullptr, dstudentNode* prev=nullptr)

: st_(st), next_(next) {}

  

  

friend std::ostream& operator<<(std::ostream& os, const dstudentNode& node){

  

return os <<"[" <<node.st_<< "--->" << node.next_<<"]";

  

};

  

  

  

student st_;

dstudentNode* next_;

dstudentNode* prev_;

};

//===================================================================================

//===================================================================================

// doubly-linked list

//

struct dstudentList {

  

dstudentList() : head_(nullptr), size_(0) {}

dstudentNode* head_;

dstudentNode* tail_;

size_t size_;

};

//===================================================================================

// doubly-linked list functions

//===================================================================================

void dli_prepend(dstudentList& dli, dstudentNode* node){

node->next_=dli.head_;

dli.head_=node;

++dli.size_;

  

  

}

void dli_prepend(dstudentList& dli, const std::string& lname, const std::string& fname,

   double gpa, int cwid){

  

  

  

dli_prepend(dli,new dstudentNode(lname,fname,gpa,cwid,dli.head_));

}

//===================================================================================

void dli_append(dstudentList& dli, dstudentNode* node){

dstudentNode* temp= dli.head_;

while(temp->next_ !=nullptr){

  

temp=temp->next_;

}

temp->next_=node;

  

  

}

void dli_append(dstudentList& dli, const std::string& lname, const std::string& fname,

double gpa, int cwid){

dli_append(dli,new dstudentNode(lname,fname,gpa,cwid,dli.head_));

  

}

//===================================================================================

void dli_insertAt(dstudentList& dli, size_t index, dstudentNode* node)

{

if(index==0){

  

dli_prepend(dli, node);

  

  

}else if (index<dli.size_){

  

  

dli_append(dli, dli.head_);

  

  

  

  

  

  

}else{

dstudentNode* p=dli.head_;

dstudentNode* prev=p;

  

for(int i=0;i<index;++i){

prev=p;

p=p->next_;

  

}

prev->next_=node;

  

}

++dli.size_;

}

  

  

  

void dli_insertAt(dstudentList& dli, size_t index,

const std::string& lname, const std::string& fname, double gpa, int cwid){

  

   dli_insertAt(dli,index, new dstudentNode(lname,fname,gpa,cwid,dli.head_));

  

  

}

//===================================================================================

void dli_delete(dstudentList& dli, size_t index);

//===================================================================================

void dli_display(dstudentList& dli){

dstudentNode* p=dli.head_;

while(p != nullptr){

std::cout<<*p<<"--->";

p=p->next_;

  

}

  

}

//===================================================================================

//

int main() {

std::cout << " ===================================================== ";

std::cout << "STUDENT LIST LAB ==================================== ";

std::cout << "... singly-linked list ";

  

student anon;

student al("Einstein", "Albert", 2.5, 12345);

  

std::cout << "anonymous is: " << anon << " ";

std::cout << "al is: " << al << " ";

  

// make new studentList, add new students and one existing

studentList sli;

sli_prepend(sli, "Newton", "Isaac", 4.0, 54321);

sli_append( sli, "Planck", "Max", 3.9, 23451);

sli_append( sli, "Edison", "Thomas", 3.9, 34512);

sli_append(sli, new studentNode(al));

  

sli_insertAt(sli, 0, new studentNode("Gauss", "Karl F.", 3.8, 45123));

sli_insertAt(sli, 4, new studentNode("Wright", "Wilbur", 3.4, 51234));

  

sli_display(sli); // display the students in the list

  

  

std::cout << " ===================================================== ";

std::cout << "===================================================== ";

std::cout << "STUDENT LIST LAB ==================================== ";

std::cout << "... doubly-linked list ";

  

student jeanluc("Picard", "Jean-Luc", 3.9, 13245);

  

std::cout << "jean-luc is: " << jeanluc << " ";

// make new studentList, add new students and one existing

dstudentList dli;

dli_prepend(dli, "Riker", "Will", 3.7, 11223);

  

dli_append( dli, "LaForge", "Geordie", 3.6, 51124);

dli_append( dli, "Data", "Lt. Cmdr.", 4.0, 55543);

dli_append(dli, new dstudentNode(jeanluc));

  

dli_insertAt(dli, 0, new dstudentNode("Crusher", "Beverly", 3.5, 11122));

dli_insertAt(dli, 4, new dstudentNode("Worf", "Lieutenant", 3.4, 33434));

  

dli_display(dli); // display the students in the list

std::cout << " ...done. ";

return 0;

  

};

// make new studentList, add new students and one existing dstudentList dli dli_prepend (dli, "Riker" Wll", 3.7, 11223) dli_append dli, "LaForge", "Geordie", 3.6, 51124); dli_append dli, "Data", "Lt. Cmdr.", 4.0, 55543) dli append(dli, new dstudentNode (jeanluc)); dli-insertAt(dli, dli_insertAt (dli, ' 4, new dstudentNode("crusher", "Beverly", new dstudentNode ("Worf", "Lieutenant", 3.5, 3.4. 11122)); 33434)); dli_display (dli); // display the students in the list td::coutnode [student[ -->node [student [ -->node [student -->node [student --nullptr Gauss , Karl F.,gpa-3.86,cwid-451231-->8x10 588] Newton, Isaac, gpa-4. 6, cwi d-54321]-->8x1eeseeed@] Planck, Max, gpa-3.96, cwid-23451]-->8x10500129] Edison, Thomas , gpa-3.98, cwid-34512]-->8x100500210] Wright, Wilbur, gpa-3.40, cwid-51234]->8x108500170] Einstein,Albert,gpa-2.50,Cwid-12345] -->0xe] .. . doubly-linked list jean-luc is student Picard,Jean-Luc,gpa-3.90,cwid-13245] node [0xe--student Crusher,Beverly,gpa-3.50.cwid-11122] -->0x100200200] -->node [6x102967984.-student [ -->node [0x10620200-student LaForge, Geordie,gpa-3.60,cwid-51124]-->0x1002602a0] -->node [6x102982 56x1ee2967eej Riker,Will,gpa-3.70,cwid-11223]-->0x100200250] node [0x1002002a0student --model6x1002ee7e&student [ -nullptr Worf.Lieutenant,gpa 3.40.cwid 33434]-->x106260740] Picard , Jean-Luc.gpa-3.96,cwid-132451-->6xej .. .done

Explanation / Answer

/*Changes done to the code – When node is inserted at last its next should be made NULL

Similarly when node is inserted first in Doubly linked list its prev should be made NULL

Wherever changes are done comments are added to that for better understanding*/

#include<iostream>

#include <iomanip>

#include <string>

using namespace std;

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<<(std::ostream& os, const student& st) {

        os << std::setprecision(2) << std::fixed;

        return os << "student[" << std::setw(18) << (st.lname_ + "," + st.fname_)

        << ",gpa=" << std::setw(4) << st.gpa_ << ",cwid=" << st.cwid_ << "]";

    }

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=NULL)

    : studentNode(student(lname,fname,gpa, cwid),next) {}

  

    studentNode(const student& st, studentNode* next=NULL)

    : st_(st), next_(next) {}

    friend std::ostream& operator<<(std::ostream& os, const studentNode& node){

       

        return os <<"[" <<node.st_<< "--->" << node.next_<<"]";

       

      

    }

   

    student st_;

    studentNode* next_;

};

//=================================================================================== // singly-linked list

struct studentList {

    studentList() : head_(NULL), 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;

    //cout<<sli.head_->st_.fname()<<"------------------ ";

    ++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_ !=NULL){

       

        temp=temp->next_;

    }

    temp->next_=node;

    node->next_=NULL;//Since "node" is Last node so make it next Node as NULL

   

}

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.size_){

       

       

        sli_append(sli, sli.head_);

       

       

       

       

       

       

    }else{

        studentNode* p=sli.head_;

        studentNode* prev=p;

       

        for(int i=0;i<index;++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_;

    cout<<"    ";

    while(p != NULL){

        std::cout<<"node"<<*p<<" --->";

        p=p->next_;

       

    }

    cout<<"nullptr ";

       

       

       

}

//=================================================================================== //=================================================================================== // doubly-linked list studentNode

struct dstudentNode {

    dstudentNode(const std::string& lname, const std::string& fname,

                 double gpa, int cwid,

                 dstudentNode* next=NULL, dstudentNode* prev=NULL)

    : dstudentNode(student(lname,fname,gpa, cwid),next) {}

   

   

   

   

    dstudentNode(const student& st, dstudentNode* next=NULL, dstudentNode* prev=NULL)

    : st_(st), next_(next) {}

   

   

    friend std::ostream& operator<<(std::ostream& os, const dstudentNode& node){

       

                return os <<"[" <<node.st_<< "--->" << node.next_<<"]";

       

    };

   

   

   

    student st_;

    dstudentNode* next_;

    dstudentNode* prev_;

};

//===================================================================================

//===================================================================================

// doubly-linked list

//

struct dstudentList {

   

    dstudentList() : head_(NULL), size_(0) {}

    dstudentNode* head_;

    dstudentNode* tail_;

    size_t size_;

};

//===================================================================================

// doubly-linked list functions

//===================================================================================

void dli_prepend(dstudentList& dli, dstudentNode* node){

        node->next_=dli.head_;

        if(dli.head_!=NULL)

               dli.head_->prev_=node;

        dli.head_=node;

        dli.head_->prev_=NULL;//Since it is the beginning node so its previous sholud be NULL

        ++dli.size_;

       

       

  }

void dli_prepend(dstudentList& dli, const std::string& lname, const std::string& fname,

                 double gpa, int cwid){

   

   

   

    dli_prepend(dli,new dstudentNode(lname,fname,gpa,cwid,dli.head_));

}

//===================================================================================

void dli_append(dstudentList& dli, dstudentNode* node){

    dstudentNode* temp= dli.head_;

    while(temp->next_ !=NULL){

       

        temp=temp->next_;

    }

    temp->next_=node;

    node->prev_=temp;

    node->next_=NULL;//Since "node" is the last node therefore its next should be NULL

   

}

void dli_append(dstudentList& dli, const std::string& lname, const std::string& fname,

                double gpa, int cwid){

      dli_append(dli,new dstudentNode(lname,fname,gpa,cwid,dli.head_));

   

}

//===================================================================================

void dli_insertAt(dstudentList& dli, size_t index, dstudentNode* node)

{

    if(index==0){

       

        dli_prepend(dli, node);

       

       

    }else if (index<dli.size_){

       

       

        dli_append(dli, dli.head_);

       

       

       

       

       

       

    }else{

        dstudentNode* p=dli.head_;

        dstudentNode* prev=p;

       

        for(int i=0;i<index;++i){

            prev=p;

            p=p->next_;

           

        }

        prev->next_=node;

        node->prev_=prev;

       

    }

    ++dli.size_;

}

   

   

   

void dli_insertAt(dstudentList& dli, size_t index,

                  const std::string& lname, const std::string& fname, double gpa, int cwid){

   

     dli_insertAt(dli,index, new dstudentNode(lname,fname,gpa,cwid,dli.head_));

   

   

}

//===================================================================================

void dli_delete(dstudentList& dli, size_t index);

//===================================================================================

void dli_display(dstudentList& dli){

    dstudentNode* p=dli.head_;

    cout<<"    ";

    while(p != NULL){

        std::cout<<"node"<<*p<<" --->";

        p=p->next_;

       

    }

    cout<<"nullptr ";

   

}

//===================================================================================

//

int main() {

    std::cout << " ===================================================== ";

    std::cout << "STUDENT LIST LAB ==================================== ";

    std::cout << "... singly-linked list ";

   

    student anon;

    student al("Einstein", "Albert", 2.5, 12345);

   

    std::cout << "anonymous is: " << anon << " ";

    std::cout << "al is: " << al << " ";

   

    // make new studentList, add new students and one existing

    studentList sli;

    sli_prepend(sli, "Newton", "Isaac", 4.0, 54321);

    sli_append( sli, "Planck", "Max", 3.9, 23451);

    sli_append( sli, "Edison", "Thomas", 3.9, 34512);

    sli_append(sli, new studentNode(al));

   

    sli_insertAt(sli, 0, new studentNode("Gauss", "Karl F.", 3.8, 45123));

    sli_insertAt(sli, 4, new studentNode("Wright", "Wilbur", 3.4, 51234));

   

    sli_display(sli); // display the students in the list

   

   

    std::cout << " ===================================================== ";

    std::cout << "===================================================== ";

    std::cout << "STUDENT LIST LAB ==================================== ";

    std::cout << "... doubly-linked list ";

   

    student jeanluc("Picard", "Jean-Luc", 3.9, 13245);

   

    std::cout << "jean-luc is: " << jeanluc << " ";

    // make new studentList, add new students and one existing

    dstudentList dli;

    dli_prepend(dli, "Riker", "Will", 3.7, 11223);

   

    dli_append( dli, "LaForge", "Geordie", 3.6, 51124);

    dli_append( dli, "Data", "Lt. Cmdr.", 4.0, 55543);

    dli_append(dli, new dstudentNode(jeanluc));

   

    dli_insertAt(dli, 0, new dstudentNode("Crusher", "Beverly", 3.5, 11122));

    dli_insertAt(dli, 4, new dstudentNode("Worf", "Lieutenant", 3.4, 33434));

   

    dli_display(dli); // display the students in the list

    std::cout << " ...done. ";

    return 0;

   

};