Question
In the llist.cc file write the implementation of the rearview function which will print the list backwards. This should be pretty easy since you have
In the llist.cc file write the implementation of the rearview function which will print the list backwards. This should be pretty easy since you have a function that prints the list frontwards right above that. Compile this and the lab9main.cc file. And run it.
Theres a problem something is not working right. Now its time to debug: gdb This time youre going to be using the commands to find your way on your own. Compile with the g option break function_name or break filename:line-number will stop the program at that spot run executes the program up until the break point, or until it crashes or until it ends n executes the line in front of you when youre single-stepping through a section s if the line in front of you is a function call this will step into the function p variable-name lets you see the contents of a variable, if you peek at a pointer you will see an address, if you peek at *pointer you will see the contents of the thing pointed at c continue the program to the next breakpoint, often used to cycle to the next iteration of a loop where if a program has crashed or been stopped with a Ctrl-c this will let you see the line where it was stopped, as well as all the other function calls that got you to this spot, a good first step in diagnosing a seg fault or infinite loop q quit
Please explain the bug
Then fix it, while keeping the unusual way that its putting items divisible by five in the middle of the list.
Test the code again.
Now write the implementation of a copy constructor, with a note that using the add function will not preserve the order of the items because of its unusual way of treating %5 == 0 items. Uncomment the section of the main that tests this.
Run your program and make a script file of the running.
main.cc
#include#include #include "llist.h" 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; }
list.cc
#include#include "llist.h" using namespace std; void LList::frontview()const{ node* cursor = head; while (cursor != NULL){ cout< data() <<" "; cursor = cursor->next(); } } // The student is required to write the implementation of this function void LList::rearview()const{ } 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; } }
list.h
#includeclass 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; };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started