In the llist.cc file write the implementation of the rearview function which wil
ID: 3600481 • Letter: I
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 a function that prints the list frontwards right above that. Compile this and the lab9main.cc file. And run it.
There’s a problem – something is not working right. Now it’s time to debug:
gdb – This time you’re 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 you’re 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 it’s 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
list.cc
list.h
Explanation / Answer
add getter methods to the list to get the head and tail of the LList :
here's the code for rearview and the copy constructor along with the getter methods :
---------------------------------------------------------------------------------------------------------------------------
// rearview()
// tail has the end of the list
// from that using previous node link we can traverse in the reverse order. the implemented method is //same as the front view provided
void list::rearview()const{
node* cursor = tail;
while( cursor != NULL ){
cout << cursor->data() << " ";
cursor = cursor -> previous;
}
}
-----------------------------------------------------------------------------------------------------------
// copy constructor
/* as both the head and tail are pointer references we can acces the same list with this by assigning
* the reference to the pointers
*/
LList :: LList( const LList& other ){
this -> head = other.get_head();
this->tail = other.get_tail();
}
------------------------------------------------------------------------------------------------------------------
// getter methods used above :
node* LList :: get_head(){
return head;
}
node* LList :: get_tail(){
return tail;
}
-------------------------------------------------------------------------------------------------------------
/* hope this helps */
/* if any queries please comment */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.