Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views25 pages

Lecture 3 List ADT

This document outlines a lecture on data collection as an Abstract Data Type (ADT) and its implementation in C++. It covers the design of a List ADT, differentiates between data collections and data structures, and introduces arrays as a concrete data structure. The lecture also emphasizes the importance of encapsulating methods and variables within a C++ class and discusses the characteristics and operations of Lists.

Uploaded by

r9pmr6kyyh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Lecture 3 List ADT

This document outlines a lecture on data collection as an Abstract Data Type (ADT) and its implementation in C++. It covers the design of a List ADT, differentiates between data collections and data structures, and introduces arrays as a concrete data structure. The lecture also emphasizes the importance of encapsulating methods and variables within a C++ class and discusses the characteristics and operations of Lists.

Uploaded by

r9pmr6kyyh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CMPT 225 A grocery list

Lecture 3 – Data collection List – as an ADT

1
Learning Outcomes

 At the end of this lecture, a student will be able to:


 define data collection ADT (designed and implemented as
an abstract data type - ADT) and data structure (concrete
data type - CDT) and differentiate between the two
 define one of the concrete data types, namely array, and
demonstrate, simulate, and trace its operations
 convert specifications into high-level design, apply
software engineering and design concepts, and express
OO design in UML class diagrams
 write C++ code
 encapsulate methods and variables for an ADT into a C++
2 class
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Last Lecture

 Introducing the concept of Abstract Data Type (ADT)


 Definition + “Wall” metaphor 4 steps of the software development
process:
 How to design an ADT Step 1 - Problem statement +
 How to implement an ADT in C++ Requirements
Step 2 – Design
 How to test an ADT Step 3 – Implementation
Step 4 – Compilation and Testing
 Example: Temperature class
 Implemented as an ADT
 Implemented as a non-ADT -> effect: client code may
break the class invariant!
 List some of the advantages and disadvantages of ADT
3

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Today’s menu

 Overview of data collections


 Introduce our first data collection: List
 Design List as an ADT
1. Design the visible section of our List ADT class
 Its public interface –> its public section (the gaps in the wall)
2. Design the invisible section of our List ADT class
 Its private section (what is hidden behind the wall)
 Look at arrays (one of the concrete data structures)

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Terminology

Data Collection versus Data Structure


(abstract data type - ADT) (concrete data type - CDT)

Not every ADT  Data structures are


 ADT class that constructs available as
class is a data
collection models a collection part of a programming
language
class, but of data
every data  Examples:
collection  Example: List  array
class is an ADT
class.  linked list (nodes and
pointers)
 Used as member
attributes (data) of an
ADT
 Hidden behind the wall

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Terminology

Categories of data organizations

 Linear
 Data organization in which each element has a unique
predecessor (except for the first element, which has none)
and a unique successor (except for the last element,
which has none)
 Non-Linear
 Data organization in which there is no first element, no last
element and for each element, there is no concept of a
predecessor and a successor

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Terminology

Categories of data organizations – cont’d

 Hierarchical
 Data organization in which each element has only one
predecessor -> its parent (except for the first element,
which has none) and up to many successors (except for
the last element(s), which has none)
 Graph
 Data organization in which each element can have many
predecessors and many successors

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


What is a List in a real world?

 Characteristics?
 Duplications allowed?
 Sorted?

 Any constraints or limitations?

A grocery list

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


List as data collection in the software world

 Let’s introduce our first data collection: List while


solving a problem!
 Step 1 - Problem Statement + Requirements
FriendsBook Application
 Design and implement an application that maintains the
data for a simple social network.
 Each person in the network must have a profile that
contains the person’s name, optional image, current status
and a list of friends.
 Your application must allow a user to join the network by
creating a profile, leave the network, modify the profile,
9 search for other profiles, and add friends.
Source: Walls and Mirrors Textbook 6th Edition - Programming Problem 11 - Chapter 9 - Page 287
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Step 2 – Design

 Describe the behaviour of our FriendsBook application by


listing the steps it will perform when it will execute:
 Display menu
join the network (create a profile)
leave the network
modify the profile
search for other profiles
add friends.
 Read user choice
 Perform requested action
Ask user for more info, if needed
 Display results
 Repeat the above until user quits
10

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Step 2 – Design

FriendsBookApp Profile Collection of Profiles


- collection of profiles - name - profile[ 0 .. * ]
- image - number of profiles
- status
- number of friends
+ join - friends[ 0 .. * ]
+ leave
+ modify
+ search

11

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Step 2 – Design – Result - OPTIONAL

Record our design using a UML class diagram


Optional –
If you are curious, FriendsBook
here is a complete
UML class diagram - numberOfMembers : integer
Describing our - members : List

design so far. + join( newProfile : Profile ) : void


+ leave( profile : Profile ) : void
+ modify( profile : Profile ) : void
Note: We shall not + search( target : Profile ) : void
cover the UML syntax
used on this page in
this course. We shall 0..* Profile
- elementCount : integer
only cover the UML - elements : Profile[0..*]
- name : String
syntax covered on the - image : String
- status : String
previous slide. + insert( newPosition : integer, newElement: Profile): boolean
+ remove( position : integer ) : boolean - numberOfFriends : integer
+ clear( ) : void - friends : String[0..*]
+ getElement(position : integer): Profile
+ setElement(position : integer, newElement : Profile):void + addFriend( aFriend : String ) : void

12

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Step 2 – Design
 As part of Step 2, we need  So we decide to design
to decide which data Collection of Profiles
a List as the data
collection to use collection ADT class
- profile[ 0 .. * ]
- number of profiles
and whether data considering the
collection should keep problem we are
elements in a certain order solving, it makes no
sense to allow for
and whether duplicated duplicated elements
elements are possible
and what kind of data
structure we shall use to
implement it.

13

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Terminology

Two kinds of List

 Position-oriented
 Operations done based on position of data

 Value-oriented
 Operations done based on value of data

14

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Step 2 – Design List’s public interface

 insert




15
=> Always design operations optimizing their time efficient

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Example of position-oriented List operations

 insert an element at a given position in the list


 insert(element, position)
 append an element at the end of the list
 append(element)
 remove the element at a given position in the list
 remove(position)
 remove all the elements from the list
 removeAll( )
 get the element at a given position in the list
 element get(position)
 swap two elements
 swap(position1, position2)
 how many elements are in the list
 integer getElementCount( )
16

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Example of value-oriented List operations

 insert an element in the list at location determined by its


value
 insert(element)
 remove a specific element
 remove(element)
 remove all the elements from the list
 removeAll( )
 get a specific element
 element get(element)
 how many elements are in the list
 integer getElementCount( )
17

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


List as an Data Collection ADT class
Public interface (Specifications)
List ADT class

Insert

Data collection

Application or Which CDT


Search Private data members
test driver can we use
(client code) -> concrete data
structure to implement
i.e., other classes
our List?

The wall of a data collection ADT isolates its data structure


from the client code that uses it.
18

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Introduce our first concrete data structure:
Array
 Concrete data structure (CDT)
 Contiguous memory locations are used when an array is
allocated
 Indexed data structure
Direct access (as opposed to sequential access)

19

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


What can we do with an array

 Insert element into it – how?


 Remove element from it – how?
no need to “erase” an element from an array cell, simply
overwrite it
 Traverse (iterate through)
 Search for (find/get) a particular element – may not visit
every element
 “Expand” (resize) an array
(see Expandable Array under Lecture 3 for more information)

 Note: Easier to manage when there are no gaps in the array!


20

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Advantages of arrays

 Indexing (e.g., elements[3]) is very time efficient


 Because array cells are assigned contiguous memory
locations, traversing an array is very simple

21

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Disadvantages of arrays
 All elements stored in an array must have the same data type
 In C++, an array does not know its size (capacity), i.e., the
number of cells it has
 We must keep track of its size using a variable
 Size is required when creating an array
 If we overestimate the size of our array -> we waste memory
 If we underestimate the size of our array -> we risk running out
of space
Solution: expand array -> How expensive is this operation?
(see Expandable Array under Lecture 3 for more information)
 In C++, there are no bound check on array indices, so we must
ensure that our index is 0 <= index < array size
22

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University


Note about STL in C++

 Most of the time, in a software development project, we


do not design and implement data collections ADT classes,
instead we make use of what is already available
Examples: STL vectors

 But in CMPT 225, we will design our own data collection


ADT classes -> Why?

 This means that in our assignments and exams, we cannot


make use of library data collections ADT classes (like STL
23 vectors)
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
√ Learning Check
 We can now …
 Explain the difference between a data collection
(designed and implemented as an abstract data type -
ADT) and data structure (concrete data type - CDT) and
differentiate between the two
 List different categories of data organization
 Describe a List ADT
 Design a List as an ADT
Design its public interface (public section)
Design its invisible/hidden section (private section)
Using concrete data structure
24  Look at arrays (one of the concrete data structures)
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Next Lectures

 Step 3 – Implementation of the array-based List ADT


 Step 4 – Compilation and Testing of the array-based List
ADT
 Introduce test cases
 Introduce dynamically allocated memory
 Introduce linked lists (another concrete data structure)
 Implement the link-based List ADT and test it

25

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University

You might also like