COS30008 Semester 1, 2020 Dr. Markus Lumpe
1
Swinburne University of Technology
Faculty of Science, Engineering and Technology
ASSIGNMENT COVER SHEET
Subject Code: COS30008
Subject Title: Data Structures & Patterns
Assignment number and title: 6 – Copy Control & Container Types
Due date: May 25, 2020, 12:30
Lecturer: Dr. Markus Lumpe
Your name: Your student id:
| Check Tutorial |
Mon 14:30 |
Tues 08:30 |
Tues 10:30 |
Tues 12:30 |
Tues 14:30 |
Wed 08:30 |
Wed 10:30 |
Wed 12:30 |
Wed 14:30 |
Thurs 08:30 |
Marker’s comments:
| Problem | Marks | Obtained |
| 1 | 38 | |
| 2 | 32 | |
| Total excluding 4&5 | 70 |
Extension certification:
This assignment has been given an extension and is now due on
Signature of Convener:
COS30008 Semester 1, 2020 Dr. Markus Lumpe
2
Problem Set 6: Copy Control & Container Types
Preliminaries
Review the solution of tutorial 7 & 9 & 10 and problem set 5, the lecture material regarding
the construction of an abstract data type, and how copy control is achieved for a given data
type.
Problem 1:
Add proper copy control to the template class List that we developed in tutorial 10.
#pragma once
#include “DoublyLinkedNode.h”
#include “DoublyLinkedNodeIterator.h”
#include <stdexcept>
template<class T>
class List
private:
// auxiliary definition to simplify node usage
typedef DoublyLinkedNode<T> Node;
| Node* fFirst; Node* fLast; unsigned int fCount; |
// the first element in the list // the last element in the list // number of elements in the list |
public:
// auxiliary definition to simplify iterator usage
typedef DoublyLinkedNodeIterator<T> Iterator;
List(); // default constructor – creates empty list
~List(); // destructor – frees all nodes
// PS6: copy control features (destructor is given)
List( const List& aOtherList ); // copy constructor
List& operator=( const List& aOtherList ); // assignment operator
| bool isEmpty() const; int size() const; |
// Is list empty? // list size |
void push_front( const T& aElement ); // adds a node aElement at front
| void push_back( const T& aElement ); void remove( const T& aElement ); |
// adds a node aElement at back // removes first node that matches aElement |
const T& operator[]( unsigned int aIndex ) const; // list indexer
| Iterator begin() const; Iterator end() const; Iterator rbegin() const; Iterator rend() const; |
// return a forward iterator // return a forward end iterator // return a backwards iterator // return a backwards end iterator |
;
COS30008 Semester 1, 2020 Dr. Markus Lumpe
3
Test harness
string s1( “AAAA” );
string s2( “BBBB” );
string s3( “CCCC” );
string s4( “DDDD” );
string s5( “EEEE” );
List<string> lList;
lList.push_front( s4 );
lList.push_front( s3 );
lList.push_front( s2 );
List<string> copy( lList );
// iterate from the top
cout << “A – Top to bottom: ” << copy.size() << ” elements” << endl;
for ( const string& element : copy )
cout << “A list element: ” << element << endl;
// override list
lList = copy;
lList.push_front( s1 );
lList.push_back( s5 );
// iterate from the top
cout << “B – Bottom to top: ” << lList.size() << ” elements” << endl;
for ( List<string>::Iterator iter = lList.rbegin();
iter != iter.rend(); iter– )
cout << “A list element: ” << *iter << endl;
The result should look like this:
A – Top to bottom: 3 elements
A list element: BBBB
A list element: CCCC
A list element: DDDD
B – Bottom to top: 5 elements
A list element: EEEE
A list element: DDDD
A list element: CCCC
A list element: BBBB
A list element: AAAA
COS30008 Semester 1, 2020 Dr. Markus Lumpe
4
Problem 2:
Using the template class List with proper copy control, implement the template class Stack
as specified below:
#pragma once
#include “List.h”
#include <stdexcept>
template<class T>
class Stack
private:
List<T> fElements;
public:
typedef typename List<T>::Iterator Iterator;
bool isEmpty() const;
int size() const;
void push( const T& aItem );
void pop();
const T& top() const;
// stack indexer
const T& operator[]( size_t aIndex ) const;
// stack iterators
Iterator begin() const;
Iterator end() const;
Iterator rbegin() const;
Iterator rend() const;
;
That is, Stack is a stack container type that can grow in size on demand.
Complete the implementation of the template class Stack.
COS30008 Semester 1, 2020 Dr. Markus Lumpe
5
Test harness
Stack<string> lStack;
lStack.push( “AAAA” );
lStack.push( “BBBB” );
lStack.push( “CCCC” );
lStack.push( “DDDD” );
// iterate from the top
cout << “A – Top to bottom: ” << lStack.size() << ” elements” << endl;
for ( const string& item : lStack )
cout << “A stack element: ” << item << endl;
// iterate from the bottom
cout << “B – Bottom to top: ” << lStack.size() << ” elements” << endl;
for ( Stack<string>::Iterator iter = lStack.rbegin();
iter != iter.rend(); iter– )
cout << “A list element: ” << *iter << endl;
cout << “3rd element from the top: ” << lStack[2] << endl;
cout << “Stack functions: ” << endl;
cout << “top: ” << lStack.top() << endl;
lStack.pop();
cout << “top: ” << lStack.top() << endl;
lStack.pop();
cout << “top: ” << lStack.top() << endl;
cout << “size: ” << lStack.size() << endl;
cout << “is empty: ” << (lStack.isEmpty() ? “T” : “F” ) << endl;
lStack.pop();
cout << “top: ” << lStack.top() << endl;
lStack.pop();
cout << “is empty: ” << (lStack.isEmpty() ? “T” : “F” ) << endl;
Result:
A – Top to bottom: 4 elements
A stack element: DDDD
A stack element: CCCC
A stack element: BBBB
A stack element: AAAA
B – Bottom to top: 4 elements
A list element: AAAA
A list element: BBBB
A list element: CCCC
A list element: DDDD
3rd element from the top: BBBB
Stack functions:
top: DDDD
top: CCCC
top: BBBB
size: 2
is empty: F
top: AAAA
is empty: T
| Submission deadline: Submission procedure: |
Monday, May 25, 2020, 12:30 PDF of printed code for class ListPS6 and Stack. |
The post COS30008 Data Structures & Patterns appeared first on My Assignment Online.