Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
C++ Assignment Help
For any help regarding CPP Assignment Help
Visit :- https://www.cpphomeworkhelp.com/ ,
Email :- info@cpphomeworkhelp.com or
Call us at :- +1 678 648 4277
Problem 1: C++ Linked List Library (cpplist)
Your job is now to refactor your C code from the second assignment and produce a much more
flexible library with similar functionality– and this time in C++. Download the zipped folder provided in
the file cpplist.zip as a basis of your program and take a look at the existing code.
• Write your implementation code in .cpp files in the src/ directory; feel free to add more header
cpphomeworkhelp.com
Problem
files as needed in the include/ directory.
•GRADER INFO.txtis a file for the grader (don’t edit!) containing PROG: cpplist,
LANG: C++
As before: you should submit a zipped folder using the same directory structure as
the provided zip file. We’ve added a section in the Makefile for your convenience: if
you type make zip in the same folder as your project, a zip file containing all of your
code and the required headers will be constructed in the project directory and you
can upload that to the grader.
We are provided a header file describing the interface for the List data structure. Look
in the file list.h
to find the functionality required of the other functions, which you will write.
II Forward declaration of applyIreduce types class
ApplyFunction;
class ReduceFunction;
cpphomeworkhelp.com
int value( size t pos ) const;?
• What is a forward declaration?
• Why is there a function int& value( size t pos ); as well as a function
class List {
II ... put whatever private data members you need here II can
also add any private member functions you'd like
public: List();
-List();
size t length() const; int& value( size t
pos );
int value( size t pos ) const; void append( int
value ); void deleteAll( int value );
void insertBefore( int value, int before ); void apply(
const ApplyFunction &interface );
int reduce( const ReduceFunction &interface ) const; void
print() const;
};
II ..etc
Some questions to ask yourself for understanding:
cpphomeworkhelp.com
• Why don’t we include the headers apply.h and reduce.h here?
You’llfind those two other header files containing definitions for yourApplyFunctionand
ReduceFunction
classes, shown here:
#include "list.h"
class ReduceFunction { protected:
virtual int function( int x, int y ) const = 0; public:
int reduce( const List &list ) const; virtual int
identity() const = 0; virtual -ReduceFunction() {}
};
II An example ReduceFunction
class SumReduce : public ReduceFunction { int function(
int x, int y ) const;
public: SumReduce() {}
-SumReduce() {}
int identity() const { return 0; }
};
and then in the source code file:
cpphomeworkhelp.com
#include "list.h" #include "reduce.h"
II This works fine, but iterating over a list like this is II fairly slow. See
if you can speed it up!
int ReduceFunction::reduce( const List &list ) const { int result =
identity();
for( size t p = 0; p < list.length(); ++p ) { result = function(
result, list.value( p ) );
}
return result;
}
int SumReduce::function( int x, int y ) const { return x + y;
}
Input/Output Format
Not applicable; your library will be compiled into a testing suite, your implemented functions will be
called by the program, and the behavior checked for correctness. For example, here is a potential test:
#include "list.h" int main() {
int N = 5;
II you'll need to write a copy constructor II to be able to do
this (see Lecture 5) auto list = List{};
cpphomeworkhelp.com
for( int i = 0; i < N; ++i ) { list.append( i );
}
list.print(); return 0;
}
Upon calling this function, the code outputs
{ 0 -> 1 -> 2 -> 3 -> 4 }
or whatever your formatted output from list.print() is made to look like. You are strongly encour
aged to write your own tests in test.cpp so that you can try out your implementation code before
submitting it to the online grader.
Best Practices
The problem is only worth 500/1000 points when you submit; the rest of the grade will be based
on how well your code follows C++ best practices and object-oriented programming principles.
See a list of those here. The rubric for the other 500 points is as follows.
• +500 points: Code is eminently readable, follows best practices, highly efficient, well
structured, and extensible.
cpphomeworkhelp.com
• +400 points: Code is easy to follow, only a few small violations of best practices, and
extensible.
• +300 points: Adecent refactoring effort, no egregiously bad practices, might be difficult to
extend.
• +200 points: Some refactoring effort, lots of violations of best practices, not very
extensible
• +100 points: Minor refactorings/improvements, little effort to follow best practices.
• +0 points: No effort to refactor or improve code (basically direct copy of HW#2)
cpphomeworkhelp.com
Look in list.h for a sense of the structure of the solution. The big idea to speed up the
reduce/apply functions while also giving users a nice way to iterate over the items in the list is to
create an "iterator" type within our class. Users will be able to write code similar to the STL:
// Print out every item in the list for( List::iterator it = list.begin(); it != list.end(); ++it ) {
std::cout < < *it << "n"; }
To speed up our "append" function, the List class will also store a pointer to the very last element
in the current list.
Directory structure:
•GRADER_INFO.txt
•include
•apply.h
•list.h
•list_node.h
•reduce.h
•Makefile
•src
•apply.cpp
•list.cpp
•list_iterator.cpp
•list_node.cpp
•reduce.cpp
•test.cpp
Solutions
cpphomeworkhelp.com
Here are the contents of apply.h:
#ifndef _6S096_CPPLIST_APPLY_H
#define _6S096_CPPLIST_APPLY_H
#include "list.h" class ApplyFunction {
protected:
virtual int function( int x ) const = 0;
public:
void apply( List &list ) const;
virtual ~ApplyFunction() {}
};
// An example ApplyFunction (see apply.cpp)
class SquareApply : public ApplyFunction { int
function( int x ) const;
};
#endif // _6S096_CPPLIST_APPLY_H
Here are the contents of list.h:
#ifndef _6S096_CPPLIST_H
#define _6S096_CPPLIST_H
cpphomeworkhelp.com
#include <cstddef>
#include <stdexcept>
class ApplyFunction;
class ReduceFunction;
class ListNode;
class List
{ size_t _length;
ListNode *_begin;
ListNode *_back;
public: // Can use outside as List::iterator type
class iterator { // Making List a friend class means we'll be able to access
// the private _node pointer data within the scope of List.
friend class List;
ListNode *_node;
public:
iterator( ListNode *theNode );
iterator& operator++();
int& operator*();
cpphomeworkhelp.com
#include <cstddef>
#include <stdexcept>
class ApplyFunction;
class ReduceFunction;
class ListNode;
bool operator==( const iterator &rhs );
bool operator!=( const iterator &rhs ); }; //
Can use outside as List::const_iterator type
class const_iterator {
// Again, this is basically the only situation you should
// be using the keyword 'friend'
friend class List;
ListNode *_node;
public:
const_iterator( ListNode *theNode );
const_iterator& operator++();
const int& operator*(); bool operator==( const const_iterator &rhs );
bool operator!=( const const_iterator &rhs );
};
List(); List( const List &list );
List& operator=( const List &list )
; ~List(); size_t length()const; int& value( size_t pos )
cpphomeworkhelp.com
; int value( size_t pos ) const;
bool empty() const;
iterator begin();
const_iterator begin() const;
iterator back();
const;
iterator back();
const_iterator back()
const; iterator end();
const_iterator end()
const;
iterator find( iterator s, iterator t, int needle );
void append( int theValue );
void deleteAll( int theValue );
void insertBefore( int theValue, int before );
void insert( iterator pos, int theValue );
void apply( const ApplyFunction &interface );
int reduce( const ReduceFunction &interface ) const;
void print() const;
void clear();
private: ListNode* node( iterator it ) { return it._node;
}
ListNode* node( const_iterator it ) { return it._node;
} };
class ListOutOfBounds :
cpphomeworkhelp.com
#ifndef _6S096_CPPLIST_NODE_H
#define _6S096_CPPLIST_NODE_H
class ListNode
{ int _value; ListNode *_next;
ListNode( const ListNode & ) = delete;
ListNode& operator=( const ListNode & ) = delete;
public:
ListNode();
ListNode( int theValue );
~ListNode();
int& value();
int value() const; ListNode* next();
void insertAfter( ListNode *before );
void setNext( ListNode *nextNode );
static void deleteNext( ListNode *before );
static void deleteSection( ListNode *before,
ListNode *after );
static ListNode* create( int theValue = 0 ); };
public std::range_error
{
public:
explicit ListOutOfBounds() : std::range_error( "List
index out of bounds" ) {} };
#endif // _6S096_CPPLIST_H
Here are the contents of list_node.h:
cpphomeworkhelp.com
#endif // _6S096_CPPLIST_NODE_H
Here are the contents of reduce.h:
#ifndef _6S096_CPPLIST_REDUCE_H
#define _6S096_CPPLIST_REDUCE_H
#include "list.h"
class ReduceFunction {
protected:
virtual int function( int x, int y ) const = 0;
public:
int reduce( const List &list )
const; virtual int identity() const = 0;
virtual ~ReduceFunction() {} };
// An example ReduceFunction
class SumReduce :
public ReduceFunction
{ int function( int x, int y ) const;
public:
SumReduce()
{} ~SumReduce() {}
int identity() const { return 0; } }; // Another ReduceFunction
class ProductReduce :
public ReduceFunction { int function( int x, int y )
const;
public:
ProductReduce() {}
~ProductReduce() {}
cpphomeworkhelp.com
iint identity() const { return 1; } };
#endif // _6S096_CPPLIST_REDUCE_H
Here is the source code file apply.cpp:
#include "list.h" #include "apply.h"
void
ApplyFunction::apply( List &list )
const
{ for( auto it = list.begin(); it != list.end(); ++it ) {
*it = function( *it );
}
}
int SquareApply::function( int x ) const {
return x * x; }
Here is the source code file list.cpp:
#include "list.h"
#include "list_node.h"
#include "apply.h"
#include "reduce.h"
#include <iostream>
List::List() : _length{0}, _begin{
nullptr }, _back{ nullptr } {}
List::List(
const List &list ) : _length{0}, _begin{nullptr}, _back{
cpphomeworkhelp.com
nullptr} {
for( auto it = list.begin(); it != list.end(); ++it )
{ append( *it );
}
}
List& List::operator=( const List &list ) {
if( this != &list ) { clear();
for( auto it = list.begin(); it != list.end(); ++it ) { append( *it ); } }
return *this;
} List::~List() { clear(); }
} } return *this; }
List::~List() { clear(); } size_t List::length() const {
return _length; } int& List::value( size_t pos ) { auto it = begin();
for( size_t i = 0; i < pos && it != end(); ++it, ++i );
if( it == end() ) {
throw ListOutOfBounds(); }
return *it; } int List::value( size_t pos )
const { auto it = begin();
for( size_t i = 0; i < pos && it != end(); ++it, ++i );
if( it == end() ) {
Throw
void List::append( int theValue )
{ auto *newNode = ListNode::create( theValue );
if( empty() ) {
cpphomeworkhelp.com
newNode->setNext( _back );
_begin = newNode;
} else
{ newNode->insertAfter( _back );
}
_back = newNode;
++_length;
}
void List::deleteAll( int theValue ) {
if( !empty() ) {
// Delete from the front
while( _begin->value() == theValue && _begin != _back ) {
auto *newBegin = _begin->next(); delete _begin; _begin =
newBegin; --_
length; }
auto *p = _begin;
if( _begin != _back ) {
// Normal deletion from interior of list
for( ; p->next() != _back; )
{ if( p->next()->value() == theValue )
{ ListNode::deleteNext( p ); --
_length; } else {
p = p->next();
}
}
cpphomeworkhelp.com
// Deleting the last item
if( _back->value() == theValue ) {
ListNode::deleteNext( p ); _
back = p;
--_length; }
} else if( _begin->value() == theValue )
{
// Deal with the case where we deleted the whole list _
begin = _back = nullptr; _
length = 0;
}
}
}
List::iterator List::find( iterator s, iterator t, int needle ) {
for( auto it = s; it != t; ++it ) {
if( *it == needle )
{ return it;
}
} return t; }
void List::insert( iterator pos, int theValue ) {
auto *posPtr = node( pos );
auto *newNode = ListNode::create( theValue );
newNode->insertAfter( posPtr ); ++_length; }
void List::insertBefore( int theValue, int before ) {
if( !empty() ) {
cpphomeworkhelp.com
if( _begin->value() == before )
{ auto *newNode = ListNode::create(
theValue );
newNode->setNext( _begin ); _begin =
newNode;
++_length; }
else { auto *p = _begin;
for( ; p != _back && p->next()->value() !=
before; p = p->next() );
if( p != _back && p->next()->value() ==
before ) {
auto *newNode = ListNode::create( theValue );
newNode->insertAfter( p );
++_length;
}
}
}
}
void List::apply(
const ApplyFunction &interface ) {
interface.apply( *this ); } int List::reduce(
const ReduceFunction &interface ) const {
return interface.reduce( *this ); }
void List::print()
cpphomeworkhelp.com
const { std::cout << "{ ";
for( auto it = begin(); it != back(); ++it ) {
std::cout << *it << " -> "; }
if( !empty() ) {
std::cout << *back() << " "; }
std::cout << "}n"; }
void List::clear() {
for( auto *p = _begin; p != nullptr; ) {
auto *p_next = p->next();
delete p; p = p_next; } _length = 0;
_begin = nullptr; _
back = nullptr;
}
Here is the source code file list_iterator.cpp:
#include "list.h" #include "list_node.h" List::iterator::iterator( ListNode *theNode ) :
_node{theNode} {} List::iterator& List::iterator::operator++() { _node = _node-
>next(); return *this; }
int& List::iterator::operator*() { return _node->value(); } bool
List::iterator::operator==( const iterator &rhs ) { return _node == rhs._node; } bool
List::iterator::operator!=( const iterator &rhs ) { return _node != rhs._node; }
List::const_iterator::const_iterator( ListNode *theNode ) : _node{theNode} {}
List::const_iterator& List::const_iterator::operator++() { _node = _node->next(); return
*this; } const int& List::const_iterator::operator*() { return _node->value(); } bool
List::const_iterator::operator==( const const_iterator &rhs ) { return _node ==
rhs._node; } bool List::const_iterator::operator!=(
cpphomeworkhelp.com
const const_iterator &rhs ) {
return _node != rhs._node;
}
Here is the source code file list_node.cpp:
#include "list_node.h"
ListNode::ListNode() :
_value{0}, _next{nullptr} {} ListNode::ListNode( int theValue ) :
_value{theValue}, _next{nullptr} {} ListNode::~ListNode() {} int&
ListNode::value() { return _value; } int ListNode::value(){const { return _value;
} ListNode* ListNode::next() { return _next; } void ListNode::insertAfter(
ListNode *before ) { _next = before->next(); before->_next = this;
}
void ListNode::setNext( ListNode *nextNode ) { _next = nextNode; } void
ListNode::deleteNext( ListNode *before ) { auto *after = before->next()->next();
delete before->next(); before->_next = after; } void ListNode::deleteSection(
ListNode *before, ListNode *after ) { auto *deleteFront = before->next(); while(
deleteFront != after ) { auto *nextDelete = deleteFront->next(); delete deleteFront;
deleteFront = nextDelete; } } ListNode* ListNode::create( int theValue ) { return
new ListNode{ theValue }; }
Here is the source code file reduce.cpp:
#include "list.h" #include "reduce.h" int ReduceFunction::reduce(const List &list )
const { int result = identity(); for( auto it = list.begin(); it != list.end(); ++it ) {
result = function( result, *it ); } return result; }
cpphomeworkhelp.com
int SumReduce::function( int x, int y ) const { return x + y; }
int ProductReduce::function(int x, int y ) const { return x * y; }
Below is the output using the test data:
cpplist: 1: OK [0.004 seconds] OK!
2: OK [0.005 seconds] OK!
3: OK [0.005 seconds] OK!
4: OK [0.009 seconds] OK!
5: OK [0.006 seconds] OK!
6: OK [0.308 seconds] OK!
7: OK [0.053 seconds] OK!
8: OK [0.007 seconds] OK!
9: OK [0.005 seconds] OK!
10: OK [0.742 seconds] OK!

More Related Content

Similar to CPP Assignment Help

unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
08 -functions
08  -functions08  -functions
08 -functions
Hector Garzo
 
C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
Amusing C#
Amusing C#Amusing C#
Amusing C#
PVS-Studio
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
M-TEC Computer Education
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
bhargavi804095
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
kanaka vardhini
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
sathish sak
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Abdullah Turkistani
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 
Python Basics
Python BasicsPython Basics
Python Basics
tusharpanda88
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
oggyrao
 
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdfComplete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
shahidqamar17
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
Atul Setu
 

Similar to CPP Assignment Help (20)

unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
08 -functions
08  -functions08  -functions
08 -functions
 
C++ language
C++ languageC++ language
C++ language
 
Amusing C#
Amusing C#Amusing C#
Amusing C#
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdfComplete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 

More from C++ Homework Help

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
C++ Homework Help
 

More from C++ Homework Help (19)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
PECB
 
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Murugan Solaiyappan
 
Split Shifts From Gantt View in the Odoo 17
Split Shifts From Gantt View in the  Odoo 17Split Shifts From Gantt View in the  Odoo 17
Split Shifts From Gantt View in the Odoo 17
Celine George
 
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Liyana Rozaini
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
Nguyen Thanh Tu Collection
 
How to Store Data on the Odoo 17 Website
How to Store Data on the Odoo 17 WebsiteHow to Store Data on the Odoo 17 Website
How to Store Data on the Odoo 17 Website
Celine George
 
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
Zuzana Mészárosová
 
L1 L2- NLC PPT for Grade 10 intervention
L1 L2- NLC PPT for Grade 10 interventionL1 L2- NLC PPT for Grade 10 intervention
L1 L2- NLC PPT for Grade 10 intervention
RHODAJANEAURESTILA
 
How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17
Celine George
 
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
thanhluan21
 
Final ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdfFinal ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdf
Zuzana Mészárosová
 
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Celine George
 
Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
EduSkills OECD
 
Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9
John Rodzvilla
 
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and RemediesArdra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Astro Pathshala
 
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISINGSYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
Dr Vijay Vishwakarma
 
Principles of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptxPrinciples of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptx
ibtesaam huma
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
SrimanigandanMadurai
 
The membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERPThe membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERP
Celine George
 
Conducting exciting academic research in Computer Science
Conducting exciting academic research in Computer ScienceConducting exciting academic research in Computer Science
Conducting exciting academic research in Computer Science
Abhik Roychoudhury
 

Recently uploaded (20)

AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
AI Risk Management: ISO/IEC 42001, the EU AI Act, and ISO/IEC 23894
 
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
 
Split Shifts From Gantt View in the Odoo 17
Split Shifts From Gantt View in the  Odoo 17Split Shifts From Gantt View in the  Odoo 17
Split Shifts From Gantt View in the Odoo 17
 
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 12 - GLOBAL SUCCESS - FORM MỚI 2025 - HK1 (C...
 
How to Store Data on the Odoo 17 Website
How to Store Data on the Odoo 17 WebsiteHow to Store Data on the Odoo 17 Website
How to Store Data on the Odoo 17 Website
 
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
Traces of the Holocaust in our communities in Levice Sovakia and Constanta Ro...
 
L1 L2- NLC PPT for Grade 10 intervention
L1 L2- NLC PPT for Grade 10 interventionL1 L2- NLC PPT for Grade 10 intervention
L1 L2- NLC PPT for Grade 10 intervention
 
How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17
 
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
DANH SÁCH THÍ SINH XÉT TUYỂN SỚM ĐỦ ĐIỀU KIỆN TRÚNG TUYỂN ĐẠI HỌC CHÍNH QUY N...
 
Final ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdfFinal ebook Keeping the Memory @live.pdf
Final ebook Keeping the Memory @live.pdf
 
Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17Views in Odoo - Advanced Views - Pivot View in Odoo 17
Views in Odoo - Advanced Views - Pivot View in Odoo 17
 
Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
 
Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9Beyond the Advance Presentation for By the Book 9
Beyond the Advance Presentation for By the Book 9
 
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and RemediesArdra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
 
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISINGSYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
SYBCOM SEM III UNIT 1 INTRODUCTION TO ADVERTISING
 
Principles of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptxPrinciples of Roods Approach!!!!!!!.pptx
Principles of Roods Approach!!!!!!!.pptx
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
 
The membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERPThe membership Module in the Odoo 17 ERP
The membership Module in the Odoo 17 ERP
 
Conducting exciting academic research in Computer Science
Conducting exciting academic research in Computer ScienceConducting exciting academic research in Computer Science
Conducting exciting academic research in Computer Science
 

CPP Assignment Help

  • 1. C++ Assignment Help For any help regarding CPP Assignment Help Visit :- https://www.cpphomeworkhelp.com/ , Email :- info@cpphomeworkhelp.com or Call us at :- +1 678 648 4277
  • 2. Problem 1: C++ Linked List Library (cpplist) Your job is now to refactor your C code from the second assignment and produce a much more flexible library with similar functionality– and this time in C++. Download the zipped folder provided in the file cpplist.zip as a basis of your program and take a look at the existing code. • Write your implementation code in .cpp files in the src/ directory; feel free to add more header cpphomeworkhelp.com Problem files as needed in the include/ directory. •GRADER INFO.txtis a file for the grader (don’t edit!) containing PROG: cpplist, LANG: C++ As before: you should submit a zipped folder using the same directory structure as the provided zip file. We’ve added a section in the Makefile for your convenience: if you type make zip in the same folder as your project, a zip file containing all of your code and the required headers will be constructed in the project directory and you can upload that to the grader. We are provided a header file describing the interface for the List data structure. Look in the file list.h to find the functionality required of the other functions, which you will write. II Forward declaration of applyIreduce types class ApplyFunction; class ReduceFunction;
  • 3. cpphomeworkhelp.com int value( size t pos ) const;? • What is a forward declaration? • Why is there a function int& value( size t pos ); as well as a function class List { II ... put whatever private data members you need here II can also add any private member functions you'd like public: List(); -List(); size t length() const; int& value( size t pos ); int value( size t pos ) const; void append( int value ); void deleteAll( int value ); void insertBefore( int value, int before ); void apply( const ApplyFunction &interface ); int reduce( const ReduceFunction &interface ) const; void print() const; }; II ..etc Some questions to ask yourself for understanding:
  • 4. cpphomeworkhelp.com • Why don’t we include the headers apply.h and reduce.h here? You’llfind those two other header files containing definitions for yourApplyFunctionand ReduceFunction classes, shown here: #include "list.h" class ReduceFunction { protected: virtual int function( int x, int y ) const = 0; public: int reduce( const List &list ) const; virtual int identity() const = 0; virtual -ReduceFunction() {} }; II An example ReduceFunction class SumReduce : public ReduceFunction { int function( int x, int y ) const; public: SumReduce() {} -SumReduce() {} int identity() const { return 0; } }; and then in the source code file:
  • 5. cpphomeworkhelp.com #include "list.h" #include "reduce.h" II This works fine, but iterating over a list like this is II fairly slow. See if you can speed it up! int ReduceFunction::reduce( const List &list ) const { int result = identity(); for( size t p = 0; p < list.length(); ++p ) { result = function( result, list.value( p ) ); } return result; } int SumReduce::function( int x, int y ) const { return x + y; } Input/Output Format Not applicable; your library will be compiled into a testing suite, your implemented functions will be called by the program, and the behavior checked for correctness. For example, here is a potential test: #include "list.h" int main() { int N = 5; II you'll need to write a copy constructor II to be able to do this (see Lecture 5) auto list = List{};
  • 6. cpphomeworkhelp.com for( int i = 0; i < N; ++i ) { list.append( i ); } list.print(); return 0; } Upon calling this function, the code outputs { 0 -> 1 -> 2 -> 3 -> 4 } or whatever your formatted output from list.print() is made to look like. You are strongly encour aged to write your own tests in test.cpp so that you can try out your implementation code before submitting it to the online grader. Best Practices The problem is only worth 500/1000 points when you submit; the rest of the grade will be based on how well your code follows C++ best practices and object-oriented programming principles. See a list of those here. The rubric for the other 500 points is as follows. • +500 points: Code is eminently readable, follows best practices, highly efficient, well structured, and extensible.
  • 7. cpphomeworkhelp.com • +400 points: Code is easy to follow, only a few small violations of best practices, and extensible. • +300 points: Adecent refactoring effort, no egregiously bad practices, might be difficult to extend. • +200 points: Some refactoring effort, lots of violations of best practices, not very extensible • +100 points: Minor refactorings/improvements, little effort to follow best practices. • +0 points: No effort to refactor or improve code (basically direct copy of HW#2)
  • 8. cpphomeworkhelp.com Look in list.h for a sense of the structure of the solution. The big idea to speed up the reduce/apply functions while also giving users a nice way to iterate over the items in the list is to create an "iterator" type within our class. Users will be able to write code similar to the STL: // Print out every item in the list for( List::iterator it = list.begin(); it != list.end(); ++it ) { std::cout < < *it << "n"; } To speed up our "append" function, the List class will also store a pointer to the very last element in the current list. Directory structure: •GRADER_INFO.txt •include •apply.h •list.h •list_node.h •reduce.h •Makefile •src •apply.cpp •list.cpp •list_iterator.cpp •list_node.cpp •reduce.cpp •test.cpp Solutions
  • 9. cpphomeworkhelp.com Here are the contents of apply.h: #ifndef _6S096_CPPLIST_APPLY_H #define _6S096_CPPLIST_APPLY_H #include "list.h" class ApplyFunction { protected: virtual int function( int x ) const = 0; public: void apply( List &list ) const; virtual ~ApplyFunction() {} }; // An example ApplyFunction (see apply.cpp) class SquareApply : public ApplyFunction { int function( int x ) const; }; #endif // _6S096_CPPLIST_APPLY_H Here are the contents of list.h: #ifndef _6S096_CPPLIST_H #define _6S096_CPPLIST_H
  • 10. cpphomeworkhelp.com #include <cstddef> #include <stdexcept> class ApplyFunction; class ReduceFunction; class ListNode; class List { size_t _length; ListNode *_begin; ListNode *_back; public: // Can use outside as List::iterator type class iterator { // Making List a friend class means we'll be able to access // the private _node pointer data within the scope of List. friend class List; ListNode *_node; public: iterator( ListNode *theNode ); iterator& operator++(); int& operator*();
  • 11. cpphomeworkhelp.com #include <cstddef> #include <stdexcept> class ApplyFunction; class ReduceFunction; class ListNode; bool operator==( const iterator &rhs ); bool operator!=( const iterator &rhs ); }; // Can use outside as List::const_iterator type class const_iterator { // Again, this is basically the only situation you should // be using the keyword 'friend' friend class List; ListNode *_node; public: const_iterator( ListNode *theNode ); const_iterator& operator++(); const int& operator*(); bool operator==( const const_iterator &rhs ); bool operator!=( const const_iterator &rhs ); }; List(); List( const List &list ); List& operator=( const List &list ) ; ~List(); size_t length()const; int& value( size_t pos )
  • 12. cpphomeworkhelp.com ; int value( size_t pos ) const; bool empty() const; iterator begin(); const_iterator begin() const; iterator back(); const; iterator back(); const_iterator back() const; iterator end(); const_iterator end() const; iterator find( iterator s, iterator t, int needle ); void append( int theValue ); void deleteAll( int theValue ); void insertBefore( int theValue, int before ); void insert( iterator pos, int theValue ); void apply( const ApplyFunction &interface ); int reduce( const ReduceFunction &interface ) const; void print() const; void clear(); private: ListNode* node( iterator it ) { return it._node; } ListNode* node( const_iterator it ) { return it._node; } }; class ListOutOfBounds :
  • 13. cpphomeworkhelp.com #ifndef _6S096_CPPLIST_NODE_H #define _6S096_CPPLIST_NODE_H class ListNode { int _value; ListNode *_next; ListNode( const ListNode & ) = delete; ListNode& operator=( const ListNode & ) = delete; public: ListNode(); ListNode( int theValue ); ~ListNode(); int& value(); int value() const; ListNode* next(); void insertAfter( ListNode *before ); void setNext( ListNode *nextNode ); static void deleteNext( ListNode *before ); static void deleteSection( ListNode *before, ListNode *after ); static ListNode* create( int theValue = 0 ); }; public std::range_error { public: explicit ListOutOfBounds() : std::range_error( "List index out of bounds" ) {} }; #endif // _6S096_CPPLIST_H Here are the contents of list_node.h:
  • 14. cpphomeworkhelp.com #endif // _6S096_CPPLIST_NODE_H Here are the contents of reduce.h: #ifndef _6S096_CPPLIST_REDUCE_H #define _6S096_CPPLIST_REDUCE_H #include "list.h" class ReduceFunction { protected: virtual int function( int x, int y ) const = 0; public: int reduce( const List &list ) const; virtual int identity() const = 0; virtual ~ReduceFunction() {} }; // An example ReduceFunction class SumReduce : public ReduceFunction { int function( int x, int y ) const; public: SumReduce() {} ~SumReduce() {} int identity() const { return 0; } }; // Another ReduceFunction class ProductReduce : public ReduceFunction { int function( int x, int y ) const; public: ProductReduce() {} ~ProductReduce() {}
  • 15. cpphomeworkhelp.com iint identity() const { return 1; } }; #endif // _6S096_CPPLIST_REDUCE_H Here is the source code file apply.cpp: #include "list.h" #include "apply.h" void ApplyFunction::apply( List &list ) const { for( auto it = list.begin(); it != list.end(); ++it ) { *it = function( *it ); } } int SquareApply::function( int x ) const { return x * x; } Here is the source code file list.cpp: #include "list.h" #include "list_node.h" #include "apply.h" #include "reduce.h" #include <iostream> List::List() : _length{0}, _begin{ nullptr }, _back{ nullptr } {} List::List( const List &list ) : _length{0}, _begin{nullptr}, _back{
  • 16. cpphomeworkhelp.com nullptr} { for( auto it = list.begin(); it != list.end(); ++it ) { append( *it ); } } List& List::operator=( const List &list ) { if( this != &list ) { clear(); for( auto it = list.begin(); it != list.end(); ++it ) { append( *it ); } } return *this; } List::~List() { clear(); } } } return *this; } List::~List() { clear(); } size_t List::length() const { return _length; } int& List::value( size_t pos ) { auto it = begin(); for( size_t i = 0; i < pos && it != end(); ++it, ++i ); if( it == end() ) { throw ListOutOfBounds(); } return *it; } int List::value( size_t pos ) const { auto it = begin(); for( size_t i = 0; i < pos && it != end(); ++it, ++i ); if( it == end() ) { Throw void List::append( int theValue ) { auto *newNode = ListNode::create( theValue ); if( empty() ) {
  • 17. cpphomeworkhelp.com newNode->setNext( _back ); _begin = newNode; } else { newNode->insertAfter( _back ); } _back = newNode; ++_length; } void List::deleteAll( int theValue ) { if( !empty() ) { // Delete from the front while( _begin->value() == theValue && _begin != _back ) { auto *newBegin = _begin->next(); delete _begin; _begin = newBegin; --_ length; } auto *p = _begin; if( _begin != _back ) { // Normal deletion from interior of list for( ; p->next() != _back; ) { if( p->next()->value() == theValue ) { ListNode::deleteNext( p ); -- _length; } else { p = p->next(); } }
  • 18. cpphomeworkhelp.com // Deleting the last item if( _back->value() == theValue ) { ListNode::deleteNext( p ); _ back = p; --_length; } } else if( _begin->value() == theValue ) { // Deal with the case where we deleted the whole list _ begin = _back = nullptr; _ length = 0; } } } List::iterator List::find( iterator s, iterator t, int needle ) { for( auto it = s; it != t; ++it ) { if( *it == needle ) { return it; } } return t; } void List::insert( iterator pos, int theValue ) { auto *posPtr = node( pos ); auto *newNode = ListNode::create( theValue ); newNode->insertAfter( posPtr ); ++_length; } void List::insertBefore( int theValue, int before ) { if( !empty() ) {
  • 19. cpphomeworkhelp.com if( _begin->value() == before ) { auto *newNode = ListNode::create( theValue ); newNode->setNext( _begin ); _begin = newNode; ++_length; } else { auto *p = _begin; for( ; p != _back && p->next()->value() != before; p = p->next() ); if( p != _back && p->next()->value() == before ) { auto *newNode = ListNode::create( theValue ); newNode->insertAfter( p ); ++_length; } } } } void List::apply( const ApplyFunction &interface ) { interface.apply( *this ); } int List::reduce( const ReduceFunction &interface ) const { return interface.reduce( *this ); } void List::print()
  • 20. cpphomeworkhelp.com const { std::cout << "{ "; for( auto it = begin(); it != back(); ++it ) { std::cout << *it << " -> "; } if( !empty() ) { std::cout << *back() << " "; } std::cout << "}n"; } void List::clear() { for( auto *p = _begin; p != nullptr; ) { auto *p_next = p->next(); delete p; p = p_next; } _length = 0; _begin = nullptr; _ back = nullptr; } Here is the source code file list_iterator.cpp: #include "list.h" #include "list_node.h" List::iterator::iterator( ListNode *theNode ) : _node{theNode} {} List::iterator& List::iterator::operator++() { _node = _node- >next(); return *this; } int& List::iterator::operator*() { return _node->value(); } bool List::iterator::operator==( const iterator &rhs ) { return _node == rhs._node; } bool List::iterator::operator!=( const iterator &rhs ) { return _node != rhs._node; } List::const_iterator::const_iterator( ListNode *theNode ) : _node{theNode} {} List::const_iterator& List::const_iterator::operator++() { _node = _node->next(); return *this; } const int& List::const_iterator::operator*() { return _node->value(); } bool List::const_iterator::operator==( const const_iterator &rhs ) { return _node == rhs._node; } bool List::const_iterator::operator!=(
  • 21. cpphomeworkhelp.com const const_iterator &rhs ) { return _node != rhs._node; } Here is the source code file list_node.cpp: #include "list_node.h" ListNode::ListNode() : _value{0}, _next{nullptr} {} ListNode::ListNode( int theValue ) : _value{theValue}, _next{nullptr} {} ListNode::~ListNode() {} int& ListNode::value() { return _value; } int ListNode::value(){const { return _value; } ListNode* ListNode::next() { return _next; } void ListNode::insertAfter( ListNode *before ) { _next = before->next(); before->_next = this; } void ListNode::setNext( ListNode *nextNode ) { _next = nextNode; } void ListNode::deleteNext( ListNode *before ) { auto *after = before->next()->next(); delete before->next(); before->_next = after; } void ListNode::deleteSection( ListNode *before, ListNode *after ) { auto *deleteFront = before->next(); while( deleteFront != after ) { auto *nextDelete = deleteFront->next(); delete deleteFront; deleteFront = nextDelete; } } ListNode* ListNode::create( int theValue ) { return new ListNode{ theValue }; } Here is the source code file reduce.cpp: #include "list.h" #include "reduce.h" int ReduceFunction::reduce(const List &list ) const { int result = identity(); for( auto it = list.begin(); it != list.end(); ++it ) { result = function( result, *it ); } return result; }
  • 22. cpphomeworkhelp.com int SumReduce::function( int x, int y ) const { return x + y; } int ProductReduce::function(int x, int y ) const { return x * y; } Below is the output using the test data: cpplist: 1: OK [0.004 seconds] OK! 2: OK [0.005 seconds] OK! 3: OK [0.005 seconds] OK! 4: OK [0.009 seconds] OK! 5: OK [0.006 seconds] OK! 6: OK [0.308 seconds] OK! 7: OK [0.053 seconds] OK! 8: OK [0.007 seconds] OK! 9: OK [0.005 seconds] OK! 10: OK [0.742 seconds] OK!