8/13/2019 CSC335-Chapter2
1/23
1
(Chapter 2-Introduction to Abstract Data Types)
CSC-335
Data Structures and Algorithms
Instructor:Ahmad Reza Hadaegh
The content of this power point lecture has been originally
created by Christos Kolonis and modified byDr. Ahmad R Hadaegh
8/13/2019 CSC335-Chapter2
2/23
2
Chapter Contents
2.1 A first look at ADTs and Implementations2.2 C++'s Simple Data Types
2.3 Programmer-Defined Data Types
2.4 Pointers
8/13/2019 CSC335-Chapter2
3/23
3
Chapter Objectives
Distinguish between ADTs and implementations of ADTs Review C++'s simple data types & ADTs they model
See examples of how implementations of ADTs aresometimes insufficient
Look at simple mechanisms to define new data types Take a first look at pointers and pointer operations
8/13/2019 CSC335-Chapter2
4/23
4
2.1 First Look at ADTs & Implementations
For a programming task we must identify The collection of data items
Basic operations to be performed on them
EX: the car example, its properties and its functions
Taken together (data items & operations)
are called an Abstract Data Type (ADT)
Implementation
Storage structures for the data items
Algorithms for the operations (functions)
8/13/2019 CSC335-Chapter2
5/23
8/13/2019 CSC335-Chapter2
6/23
6
Two's Complement Representation
For nonnegative n:
Use ordinary base-two representation with leading(sign) bit 0
For n < 0
1) Find w-bit base-2 representation of |n|2) Complement each bit.
3) Add 1
----------------------------------------------------------------------------
Ex: 00001010 in 8-bits system is equal to 10 indecimal. Now to get -10, we flip every bit to get11110101 and add 1 to get 11110110 which is -10 inbinary
8/13/2019 CSC335-Chapter2
7/23
7
Two's Complement Representation
Example:
881. 88 as a 16-bit base-two number
0000000001011000
2. Complement this bit string
11111111101001113. Add 1
1111111110101000WHY?
8/13/2019 CSC335-Chapter2
8/23
8
Two's Complement Representation
Works well for arithmetic computations
5 +6:
0000000000000101
+11111111111110101111111111111111
What gets done to
the bits to give this
answer?
8/13/2019 CSC335-Chapter2
9/23
9
Problems with Integer Representation
Limited Capacity
a finite number of bits An operation can produce a value that requires more bits
than maximum number allowed.
This is called overflow .
None of these is aperfectrepresentation of(mathematical) integers
Can only store a finite (sub)range of them.
See Demonstrations Figure 2.1 and Figure 2.2
8/13/2019 CSC335-Chapter2
10/23
10
2.2 C++ Simple Data TypesCharacter Data
1 byte for ASCII, EBCDIC 2 bytes for Unicode (java)
or C++ wide character type
Operations ==, , etc. Using numeric code
8/13/2019 CSC335-Chapter2
11/23
11
2.2 C++ Simple Data TypesBoolean Data
Values {false, true } Could be stored in bits, usually use a byte
Operations &&, ||, !
In C++ bool type
int (boolVal) evaluates to
0 if false
1 if true
8/13/2019 CSC335-Chapter2
12/23
12
2.3 Programmer-Defined Data Types
Typedefs Mechanism usable to create a new type
Give new name to existing type
Example:
typedef double real;
Now eitherdoubleorrealcan be used.
8/13/2019 CSC335-Chapter2
13/23
13
2.3 Programmer-Defined Data Types
Enumerations Mechanism for creating types whose literals are
identifiers
Each identifier associated with unique integer
8/13/2019 CSC335-Chapter2
14/23
14
2.3 Programmer-Defined Data Types
Also possible to specify explicit values to give theenumeratorsenum NumberBase { BINARY = 2,
OCTAL = 8,
DECIMAL = 10,
HEXADECIMAL = 16};
8/13/2019 CSC335-Chapter2
15/23
15
2.4 Pointers
When regular variables aredeclared
Memory allocated for valueof specified type
Variable name associatedwith that memory location
Memory initialized withvalues provided (if any)
Fig. 2.3
27
8/13/2019 CSC335-Chapter2
16/23
16
2.4 Pointers
Pointer Variables value stored is a memory address
Note sample program, Figure 2.4
Declares variables that can store intaddressesand doublevariables
Displays the addresses
8/13/2019 CSC335-Chapter2
17/23
17
Basic Pointer Operations
Dereferencing and indirection
Pointer variable stores address of a location
Accessing contents of that location requiresdereferencing operator *
Note program exampleFigure 2.5
8/13/2019 CSC335-Chapter2
18/23
18
Basic Pointer Operations Assignment
Pointer variables can be assigned the values of otherpointer variables bound to same type
8/13/2019 CSC335-Chapter2
19/23
19
Basic Pointer Operations
Consider *jPtr = 44;
Changes value that both pointers reference
Not good programming practice, hard to debug
Known as aliasing problem
*iPtriPtr = &j
8/13/2019 CSC335-Chapter2
20/23
20
Basic Pointer Operations
Comparison Relational operators used to compare two pointers
Must be bound to same type
Most common == and !=
The null address may be compared with any pointervariable
8/13/2019 CSC335-Chapter2
21/23
21
Dynamic Memory Allocation
The newoperation Example
typede int* intPtr;intPtr p = new int;
X
X
X
intPtr p = new int;
8/13/2019 CSC335-Chapter2
22/23
22
Pointer Arguments
Pointers can be passed as arguments tofunctions
This is logically equivalent to reference parameters
In fact, this is how early C++ compilers accomplishedreference parameters
8/13/2019 CSC335-Chapter2
23/23
23
Pointer Examples
int i1; int i2;
int* iPtr1;
Int* iPtr2; double d1;
double d2;
double* dPtr1;
double* dPtr2;
dPtr1 = &d1 dPtr1 = d1;
iPtr1 = & d1;
iPtr2 = &i2;
iPtr1 = iPtr2;
dPtr1 = iPtr1;
*iPtr1 = 19
*dPtr1 = *iPtr1;
*dPtr2 = 20.67;
*iPtr2 = *dPtr2;