+ All Categories
Home > Documents > Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central...

Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central...

Date post: 23-Dec-2015
Category:
Upload: kathlyn-george
View: 224 times
Download: 5 times
Share this document with a friend
25
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Transcript
Page 1: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Chapter 3 Data Structures and Abstract Data Type

Dr. Bernard Chen Ph.D.University of Central Arkansas

Fall 2008

Page 2: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Outline

Introduction to Abstract Data Types (ADTs)

Arrays

Page 3: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

A first look an ADTs Solving a problem involves processing data, and

an important part of the solution is the careful organization of the data

In order to do that, we need to identify:1. The collection of data items 2. Basic operation that must be performed on them

Abstract Data Type (ADT): a collection of data items together with the operations on the data

Page 4: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Abstract Data Type (ADT)

The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented

We think about what can be done with the data, not how it is done

Page 5: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

ADT example

Page 6: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Implementation of ADT

An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation

Page 7: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Data Structures, Abstract Data Types, and Implementations

Consider example of an airplane flight with 10 seats to be assigned

Tasks List available seats Reserve a seat

How to store, access data?

Page 8: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Data Structures, Abstract Data Types, and Implementations

Consider example of an airplane flight with 10 seats to be assigned

Tasks List available seats Reserve a seat

How to store, access data? 10 individual variables

Page 9: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Use 10 individual variables Algorithm to List

available seats

1. If seat1 == ‘ ’:display 1

2. If seat2 == ‘ ’: display 2

.

.

.10. If seat10 == ‘ ’:

display 10

Algorithm to Reserve a seat

1. Set DONE to false2. If seat1 ==‘ ’:

print “do you want seat #1??”Get answerif answer==‘Y’:

set seat1 to ‘X’set Done to True

3. If seat2 ==‘ ’:print “do you want seat #2??”Get answerif answer==‘Y’:

set seat2 to ‘X’set Done to True

.

.

.

Page 10: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Data Structures, Abstract Data Types, and Implementations

Consider example of an airplane flight with 10 seats to be assigned

Tasks List available seats Reserve a seat

How to store, access data? 10 individual variables An array of variables

Page 11: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Use Array Algorithm to List available seats

For number ranging from 0 to max_seats-1, do:If seat[number] == ‘ ’:

Display number

Algorithm to Reserve a seat

Readin number of seat to be reservedIf seat[number] is equal to ‘ ’:

set seat[number] to ‘X’Else

Display a message that the seat having this number is occupied

Page 12: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

ADTs In this simple example, it does illustrate the

concept of an Abstract Data Type

ADT consists of1. The collection of data items 2. Basic operation that must be performed on

them

In the example, a collection of data is a list of seats

The basic operations are (1) Scan the list to determine which seats are occupied (2) change seat’s status

Page 13: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Data Structure and Abstract Data Type The term of Data Structure and Abstract

Data Type are often used interchangeably

However, we use ADT when data is studied at a logical level

The term data structure refers to a construct in programming language that can be used to store data

Page 14: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Outline

Introduction to Abstract Data Types (ADTs)

Arrays

Page 15: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Array ADT

Collection of data elements A fixed-size sequence of elements, all

of the same type

Basic Operations Direct access to each element in the

array by specifying its position so that values can be retrieved from or stored in that position

Page 16: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Different types of Array One-dimensional array: only one

index is used Multi-dimensional array: array

involving more than one index

Static array: the compiler determines how memory will be allocated for the array

Dynamic array: memory allocation takes place during execution

Page 17: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

One-Dimensional Static Array

Syntax:ElementType arrayName [CAPACITY];ElementType arrayName [CAPACITY] =

{ initializer_list };

Example:int b [10];

int b [10]={1,2,3,4,5,6,7,8,9,10};

Page 18: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Array and ADT Collection of data elements

A fixed-size sequence of elements, all of the same type

Basic Operations Direct access to each element in the array by specifying its

position so that values can be retrieved from or stored in that position

An Array as an ADT C++ Array

Fixed size ----------------------specify the capacity of the arrayOrdered-------------------------indices are numbered 0,1,2,…,capacity-1 Same type of elements-------specify the element type Direct access-------------------subscript operator[]

Page 19: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Example of array Consider array a,b,c,d to store collection of 10

integers declared by:

int capacity=10int a[capacity],

b[capacity]={1,2,3,4,5,6,7,8,9,10}, c[capacity]={1,2,3}, d[capacity]={0};

char name[capacity]=“John Doe”;

Page 20: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Array Output Function

Void display(int array[], int num_values)

{for (int I = 0; i<num_values; i++)

cout<< array[i] << “ ”;}

Page 21: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Multidimensional Arrays

Consider a table of test scores for several different students

Page 22: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Array of Array Declarations

An array of arrays An array whose elements are other

arrays

Page 23: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Array of Array Declarations

Each of the rows is itself a one dimensional array of values

scoresTable[2]

is the whole row numbered 2

scoresTable[2]

is the whole row numbered 2

scoresTable [1][3]scoresTable [1][3]

Page 24: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Multidimensional Arrays

Syntax: ElementType arrayName [num_rows][num_columns];

int scoretable [num_students][num_tests];

int scoretable[2][5]={{80,80,80,80,80},{60,60,60,60,60}};

If you want to change the score of first student’s 3rd test score to 100, you just need to do:

Scoretable[0][2]=100

Page 25: Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Multidimensional Arrays

Consider multiple pages of the student grade booktypedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS];

. . .


Recommended