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

Need help debugging thiis program. Output is an infinite loop of: Also need help

ID: 3601102 • Letter: N

Question

Need help debugging thiis program. Output is an infinite loop of:

Also need help writing the copy constructor.

Full code is as follows:

main.cpp

#include<iostream>

#include<cstdlib>

#include "llist.hpp"

using namespace std;

int main(){

  

int n;

LList somenums;

for(int i = 0; i <35; ++i)

{   n = rand()%700 + 1;

somenums.add_item(n);

}

// Once you have written the rear-view this should let you

// see the list frontwards and backwards.

somenums.frontview();

somenums.rearview();

// This part will be uncommented once you have written the copy constructor

/*

   {

   LList numcopy(somenums); // call to the copy constructor

   numcopy.frontview();

   numcopy.rearview();

   } // Line 34 - What happens here?

   // Checking the original list

   somenums.frontview();

   somenums.rearview();

   */

return 0;

}

llist.cpp

#include<iostream>

#include "llist.hpp"

using namespace std;

void LList::frontview()const{

node* cursor = head;

while (cursor != NULL){

cout<<cursor->data() <<" ";

cursor = cursor->next();

}

}

// The student is required to write the implementation of this function

void LList::rearview()const{

node* cursor = tail;

while (cursor != NULL){

cout<<cursor->data() <<" ";

cursor = cursor->previous();

}

}

void LList::add_item(int item){

if(head == NULL){

head=tail=new node(item);

nodecount++;

}

else if(item%5 == 0 && nodecount > 3){

int i = 0;

node* cursor=head;

while(i < nodecount/2){

cursor = cursor->next();

i++;

}

cursor->previous()->set_next(new node(item,cursor->next(),cursor->previous()));

nodecount++;

}

else{

tail->set_next(new node(item, tail));

tail = tail->next();

nodecount++;

}

}

LList::~LList(){

node* rmptr;

while(head != NULL){

rmptr=head;

head = head->next();

delete rmptr;

}

}

llist.h

#include<iostream>

class node{

public:

node(int d = 0, node* p = NULL, node* n = NULL){

datafield = d; previousptr=p; nextptr = n;

}

int data() {return datafield;}

node *previous() {return previousptr;}

node *next() {return nextptr;}

void set_data(int d){datafield = d;}

void set_previous(node * p){previousptr = p;}

void set_next(node *n) {nextptr = n;}

private:

int datafield;

node *nextptr;

node *previousptr;

};

class LList{

public:

LList(){

head = tail = NULL;

nodecount = 0;

}

void add_item(int item);

void frontview()const;

void rearview() const;

~LList();

int size()const {return nodecount;}

//LList(const LList& other);

private:

node* head;

node* tail;

int nodecount;

};

8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310 8 310

Explanation / Answer

#include<iostream>

#include<cstdlib>

using namespace std;

class node{

public:

node(int d = 0, node* p = NULL, node* n = NULL){

datafield = d; previousptr=p; nextptr = n;

}

int data() {return datafield;}

node *previous() {return previousptr;}

node *next() {return nextptr;}

void set_data(int d){datafield = d;}

void set_previous(node * p){previousptr = p;}

void set_next(node *n) {nextptr = n;}

private:

int datafield;

node *nextptr;

node *previousptr;

};

class LList{

public:

LList(){

head = tail = NULL;

nodecount = 0;

}

void add_item(int item);

void frontview()const;

void rearview() const;

~LList();

int size()const {return nodecount;}

LList(const LList& other);

private:

node* head;

node* tail;

int nodecount;

};

void LList::frontview()const{

node* cursor = head;

while (cursor != NULL){

cout<<cursor->data() <<" ";

cursor = cursor->next();

}

}

// The student is required to write the implementation of this function

void LList::rearview()const{

node* cursor = tail;

while (cursor != NULL){

cout<<cursor->data() <<" ";

cursor = cursor->previous();

}

}

void LList::add_item(int item){

if(head == NULL){

head=tail=new node(item);

nodecount++;

}

else if(0){

int i = 0;

node* cursor=head;

while(i < nodecount/2){

cursor = cursor->next();

i++;

}

cursor->previous()->set_next(new node(item,cursor->next(),cursor->previous()));

nodecount++;

}

else{

tail->set_next(new node(item, tail));

tail = tail->next();

nodecount++;

}

}

LList::~LList(){

node* rmptr;

while(head != NULL){

rmptr=head;

head = head->next();

delete rmptr;

}

}

LList::LList(const LList& other){

node* cursor = other.tail;

while (cursor != NULL){

add_item(cursor->data());

cout <<cursor->data()<<endl;

cursor = cursor->previous();

}

}

int main(){

  

int n;

LList somenums;

for(int i = 0; i <26; i++)

{ n = rand()%700 + 1;

//cout <<n<<endl;

somenums.add_item(n);

}

// Once you have written the rear-view this should let you

// see the list frontwards and backwards.

somenums.frontview();

somenums.rearview();

// This part will be uncommented once you have written the copy constructor

/*

{

LList numcopy(somenums); // call to the copy constructor

numcopy.frontview();

numcopy.rearview();

} // Line 34 - What happens here?

*/

// Checking the original list

somenums.frontview();

somenums.rearview();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote