overview of lecture

26
Dr. Nazli Mollah Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Overview of Lecture Overview of Lecture Introduction to Data Structure Storage Containers Abstract Data Types (ADT) Classes Application Programmer Interface (API) Strings

Upload: joan-nelson

Post on 31-Dec-2015

30 views

Category:

Documents


0 download

DESCRIPTION

Overview of Lecture. Introduction to Data Structure Storage Containers Abstract Data Types (ADT) Classes Application Programmer Interface (API) Strings. What is a Data Structure?. A data structure is a systematic way of organizing and accessing data - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Overview of LectureOverview of Lecture

Introduction to Data Structure Storage Containers Abstract Data Types (ADT) Classes Application Programmer Interface (API) Strings

Page 2: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

A data structure is a systematic way of organizing and accessing data

Programmer-defined data structures bundle data with operations that manipulate the data

The structures, called containers have operations to access, insert,

and remove items from the collection.

What is a Data Structure?What is a Data Structure?

Page 3: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Storage ContainersStorage Containers

Containers such as vectors, lists or maps are storage structures that provide Containers such as vectors, lists or maps are storage structures that provide ways to access data and insert/delete itemsways to access data and insert/delete items

Vectors Lists Maps

Page 4: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Storage Containers: VectorsStorage Containers: Vectors

A vector has all of the nice indexing features of an array along with the ability to dynamically grow to meet demand – aka “super array”

Allows for direct access to its elements through the index

We have the option to growing or shrinking vectors

7 4 9

7 4 9 3 1 0 0 0

7 4 9 3

vec to r v (w ith 5 e lem en ts )

v.res ize (8 ); (gro w to 8 e lem en ts )

1

v.res ize (3 ); (sh rin k to 3 e lem en ts )

Page 5: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Storage Containers: VectorsStorage Containers: Vectors

Limitations to Vectors: Not an efficient storage structure for general insertion and deletion of items at an arbitrary position in the list Suppose an application wants to store elements in order and a vector is used:

The insert operation requires shifting a block of elements to the right to make room for a new item

The deletion operation requires shifting a bloc of elements to the right to make room for the new item

Problem: High overhead of maintaining Vectors when collection of elements are large and the application calls for frequent insertion and deletion of items

Solution: List containers

3 0 3 51 5 2 0 3 0 3 5

1 5 2 0 3 0 3 51 5 2 0 3 0 3 5 4 0

4 02 5In sert 2 5 a t

P o s it io n 2

V ecto r 4 0

1 5 4 0

2 0

E rase 2 0 a tP o s it io n 1

V ecto r

S h ift righ t S h ift le ft

Page 6: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Storage Containers: ListsStorage Containers: Lists

Each element has a reference that identifies the next item in the list.

Adding a new item involves breaking a link in the chain and creating two new links to connect the item

fro n t rea r

/ /

n ew ItemReference identifying next item on list

The insertion occurs locally in the list and does not require the moving of the other elements to make room for the new element

Page 7: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Storage Containers: MapsStorage Containers: Maps

Arrays, vectors, and lists store elements by position: 0,1,2 etc., thus to access an element efficiently, we must know its position in the list – otherwise item-by-item search of list – not very effective for large list with say 100,000 elements – time consuming iterations of a loop to locate the position.

Solution: Maps – which use tree structure to store data

Data is stored by value and not position

e.g. an airline reservation/ mail has a tracking number consisting of alphanumeric strings

TV 93

W 29Z

N T2P

D 29Z

F A 27

B 40A

R o o t

G 9B 7

H 14K

Map: structure where the index is represented by a value not position

Maps use tree structures that maintain system files and directories and can parse expressions – very useful where trees hold a large number of items

e.g.D29Z requires at most 3 iterations along a path from the root

Page 8: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Abstract view of Data Structures

To understand the design of a data structure, we use an abstract model that specifies the

type of data stored operations that support the data.

Abstract Data Types (ADT’s) are a model used to understand the design of a data structure.

The main feature if an ADT is a simple and clear description of the operations in the data structure

“abstract” implies that the data structure is given an implementation-independent view

viewing a data structure as an ADT allows a programmer to focus on an idealized model of the data and its

operations.

Abstract Data Types (ADT)Abstract Data Types (ADT)

Page 9: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

ADT Operation DescriptionADT Operation Description

An ADT provides simple and clear description of: the input to an operation. the action of the operation. its return type.

Operation NameOperation Name: Action statement that specifies the input arguments type of operation on the elements of the date structure output value

PreconditionsPreconditions: Part of the description of an operation. A listing of the conditions that must apply in order for the operation to execute successfully.

PostconditionsPostconditions: Indicate changes to the object's data caused by the operation. Necessary because operations often alter the value of data.

If the operation does not have a precondition or a postcondition, the ADT operation description disregards the condition

Page 10: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

An ADT as a ClassAn ADT as a Class

An ADT provides an abstract model of a data structure: The view is like an architect’s first sketch of a house – layout & # of rooms

But becomes reality only when a(n): architect creates a structural blueprint contractor builds the walls and interior

For an ADT, we need to create a: physical representation of the data computer model for the operations

The object-oriented C++ language is ideal for this: The class construct provides a physical realization of an ADT The class declaration is the blueprint The writing of the implementation code actually builds the class

Page 11: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

The time24 ADTThe time24 ADT

Consider a data structure that uses integer values for hours and minutes to represent the time of day in a 24-hour clock

Use time24 to identify the structure

The ADT designer has the responsibility to specify the: number of operations action of each operation

Like the architect who creates plans for a house – focus of ADT designer is on functionality and ease of use in a program

Page 12: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

The time24 ADT: OperationsThe time24 ADT: Operations

e.g., the ADT could provide the operation called duration to measure the length of time between current value and some later time in the day

The value is a time24 value that becomes the output parameter: Duration(t): Time t is an input argument. Measure the length of time from the

current time to time t, and return the result as a time24 value Precondition: Time t must not be earlier than the current time readTime(): Input from the keyboard the hours and minutes for a time24 object,

using the format hh:mm Postcondition: Set the hour and minute values of the time to hh and mm

respectively. If either of the values is out of its designated range, the operation makes the appropriate adjustment e.g. 9:75 is stored as 10:15

writeTime(): Display on the screen the current time in the form hh:mm getHour(): Return the hour value for the current time getMinute(): Return the minute value for the current time

See Exercise on page 13: “d_time24.h”

Page 13: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

The C++ ClassThe C++ Class

CLASS className Declaration class className { public: // <public member function prototypes> . . . . . . . . private: // <private data members> . . . . . . . . // <private member function prototypes> . . . . . . . . };

In object-oriented program design, building blocks of an application are values of primitive type and application

Objects consist of data and operations on the data – specific instances of class type Class specifies the structure of an object - referred to as an object type

Declaration of a class includes:Class Header consisting of reserved word class followed by the name of the class

Declaration of a class includes:Class Body – beginning with ({ ), ending with (;)

Format of Body includes:Private and Public sections

Function Prototype:Specifies the parameters in an argument list within { } and indicates the return type of the outputFunction Prototype:return Type functionName (<argument list>);

Data values in the Class

Page 14: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Classes: Private/Public SectionsClasses: Private/Public Sections

The public and private sections in a class declaration allow program statements outside the class different access to the class members.

pr i vate :

data m e m be rsm e m be r func t io ns

publ i c :

a c c e s s ib le to theim ple m e nta tion of

the m e m be r func tionsac c e s s ible by

any pro gram s tate m e ntdata m e m be rsm e m be r func t io ns

Page 15: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Classes: Public SectionClasses: Public Section

Public members of a class are the interface of the object to the program. Any statement in a program block that declares an object can access a public

member of the object

publ i c :

a c c e s s ib le to theim ple m e nta tion of

the m e m be r func tionsac c e s s ible by

any pro gram s tate m e ntdata m e m be rsm e m be r func t io ns

Page 16: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Classes: Private SectionClasses: Private Section

The private section typically contains the data values of the object and utility functions that support class implementation.

Only member functions of the class may access elements in the private section.

pr i vate :

data m e m be rsm e m be r func t io nsa c c e s s ib le to the

im ple m e nta tion ofthe m e m be r func tions

In addition, a class encapsulates information by bundling the data items and operations within an object

Page 17: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Class: ConstructorClass: Constructor

Constructor: a special member function of a class whose task it is to initialize the object

The constructor always has the name of the class

Page 18: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Application Programming Interface (API)Application Programming Interface (API)

Object-oriented programmers have developed a documentation format that collapses the key information from the ADT and the class declaration – this documentation is called application programming interface (API)

API allows other programmers to use the public interface of the class without having to view the technical details of the class declaration or implementation

The format includes: Function prototype for the constructor – provides programmer with the function

name, arguments, and return type Other public member functions of the data structure

The API includes: An action statement that describes what the operation does Preconditions that must apply before the operation can execute successfully, along

with an exception that is thrown when the condition fails Postconditions indicate any changes to the current state of the object

Page 19: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

API: ConstructorAPI: Constructor

CLASS className Constructors “<file>.h”

className(<arguments>);Initializes the attributes of the objectPostconditions: the data members of the object have initial values

Page 20: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

API: OperationsAPI: Operations

CLASS className Operations “<file>.h”

returnType functionName(argument list);Description of the action of the function and any return value

Preconditions: Necessary state of the object before executing the operation. Any exceptions that are thrown when an error is

detected.

Postconditions: State of the data items in the object after executing the operation

….

Page 21: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

StringsStrings

String: a sequence of characters that programs use to identify names, words, and sequences

C++ provides two approaches to string handling.

Older MethodOlder Method:C-style string - a character array that designates the end of the string by using the NULL character.Used by the C programming language and older C++ programs

Modern MethodModern Method: String class – defines a string as an object and provides access to individuals characters and collections of characters within the string. The standard C++ library provides the string class, which is accessed using the directive:

#include <string>

Page 22: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

Strings: Functions and OperationsStrings: Functions and Operations

The string class provides:

string handling functions that enable a programmer to search for characters within a string

extraction of consecutive sequence of characters called substrings modification of a string by adding or removing characters

Page 23: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

String: Functions and OperationsString: Functions and Operations

int find_first_of(char c, int start = 0):Look for the first occurrence of cc in the string beginning at index startstart. Return the index of the match if it occurs; otherwise return -1. By default, startstart is 0 and the function searches the entire string.

Searching for characters with a string:

This function performs simple pattern matching that looks for a single character c in the string

The index of the match is the return value. If no match occurs, the function returns -1

Page 24: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

String Functions and OperationsString Functions and Operations

int find_last_of(char c): Look for the last occurrence of cc in the string. Return the index of the match if it occurs; otherwise return -1.Since the search seeks a match in the tail of the string, no starting index is provided.

Useful in pattern matching especially in large strings See Example 1-4

Page 25: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

String Functions and OperationsString Functions and Operations

string substr(int start = 0, int count = -1):

Copy count characters from the string beginning at index startstart and return the characters as a substring. If the tail of the string has fewer than count characters or count is -1, the copy stops at end-of-string.

By default, startstart is 0 and the function copies characters from the beginning of the string. Also by default, the function copies the tail of the string.

Extraction of consecutive sequence

The function substr() extracts from the string a consecutive sequence of characters called substrings

The operation assumes a starting index and a count for the number of characters Useful in name searches on directories/ airline reservations etc See Example 1-5

Page 26: Overview of Lecture

Dr. Nazli Mollah

Data Structures: CSCI 362 - Chapter 1Data Structures: CSCI 362 - Chapter 1

lecture notes adapted fromData Structures with C++ using STL

String Functions and OperationsString Functions and Operations

int find(const string& s, int start = 0):The search takes string ss and index start and looks for a match of ss as a substring. Return the index of the match if it occurs; otherwise return -1. By default, startstart is 0 and the function searches the entire string.

The function find() locates a specific pattern in the string