Array-Based Implementations

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Link-Based Implementations
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Sections 3.4 Formal Specification
Chapter 13: Overloading and Templates
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Abstract Data Types and Encapsulation Concepts
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Inheritance and Polymorphism
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
Bag Implementations that Use Arrays
Types of Programming Languages
C++ Classes C++ Interlude 1
C++ Classes C++ Interlude 1.
CMSC 341 Lecture 5 Stacks, Queues
Lecture 9 Concepts of Programming Languages
Implementations of the ADT Stack
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 4 Link Based Implementations
Abstract Data Types and Encapsulation Concepts
Chapter 3 Array-Based Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
List Implementations Chapter 9
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
List Implementations Chapter 9.
Chapter 16 Tree Implementations
Java – Inheritance.
Sorted Lists and Their Implementations
Overriding Methods & Class Hierarchies
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Array-Based Implementations
Chapter 8: Class Relationships
CIS 199 Final Review.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
Lists CMSC 202, Version 4/02.
Chapter 11 Class Inheritance
Lists CMSC 202, Version 4/02.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

Array-Based Implementations Chapter 3

The Approach An ADT is A collection of data … and … A set of operations on that data Specifications indicate What ADT operations do But not how to implement First step for implementation Choose data structure

Poor ADT Design Violating the wall of ADT operations by allowing client program to access data members of class directly.

Core Methods Poor approach Define entire class and attempt to test Better plan – Identify, then test basic (core) methods Typical core methods Create the container (constructors) Setters Getters Add items Display/list items Remove items

Using Fixed-Size Arrays Must keep track of array elements used, available Consider if the add method places elements in consecutive positions of array What happens when add method has used up final available position? Where can elements be removed? What happens when last element is removed?

Array-Based Implementation of ADT Sack Core ArraySack methods +getCurrentSize(): integer + isEmpty(): boolean +add(item: T): boolean +remove(item: T): boolean +clear(): void +contains(item: T) :boolean Also; Constructors (plus copy), assignment operator, destructor, iostream operator overloads.

Array-Based Implementation of ADT Sack An array-based implementation of the ADT sack

SackInterface – Chapter 3 Template class – supports generics, the class collection may hold any valid C++ type/class. All methods are virtual allowing the derived class to override the method, thus late binding occurs at execution time not compile time – which method to call – the child’s or parent’s? All methods are abstract = 0, no bodies meaning the class can not be instantiated, no objects of the class can be created. The class is a pure abstract class, abstract if at least one method is virtual with an empty body, pure because all methods are defined as such (except destructor). The class interface is used for inheritance of specification – all derived classes of the interface must include the interface methods.

ArraySack ADT The header file for the class ArraySack Code for header and implementation are available on lecture page of class web site Notice use of template parameter T Notice use of inheritance; ArraySack is a child class of the SackInterface class Inheritance is “public” – everything that is public in Base class is public in Derived class Go over method specifications and data member declarations

Defining the Core Methods Go over method implementations

Defining the Core Methods Inserting a new entry into an array-based sack

Methods That Remove Entries The array items after a successful search for the string "Alice"

Methods That Remove Entries A gap in the array items after the entry in items[index] is removed and decrementing itemCount;

Methods That Remove Entries Could shift subsequent entries to avoid a gap as in figure or simply copy last entry into gap (as in next slide)

Methods That Remove Entries Avoiding a gap in the array while removing an entry

Testing the Core Methods Go over main implementation

End Chapter 3