0% found this document useful (0 votes)
9 views35 pages

03 Sequences

Uploaded by

lireb53846
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views35 pages

03 Sequences

Uploaded by

lireb53846
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

DATA STRUCTURES

Systems Engineering Department

Eng. Alejandro León, M.Sc.


[email protected]
ADT
Abstract Data Type
What is?
Concept

New well-specified (own) data type.

Expands the programming language.

It consists of:
• Data (representation): (protected or private)

• Functions and procedures (operations): encapsulated within the ADT,


access by interface.

3
What is?
Advantages

• Better conceptualization and modeling.

• Improves robustness.

• Improves performance (compile time optimization).

• Separate the implementation from the specification.

• Allows extensibility.

4
What is?
Examples:

• ADT Career.

• ADT Library.

• ADT Vehicle.

5
DESIGN
Concept

• Programs = Algorithms + Data. (Wirth's equation)

• ADT’s design:

• Behaviors of the object in the real world (interface/verbs).


• Minimum set of data that represents it (state/nouns).
• ADT = Interface + Status.

https://web.archive.org/web/20130207170133/http://
www.inf.ethz.ch/personal/wirth/books/AlgorithmE0/ 6
DESIGN
Template
• Name ADT Career ADT
• Minimum data set: • Minimum data set:
• Name value(variable); data type; • name; string; career identification
description. • numSt; whole; number of students
• … enrolled.

• Behavior (operations): • Behavior (operations):


• Operation name (arguments); • getName(); returns the name of the
Functional description career.
• getStNum(); returns the number of
students.

7
DESIGN
Diagram of relationship between ADT’s

Note:

It is not a class diagram.

8
IMPLEMENTATION
Alternatives

• Structure.

• Classes with access levels.

• Using libraries:
• .h → interface, method signature.
• .cpp → implementation

9
IMPLEMENTATION
Op1 : Struct

struct Carrera{
std::string nombre; struct Carrera{
unsigned long numEst; std::string nombre;
}; unsigned long numEst;

std::string ObtenerNombre(Carrera *c); std::string ObtenerNombre();


unsigned long ObtenerNumEst(Carrera *c); unsigned long ObtenerNumEst();
void FijarNombre(Carrera *c, std::string void FijarNombre(std::string nombre);
nombre); void AgregarEstud();
void AgregarEstud(Carrera *c); void EliminarEstud();
void EliminarEstud(Carrera *c); };

.cpp .cpp
10
IMPLEMENTATION
Op2 : Class (OOP)
class Carrera {
public:
Carrera();
std::string ObtenerNombre();
unsigned long ObtenerNumEst();
void FijarNombre(std::string name);
void AgregarEstud();
void EliminarEstud();
protected:
std::string nombre;
unsigned long numEst;
};

.cpp
11
ADT DESIGN AND IMPLEMENTATION
Summary:

1. Design ADT → Description + Diagram.

2. Create header (.h).

3. Implement “.h” inside files (.cxx , .cpp).

4. Write main program (main.cpp)

12
ADT DESIGN AND IMPLEMENTATION
Reminder:

The design and implementation described above will be


required in all course activities (workshops, partials, projects,
quizzes) from now on, throughout the semester.

13
SEQUENCES
SEQUENCE
Concept

Structure representing a list of


elements of the same type. 𝑆 = {𝑠𝑖 ∈ 𝑇: 1 ≤ 𝑖 ≤ 𝑛 , 𝑖 ∈ ℕ

𝑆 = {𝑠1 , 𝑠2 , 𝑠3 , … , 𝑠𝑛 }

EXAMPLE: Fibonacci Sequence.

1 1 2 3 5 8 13 ...
S0 S1 S2 S3 S4 S5 S6 ...

15
SEQUENCE
Concept

previous
first
S0 S1 S2 S3 S4 S5 S6 S7
last
next

Index
position

16
SEQUENCE
Concept

Sequece 1
S0 S1 S2 S3 S4 S5 S6 S7

Sequence 2 ≠ Sequence 1
S1 S3 S6 S0 S2 S5 S7 S4

17
IMPLEMENTATION
Static array:
Limited to predefined capacity
int miArr[8] =
{1,1,2,3,5,8,13,21};

1 1 2 3 5 8 13 21
1 1 2 3 5 8 13 21 34

string miStr = “palabras”;


int miArr2[9];
p a l a b r a s
for (int i = 0; i < 8; i++)
miArr2[i] = miArr[i];
miArr2[8] = 34;

18
IMPLEMENTATION
Dynamic Array:
Limited to the capacity with which it is
created.

int *miArr = new int[n];


for (int i = 0; i < n; i++)
*(miArr+i) = i+1; 1 2 3 4 5 6 7 8 9

n = 8 int *miArr2 = new int[9];


1 2 3 4 5 6 7 8 for (int i = 0; i < 8; i++)
*(miArr2+i) = *(miArr+i);
*(miArr2+8) = 9;
delete[] miArr;

19
SEQUENCES
Summary:

1. Necessity

2. Fully dynamic structure → runtime tuning.

3. Flexible structure → various types of data are accepted.

20
GENERIC
PROGRAMMING
Templates
GENERIC PROGRAMMING
Concept

Obstacles:
𝑎 = 𝑏 + 𝑐;

INT Data type FLOAT Data type


int b = 5; float b = 5;
int c = 6; float c = 6;
int a = b + c; float a = b + c;

int suma(int a, int b ){ float suma(float a, float b ){


return( a + b ); return( a + b );
} }

22
GENERIC PROGRAMMING
Use of templates

GENERALIZATION → ADAPTABILITY → FLEXIBILITY

template< class id >


function_declaration;

template< typename id >


function_declaration;

23
GENERIC PROGRAMMING
Template with a data type

template< class T >


T sum( T a, T b )
{ return( a + b ); }

int a = sum<int>(5, 7);


double b = sum<double>(6.4, 1.7);

24
GENERIC PROGRAMMING
Template with two data types

template< class T, class U >


T sum( T a, U b )
{ return( a + b ); }

int i, j = 25;
long l = 4567;
i = sum<int,long> (j,l);

25
GENERIC PROGRAMMING
Template for classes

template< class T > template< class T >


class vec_par { T vec_par<T>::min () {
T vals [2]; T result;
public: if (vals[0]<vals[1])
vec_par (T uno, T dos) result = vals[0];
{vals[0] = uno; else
vals[1] = dos; } result = vals[1];
T min (); return result;
}; }

26
GENERIC PROGRAMMING
Template for classes

vec_par<int> obj_i (115,36);


int res;
res = obj_i.min();

vec_par<float> obj_f (32.56,76.98);


float res2;
res2 = obj_f.min();

27
GENERIC PROGRAMMING
Exercise:

Using templates, implement a TAD Binary Operation, defined by:

1. Operand1 (integer, real or character).


2. Operand2 (integer, real or character).
3. Operation (character, one of: '+', '-', '*', '/’).

And with the operation:


• EvaluateOperation (Binary operation): returns the result of applying
the operation on the operands.

28
GENERIC PROGRAMMING
Exercise :
OpBinaria.h OpBinaria.hxx main.cpp

29
STL

Standard Template Library


STL
Concept

• Library with “many things”→ generics


• Common classes, usable with any type of data and elementary
operations.

31
STL
Components

Containers Algorithms Iterators

Predefined classes→ Basic operations They allow to iterate


Data storage over data in containers.
Espacio para
Imagen, icono o
diagrama

32
STL
Components

Espacio para
Imagen, icono o
diagrama

www.bogotobogo.com/cplusplus/stl3_iterators.php
33
STL
Homework

Review the STL and identify:

What containers does it provide?


What are the differences between them?

34
Thank you!

You might also like