0% found this document useful (0 votes)
10 views32 pages

05 Multilists

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)
10 views32 pages

05 Multilists

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/ 32

DATA STRUCTURES

Systems Engineering Department

Eng. Alejandro León, M.Sc.


[email protected]
STL
Standard Template Library
Vector, Deque, List

Fuente: https://realpython.com/python-iterators-iterables/
STL
std::vector
• Dynamic array, grows in
memory as needed.

• Random access with []


operator (overloaded).

• Direct insertion and


deletion only at the end
(tail) of the array.

3
Vector
Exercise
#include <vector>
#include <iostream>

int main() {
std::vector<int> arr;
1.for (int
Insert somei=0; i<6;
data ... i++)
arr.push_back(10-i);
for (int ind=0; ind<arr.size(); ind++)
std::cout
2. Print << arr[ind]
the inserted << screen
data on the " "; ...
std::cout << std::endl;
}

4
Vector
Exercise
#include <vector>
#include <iostream>

int main() {
std::vector<int> arr;
for (int i=0; i<6; i++)
arr.push_back(10-i);
for (int ind=0; ind<arr.size(); ind++)
std::cout << arr[ind] << " ";
std::cout << std::endl;
}

5
STL
std::deque
• Two-headed tail, dynamic
array growing at both ends.

• Random access with []


operator (overloaded).

• Direct insertion and


deletion in both head and
tail.

6
Deque
Ejercicio
#include <iostream>
#include <deque>
int main() {
std::deque<int> dcola; Espacio para
for (int i=0; i<3; i++) { Imagen, icono o
3. Insert some data at both ends ... diagrama
dcola.push_back(i);
dcola.push_front(10-i);
}
for (int ind=0; ind<dcola.size(); ind++)
4. Print the inserted
std::cout data on the<<screen
<< dcola[ind] " "; ...
std::cout << std::endl;
}
7
Deque
Exercise
#include <iostream>
#include <deque>
int main() {
std::deque<int> dcola; Espacio para
for (int i=0; i<3; i++) { Imagen, icono o
diagrama
dcola.push_back(i);
dcola.push_front(10-i);
}
for (int ind=0; ind<dcola.size(); ind++)
std::cout << dcola[ind] << " ";
std::cout << std::endl;
}
8
STL
std::list
• Double chained list,
connection to next and
previous.

• Direct access to head and


tail, other elements through
iterators.

• Direct insertion and


deletion in both head and
tail.

9
List
Exercise
#include <iostream>
#include <list>

int main() {
std::list<int> lista;
for (int i=0; i<3; i++) { Espacio para
lista.push_back(i); Imagen, icono o
5. Insert some data at both
lista.push_front(10-i); ends ... diagrama
}
std::list<int>::iterator it;
for (it=lista.begin(); it!=lista.end(); it++)
6. Print the inserted
std::cout << *it data on the screen ...
<< " ";
std::cout << std::endl;
}

10
List
Exercise
#include <iostream>
#include <list>

int main() {
std::list<int> lista;
for (int i=0; i<3; i++) { Espacio para
lista.push_back(i); Imagen, icono o
lista.push_front(10-i); diagrama
}
std::list<int>::iterator it;
for (it=lista.begin(); it!=lista.end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
}

11
ON-SCREEN OUTPUT
Vector<T> Deque<T> List<T>
vector<int> seq; deque<int> seq; list<int> seq;
vector<int>::iterator it; deque<int>::iterator it; list<int>::iterator it;
for (it=seq.begin(); for (it=seq.begin(); for (it=seq.begin();
it!=seq.end(); it++) { it!=seq.end(); it++) { it!=seq.end(); it++) {
std::cout << *it << std::cout << *it << std::cout << *it <<
std::endl; std::endl; std::endl;
} } }
Espacio para
vector<int> seq; deque<int> seq; list<int>
Imagen, icono o seq;
vector<int>::reverse_iterator deque<int>::reverse_iterator list<int>::reverse_iterator
diagrama
it; it; it;
for (it=seq.rbegin(); for (it=seq.rbegin(); for (it=seq.rbegin();
it!=seq.rend(); it++) it!=seq.rend(); it++) it!=seq.rend(); it++)
{ { {
std::cout << *it << std::cout << *it << std::cout << *it <<
std::endl; std::endl; std::endl;
} } }

12
Multilists
Multilists
Dynamic matrix representation

int **p_mat;
p_mat = new int* [size1];
for (int i=0; i<size1; i++)
*(p_mat+i) = new int [size2];

14
Multilists
Dynamic matrix representation
size1

p_mat

15
Multilists
Dynamic matrix representation
size2

*(p_mat+0)
*(p_mat+1)
size1

*(p_mat+2)
*(p_mat+3)
*(p_mat+4)

p_mat *(p_mat+5)

16
Multilists
Matrix representation as a set of lists

17
Multilists
Matrix representation as a set of lists

head

18
Multilists
Matrix representation as a set of lists

head

19
Multilists
Matrix representation as a set of lists
Multilist: sequence of sequences
Advantage: They do not all have to be the same size.

head

20
Multilists
Implementation
List of integers:
std::list<int> integers;
head
4 8 13

List of pointers to real numbers:


std::list<float*> reals;
head

1,5 0,5 9,6


21
Multilists
Implementation → Vector of lists.

std::vector< std::list<int> > arr;


arr[0].push_back(4);
arr[2].push_back(13);
arr[0].push_back(8); 4 13

22
Multilists
Implementación → List of lists.

std::list< std::list<int> > multilist;


std::list< std::list<int> >::iterator it;
it = multilist.begin();
it->push_back(4);
it->push_back(8);
head
it++; 4 13
it++;
it->push_back(13);
8

23
Multilists
Exercise
• Consider the implemented TAD Vehicle
class Vehicle {
protected:
unsigned short model;
string regPlate;
unsigned int dailyRate;
public:
unsigned short getModel();
string getRegPlate();
unsigned int getDailyRate();
...
};
24
Multilists
Exercise

• Also consider an entity that manages different parking lots,


each viewed as a list(sequence) of vehicles.

• 1) How would the entity be represented?

25
Multilists
Exercise

• Also consider an entity that manages different parking lots,


each viewed as a list(sequence) of vehicles.

• 1) How would the entity be represented?


std::list< std::list<Vehicle> > entity;

26
Multilists
Ejercicio
• Assuming that the parking lots are identified by a
consecutive integer, how would a vehicle be inserted into a
given parking lot?

27
Multilists
Ejercicio
• Assuming that the parking lots are identified by a
consecutive integer, how would a vehicle be inserted into a
given parking lot?
void InsertVeh( Vehicle nVeh, int idParq )
{
std::list< std::list<Vehicle> >::iterator itP;
itP = entity.begin();
for (int i = 1; i <= idParq; i++)
itP++;
itP->push_back(nVeh);
}

28
Multilists
Exercise
• How would you calculate the total number of vehicles in the
entity?

29
Multilists
Exercise
• How would you calculate the total number of vehicles in the
entity?
unsigned int totalVehs( )
{
unsigned int total = 0;
std::list< std::list<Vehicle> >::iterator itP;
itP = entity.begin();
for ( ; itP != entity.end(); itP++)
total += itP->size();
return total;
}

30
Multilists
Exercise
• How would the total daily value received by the entity for parked
vehicles be calculated?
unsigned int totalDaily( )
{
unsigned int total = 0;
std::list< std::list<Vehicle> >::iterator itP;
std::list<Vehicle>::iterator itV;
itP = entity.begin();
for ( ; itP != entity.end(); itP++) {
itV = itP->begin();
for ( ; itV != itP->end(); itV++)
total += itV-> getDailyRate();
}
return total;
} 31
Thank you

You might also like