Faculty of Information and Communication Technologies

Similar documents
CSCE 110 PROGRAMMING FUNDAMENTALS

Doubly-Linked Lists

Faculty of Science, Engineering and Technology

Short Notes of CS201

Pointers, Dynamic Data, and Reference Types

CS201 - Introduction to Programming Glossary By

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Dynamic Data Structures

CS

Financial computing with C++

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Link-Based Implementations. Chapter 4

ADT: Design & Implementation

Data Structures and Algorithms

STL: C++ Standard Library

CSC 210, Exam Two Section February 1999

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Object-Oriented Programming for Scientific Computing

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Lecture 5. Function Pointers

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Structuur van Computerprogramma s 2

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCE 110 PROGRAMMING FUNDAMENTALS

Exercise 6.2 A generic container class

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Lecture 23: Pointer Arithmetic

CS24 Week 4 Lecture 1

Programming in C++: Assignment Week 8

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Class and Function Templates

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

C++ (Non for C Programmer) (BT307) 40 Hours

And Even More and More C++ Fundamentals of Computer Science

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

CS3157: Advanced Programming. Outline

Assignment of Objects

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Module 1. C++ Classes Exercises

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Motivation for Templates

Topics. Constructor. Constructors

Chapter 17: Linked Lists

Consider the program...

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Instantiation of Template class

Intermediate Programming, Spring 2017*

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Array Elements as Function Parameters

Topic Binary Trees (Non-Linear Data Structures)

EL2310 Scientific Programming

Come and join us at WebLyceum

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Object oriented programming

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

List, Stack, and Queues

Data Structures and Algorithms

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

What does it mean by information hiding? What are the advantages of it? {5 Marks}

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

Common Misunderstandings from Exam 1 Material

C++ Programming Lecture 7 Software Engineering Group

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

C:\Temp\Templates. Download This PDF From The Web Site

CS24 Week 4 Lecture 2

Programming, numerics and optimization

C++ Templates. David Camp

Review: C++ Basic Concepts. Dr. Yingwu Zhu

pointers + memory double x; string a; int x; main overhead int y; main overhead

eingebetteter Systeme

CS201 Some Important Definitions

a data type is Types

Implementing an ADT with a Class

AN OVERVIEW OF C++ 1

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Polymorphism. Zimmer CSCI 330

Introduction to Classes

Object-Oriented Principles and Practice / C++

CPSC 427: Object-Oriented Programming

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

CSC212. Data Structure. Lecture 9 Templates, Iterators and STL. Instructor: George Wolberg Department of Computer Science City College of New York

Linked List using a Sentinel

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Transcription:

Swinburne University Of Technology Faculty of Information and Communication Technologies ASSIGNMENT COVER SHEET Subject Code: Subject Title: Assignment number and title: Due date: Lecturer: HIT3303 Data Structures & Patterns 2 Lists and Design Patterns April 7, 2009, 02:30 p.m., on paper Dr. Markus Lumpe Your name: Marker's comments: Problem Marks Obtained 1 27 2 79 Total 106 Extension certification: This assignment has been given an extension and is now due on Signature of Convener: 1

Problem Set 2: Lists and Design Patterns Preliminaries Study or review the following concepts: 1. C++ templates 2. What is difference between value semantics and reference semantics? 3. What is a constant reference? 4. What is a constant object? 5. What is an enumeration type? 6. What is a typedef declaration? 7. What is delete and how does it work? 8. When do we need destructors? 9. What is a state machine? 2

Problem 1: Define a double-linked list that satisfies the following template class specification: template<class DataType> class Node private: const DataType& fvalue; Node<DataType>* fnext; Node<DataType>* fprevious; public: Node( const DataType& avalue, Node<DataType>* anext = (Node<DataType>*)0, Node<DataType>* aprevious = (Node<DataType>*)0 ); ~Node(); const DataType& GetValue() const; const Node<DataType>* GetNext() const; const Node <DataType>* GetPrevious() const; ; The template class Node defines the structure of a double-linked list. It uses two pointers: fnext and fprevious to connect two adjacent list elements. The constructor takes avalue, anext, and aprevious as arguments and returns a properly initialized list node. The arguments anext and aprevious take a default argument (Node<DataType>*)0, which is a null-pointer to elements of type Node. The destructor destroys the list and releases all allocated resources (i.e., memory) in turn. The methods GetValue, GetNext, and GetPrevious and data providers that do not change the state of the corresponding object. As a result, objects of type Node are constant objects. There is, however, one complication. Template classes are class blueprints or, better, abstractions over classes. Before we can use template classes, we have to instantiate them. But, the instantiation process, to work correctly, requires also the implementation. For this reason, when defining template classes, the implementation has to be included in the header file. There are two ways to accomplish this: Implement the member functions directly in the class specification (like it is done in Java or C#). Implement the member functions outside the class specification but within the same header file. If you follow this scheme, working with templates is pretty straightforward. Implement class Node. 3

Test sample 1: void TestListImplementation1() string s1( "One" ); string s2( "Two" ); string s3( "Three" ); string s4( "Four" ); Node<string> n1( s1 ); Node<string> n2( s2, (Node<string>*)0, &n1 ); Node<string> n3( s3, (Node<string>*)0, &n2 ); Node<string> n4( s4, (Node<string>*)0, &n3 ); cout << "Forward:" << endl; for ( const Node<string>* nodes = &n1; nodes!= (Node<string>*)0; nodes = nodes->getnext() ) cout << nodes->getvalue() << endl; cout << "Backward:" << endl; for ( const Node<string>* nodes = &n4; nodes!= (Node<string>*)0; nodes = nodes->getprevious() ) cout << nodes->getvalue() << endl; Result: Forward: One Two Three Four Backward: Four Three Two One 4

Test sample 2: void TestListImplementation2() string s1( "One" ); string s2( "Two" ); string s3( "Three" ); Node<string>* pn1 = new Node<string>( s1 ); Node<string>* pn2 = new Node<string>( s2, (Node<string>*)0, pn1 ); Node<string>* pn3 = new Node<string>( s3, (Node<string>*)0, pn2 ); cout << "Tree elements:" << endl; for ( const Node<string>* nodes = pn1; nodes!= (Node<string>*)0; nodes = nodes->getnext() ) cout << "("; if ( nodes->getprevious()!= (Node<string>*)0 ) cout << nodes->getprevious()->getvalue(); else cout << "<NULL>"; cout << "," << nodes->getvalue() << ","; if ( nodes->getnext()!= (Node<string>*)0 ) cout << nodes->getnext()->getvalue(); else cout << "<NULL>"; cout << ")" << endl; delete pn2; cout << "Two elements:" << endl; for ( const Node<string>* nodes = pn1; nodes!= (Node<string>*)0; nodes = nodes->getnext() ) cout << "("; if ( nodes->getprevious()!= (Node<string>*)0 ) cout << nodes->getprevious()->getvalue(); else cout << "<NULL>"; cout << "," << nodes->getvalue() << ","; if ( nodes->getnext()!= (Node<string>*)0 ) cout << nodes->getnext()->getvalue(); else cout << "<NULL>"; cout << ")" << endl; delete pn3; delete pn1; 5

Result: Tree elements: (<NULL>,One,Two) (One,Two,Three) (Two,Three,<NULL>) Two elements: (<NULL>,One,Three) (One,Three,<NULL>) 6

Problem 2: Define a bi-directional list iterator for double-linked lists that satisfies the following template class specification: template<class DataType> class NodeIterator private: enum IteratorStates BEFORE, DATA, END ; const Node<DataType>* ftop; const Node<DataType>* flast; const Node<DataType>* fcurrent; IteratorStates fstate; public: NodeIterator( Node<DataType>* alist ); DataType operator*() const; // dereference NodeIterator& operator++(); // prefix increment NodeIterator operator++(int); // postfix increment NodeIterator& operator--(); // prefix decrement NodeIterator operator--(int); // postfix decrement bool operator==( const NodeIterator& aotheriter ) const; bool operator!=( const NodeIterator& aotheriter ) const; NodeIterator begin(); NodeIterator end(); ; The bi-directional list iterator implements the standard operators for iterators: dereference to access the current iterator element, both versions of increment to advance the iterator to the next element, and both versions of decrement to go backwards. The list iterator also defines the equivalence predicates and the two factory methods begin() and end(). The method begin() returns a new list iterator positioned before the first element of the double-linked list, whereas end() returns a new list iterator that is positioned after the last element of the doublelinked list. Implement the list iterator. Please note that the constructor of the list iterator has to properly set ftop, flast, and fcurrent. In particular, the constructor has to position the iterator on the first element of the list or yield an iterator equivalent to end() if the list is empty. An iterator must not change the underlying collection. However, in the case of NodeIterator we need a special marker to denote, whether the iterator is before the first list element or after the last list element. Since we cannot change the underlying list, we need to add state to the iterator. Using the iterator state (i.e., fstate) we can now clearly mark when the iterator is before the first element, within the first and the last element, or after the last element. 7

To guarantee to correct behavior of the NodeIterator, it must implement a state machine with three states: BEFORE, DATA, END. The following state transition diagram illustrates, how NodeIterator works: All increment and decrement operators have to test, whether the iterator is still positioned within the collection. In this case the current iterator is different from both begin() and end(). If the iterator is positioned before the first element, then it is equivalent to begin(). If the iterator is positioned past the last element, then it is equivalent to end(). Please note that the iterator can in one step become equivalent to begin() or end(). Implement class NodeIterator. 8

Test sample 3: void TestListIterator() Node<int> n1( 1 ); Node<int> n2( 2, (Node<int>*)0, &n1 ); Node<int> n3( 3, (Node<int>*)0, &n2 ); Node<int> n4( 4, (Node<int>*)0, &n3 ); Node<int> n5( 5, (Node<int>*)0, &n4 ); Node<int> n6( 6, (Node<int>*)0, &n5 ); cout << "Forward iteration:" << endl; NodeIterator<int> iter1( &n1 ); for ( ; iter1!= iter1.end(); iter1++ ) cout << *iter1 << endl; cout << "Backward iteration:" << endl; NodeIterator<int> iter2( &n6 ); for ( ; iter2!= iter2.begin(); iter2-- ) cout << *iter2 << endl; Result: Forward iteration: 1 2 3 4 5 6 Backward iteration: 6 5 4 3 2 1 Submission deadline: Tuesday, April 7, 2009, 2:30 p.m. Submission procedure: on paper. 9