Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 – Data collection List ADT

Similar presentations


Presentation on theme: "Lecture 4 – Data collection List ADT"— Presentation transcript:

1 Lecture 4 – Data collection List ADT
CMPT 225 Lecture 4 – Data collection List ADT

2 Last Lecture Overview of data collections
Introduce our first data collection: List Design List as an ADT 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) Data structure itself -> array Size of array Number of elements currently stored in the array Look at arrays (one of the concrete data structures)

3 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data types, namely array, and demonstrate, simulate, and trace its operations write C++ code encapsulate methods and variables for an ADT into a C++ class manage memory in C++, allocate and free arrays and individual elements in heap differentiate between memory declared globally, on the stack, and on the heap design and use test cases

4 Today’s menu Position-oriented versus value-oriented Lists – operations issues? Implement the array-based List ADT and test it Introduce dynamically allocated memory Stack-allocated array and heap-allocated array Introduced test cases and a test driver -> more in our Labs

5 Position-oriented List versus value-oriented List – issues related to their operations
insert remove Value-oriented List insert remove

6 Step 3 - Documentation Header comment block
Class/file name Class description Class invariant (if any) Author Creation/modification date For each public and private methods Description Precondition Postcondition

7 Step 3 - Array-based implementation List ADT
Public interface (Specifications) List ADT class FriendsBook (client code) array of Profile objects Etc... Class data members (private): Insert Search

8 Step 3 – Implementing List ADT class
FriendsBook Demo online

9 Stack versus heap memory allocation
Memory layout /* Header Comment Block */ ... class List { private: static const int INITIAL_SIZE = 5; Patient elements[INITIAL_SIZE]; OR Patient* elements; int elementCount; int capacity; public: // Destructor ~List(); insert(...) }; List.h stack heap static code delete [] elements; elements = new Patient[capacity]; if (elements == NULL) // error! else // insert new element

10 Step 3 - Array-based implementation List ADT
Summary of heap-allocated array new delete [ ] destructor method When to use stack-allocated memory? When to use heap-allocated memory?

11 Step 4 – Testing: Test driver and test cases
Each class must be tested using a test driver program Test driver program must call each of the class’ public methods at least once Test driver program is made of many test cases A test case: Specific Test data Expected results Actual results

12 Example of test cases for our List ADT class
insert( )

13 Step 4 – Testing – Test driver
See example of TestDriver on course web site

14 √ Learning Check We can now … Implement a List ADT using an array
stack-allocated or heap-allocated Differentiate between stack-allocated and heap- allocated arrays Create test cases and a test driver and test our List ADT Describe test driver program Describe test case Use List ADT in our software solution

15 Next Lecture Pointers and linked lists
A second implementation of our List ADT class: Link-based implementation of List ADT class Move on to our next ADT’s


Download ppt "Lecture 4 – Data collection List ADT"

Similar presentations


Ads by Google