Skip to content
Snippets Groups Projects
Verified Commit 9c546984 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added cours 10

parent 3727c589
No related branches found
No related tags found
No related merge requests found
---
title: "Piles et files d'attente"
date: "2021-12-08"
patat:
eval:
tai:
command: fish
fragment: false
replace: true
ccc:
command: fish
fragment: false
replace: true
images:
backend: auto
---
# Rappel
## Qu'est-ce qu'une pile?
. . .
* Structure de données LIFO.
## Quelles fonctionnalités?
. . .
1. Empiler (push): ajouter un élément sur la pile.
2. Dépiler (pop): retirer l'élément du sommet de la pile et le retrouner.
3. Liste vide? (is_empty?).
4. Jeter un oeil (peek): retourner l'élément du sommet de la pile (sans le dépiler).
5. Nombre d'éléments (length).
# Le tri à deux piles (3/3)
## Exercice: trier le tableau `[2, 10, 5, 20, 15]`
```C
```
# La calculatrice (1/8)
## Vocabulaire
```C
2 + 3 = 2 3 +,
```
`2` et `3` sont les *opérandes*, `+` l'*opérateur*.
. . .
## La notation infixe
```C
2 * (3 + 2) - 4 = 6.
```
## La notation postfixe
```C
2 3 2 + * 4 - = 6.
```
## Exercice: écrire `2 * 3 * 4 + 2` en notation `postfixe`
. . .
```C
2 3 4 * * 2 + = (2 * (3 * 4)) + 2.
```
# La calculatrice (2/8)
## De infixe à post-fixe
* Une *pile* est utilisée pour stocker *opérateurs* et *parenthèses*.
* Les opérateurs on des *priorités* différentes.
```C
^ : priorité 3
* / : priorité 2
+ - : priorité 1
( ) : priorité 0 // pas un opérateur mais bon
```
# La calculatrice (3/8)
## De infixe à post-fixe: algorithme
* On lit l'expression infixe de gauche à droite.
* On examine le prochain caractère de l'expression infixe.
* Si opérande, le placer dans l'expression du résultat.
* Si parenthèse le mettre dans la pile (priorité 0).
* Si opérateur, comparer sa priorité avec celui du sommet de la pile:
* Si sa priorité est plus élevée, empiler.
* Sinon dépiler l'opérateur de la pile dans l'expression du résultat et
recommencer jusqu'à apparition d'un opérateur de priorité plus faible
au sommet de la pile (ou pile vide).
* Si parenthèse fermée, dépiler les opérateurs du sommet de la pile et les
placer dans l'expression du résultat, jusqu'à ce qu'une parenthèse
ouverte apparaisse au sommet, dépiler également la parenthèse.
* Si il n'y a pas de caractère dans l'expression dépiler tous les
opérateurs dans le résultat.
# La calculatrice (4/8)
## De infixe à post-fixe: exemple
```C
Infixe Postfixe Pile Priorité
((A*B)/D-F)/(G+H) Vide Vide Néant
(A*B)/D-F)/(G+H) Vide ( 0
A*B)/D-F)/(G+H) Vide (( 0
*B)/D-F)/(G+H) A (( 0
B)/D-F)/(G+H) A ((* 2
)/D-F)/(G+H) AB ((* 2
/D-F)/(G+H) AB* ( 0
D-F)/(G+H) AB* (/ 2
-F)/(G+H) AB*D (/ 2
F)/(G+H) AB*D/ (- 1
)/(G+H) AB*D/F (- 1
/(G+H) AB*D/F- Vide Néant
```
# La calculatrice (5/8)
## De infixe à post-fixe: exemple
```C
Infixe Postfixe Pile Priorité
((A*B)/D-F)/(G+H) Vide Vide Néant
--------------------------------------------------------
/(G+H) AB*D/F- Vide Néant
(G+H) AB*D/F- / 2
G+H) AB*D/F- /( 0
+H) AB*D/F-G /( 0
H) AB*D/F-G /(+ 1
) AB*D/F-GH /(+ 1
Vide AB*D/F-GH+ / 2
Vide AB*D/F-GH+/ Vide Néant
```
# La calculatrice (6/8)
\footnotesize
## Exercice: écrire le code et le poster sur matrix
* Quelle est la signature de la fonction?
. . .
```C
char *infix_to_postfix(char* infix) { // init and alloc stack and postfix
for (size_t i = 0; i < strlen(infix); ++i) {
if (is_operand(infix[i])) {
// we just add operands in the new postfix string
} else if (infix[i] == '(') {
// we push opening parenthesis into the stack
stack_push(&s, infix[i]);
} else if (infix[i] == ')') {
// we pop everything into the postfix
} else if (is_operator(infix[i])) {
// this is an operator. We add it to the postfix based
// on the priority of what is already in the stack and push it
}
}
// pop all the operators from the s at the end of postfix
// and end the postfix with `\0`
return postfix;
}
```
# La calculatrice (7/8)
## Évaluation d'expression postfixe: algorithme
* Chaque *opérateur* porte sur les deux opérandes qui le précèdent.
* Le *résultat d'une opération* est un nouvel *opérande* qui est remis au
sommet de la pile.
## Exemple
```C
2 3 4 + * 5 - = ?
```
* On parcours de gauche à droite:
```C
Caractère lu Pile opérandes
2 2
3 2, 3
4 2, 3, 4
+ 2, (3 + 4)
* 2 * 7
5 14, 5
- 14 - 5 = 9
```
# La calculatrice (8/8)
## Évaluation d'expression postfixe: algorithme
1. La valeur d'un opérande est *toujours* empilée.
2. L'opérateur s'applique *toujours* au 2 opérandes au sommet.
3. Le résultat est remis au sommet.
## Exercice: écrire l'algorithme en C (et poster sur matrix)
. . .
```C
bool evaluate(char *postfix, double *val) { // init stack
for (size_t i = 0; i < strlen(postfix); ++i) {
if (is_operand(postfix[i])) {
stack_push(&s, postfix[i]);
} else if (is_operator(postfix[i])) {
double rhs = stack_pop(&s);
double lhs = stack_pop(&s);
stack_push(&s, op(postfix[i], lhs, rhs));
}
}
return stack_pop(&s);
}
```
# La liste chaînée et pile (1/6)
## Structure de données
* Chaque élément de la liste contient:
1. une valeur,
2. un pointeur vers le prochain élément.
* La pile est un pointeur vers le premier élément.
![Un exemple de liste chaînée.](figs/Singly-linked-list.svg){width=80%}
# La liste chaînée et pile (2/6)
## Une pile-liste-chaînée
```C
typedef struct _element {
int data;
struct _element *next;
} element;
typedef element* stack;
```
## Fonctionnalités?
. . .
```C
void stack_create(stack *s); // *s = NULL;
void stack_destroy(stack *s);
void stack_push(stack *s, int val);
void stack_pop(stack *s, int *val);
void stack_peek(stack s, int *val);
bool stack_is_empty(stack s); // reutrn NULL == stack;
```
# La liste chaînée et pile (3/6)
## Empiler? (faire un dessin)
. . .
```C
```
## Empiler? (le code ensemble)
. . .
```C
void stack_push(stack *s, int val) {
element *elem = malloc(sizeof(*elem));
elem->data = val;
elem->next = *s;
s = elem;
}
```
# La liste chaînée et pile (4/6)
## Jeter un oeil? (faire un dessin)
. . .
```C
```
## Jeter un oeil? (le code ensemble)
. . .
```C
void stack_peek(stack s, int *val) {
*val = s->data;
}
```
# La liste chaînée et pile (5/6)
## Dépiler? (faire un dessin)
. . .
```C
```
## Dépiler? (le code ensemble)
. . .
```C
void stack_pop(stack *s, int *val) {
stack_peek(*s, val);
element *tmp = *s;
*s = (*s)->next;
free(tmp);
return val;
}
```
# La liste chaînée et pile (6/6)
## Détruire? (faire un dessin)
. . .
```C
```
## Détruire? (le code ensemble)
. . .
```C
void stack_destroy(stack *s) {
while (!stack_is_empty(*s)) {
int val = stack_pop(s);
}
}
```
# La file d'attente (1/2)
* Structure de données abstraite permettant le stockage d'éléments.
* *FIFO*: First In First Out, ou première entrée première sortie.
* Analogue de la vie "réelle"":
* File à un guichet,
* Serveur d'impressions,
* Mémoire tampon, ...
## Fonctionnalités
. . .
* Enfiler, ajouter un élément à la fin de la file.
* Défiler, extraire un élément au devant de la file.
* Tester si la file est vide.
. . .
* Lire l'élément de la fin de la file.
* Lire l'élément du devant de la file.
* Créer une liste vide.
* Détruire une liste vide.
# La file d'attente (2/2)
## Implémentation possible
* La structure file, contient un pointeur vers la tête et un vers la queue.
* Entre les deux, les éléments sont stockés dans une liste chaînée (comme une
pile).
![Illustration d'une file
d'attente.](figs/fig_queue_representation.png)
## Structure de données en C?
. . .
```C
txpedef struct _element { // Elément de liste
int data;
struct _element* next;
} element;
typedef struct _queue { // File d'attente:
element* head; // tête de file d'attente
element* tail; // queue de file d'attente
} queue;
```
# Fonctionnalités d'une file d'attente
## Creation et consultations
. . .
```C
void queue_init(queue *fa); // head = tail = NULL
bool queue_is_empty(queue fa); // fa.head == fa.tail == NULL
int queue_tail(queue fa); // return fa.head->data
int queue_head(queue fa); // return fa.tail->data
```
## Manipulations et destruction
. . .
```C
void queue_enqueue(queue *fa, int val); // adds an element before the tail
int queue_dequeue(queue *fa); // removes the head and returns stored value
void queue_destroy(queue *fa); // dequeues everything into oblivion
```
# Enfilage
## Deux cas différents:
1. La file est vide (faire un dessin):
. . .
![Insertion dans une file d'attente vide.](./figs/fig_empty_queue_insert.png){width=40%}
2. La file n'est pas vide (faire un dessin):
. . .
![Insertion dans une file d'attente non-vide.](./figs/fig_non_empty_queue_insert.png){width=70%}
# Enfilage
## Live (implémentation)
. . .
```C
void queue_enqueue(queue *fa, int val) {
element elmt = malloc(sizeof(*elmt));
elmt->data = val;
elmt->next = NULL;
if (queue_is_empty(*fa)) {
fa->head = elmt;
fa->tail = elmt;
} else {
fa->tail->next = elmt;
fa->tail = elmt;
}
}
```
# Défilage
## Trois cas différents
1. La file a plus d'un élément (faire un dessin):
. . .
![Extraction d'une file d'attente](./figs/fig_queue_extract.png){width=80%}
2. La file un seul élément (faire un dessin):
. . .
![Extraction d'une file d'attente de longueur 1.](./figs/fig_queue_extract_one.svg){width=25%}
3. La file est vide (problème)
# Défilage
## Live (implémentation)
. . .
```C
int queue_dequeue(queue *fa) {
elmt = fa->head;
int val = elmt->data;
fa->head = fa->head->next;
free(elmt);
if (NULL == fa->head) {
fa->tail = NULL;
}
return val;
}
```
. . .
## Problème avec cette implémentation?
This diff is collapsed.
slides/figs/fig_empty_queue_insert.png

13.3 KiB

slides/figs/fig_non_empty_queue_insert.png

20.1 KiB

slides/figs/fig_queue_extract.png

19.5 KiB

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="71.796646mm"
height="55.054691mm"
viewBox="0 0 71.796646 55.054692"
version="1.1"
id="svg26163"
inkscape:version="1.1.1 (3bf5ae0d25, 2021-09-20, custom)"
sodipodi:docname="fig_queue_extract_one.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview26165"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:document-units="mm"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:zoom="1.2953002"
inkscape:cx="183.35518"
inkscape:cy="147.84217"
inkscape:window-width="1920"
inkscape:window-height="1080"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs26160">
<marker
style="overflow:visible"
id="Arrow2Lend"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.625;stroke-linejoin:round"
id="path8989" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Lend-7"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.625;stroke-linejoin:round"
id="path8989-5" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Lend-7-3"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.625;stroke-linejoin:round"
id="path8989-5-6" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Lend-7-2"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.625;stroke-linejoin:round"
id="path8989-5-9" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Lend-7-27"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.625;stroke-linejoin:round"
id="path8989-5-0" />
</marker>
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-38.04528,-128.39909)">
<g
id="g27567">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-opacity:1"
id="rect849"
width="32.000549"
height="13.894769"
x="61.211033"
y="163.60229" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 77.211308,163.81338 v 13.47261"
id="path986" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.52777px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264583"
x="63.648254"
y="167.44313"
id="text3270"><tspan
sodipodi:role="line"
id="tspan3268"
x="63.648254"
y="167.44313"
style="stroke-width:0.264583">data</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:3.52777px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264583"
x="80.402161"
y="167.34151"
id="text3270-3"><tspan
sodipodi:role="line"
id="tspan3268-6"
x="80.402161"
y="167.34151"
style="stroke-width:0.264583">next</tspan></text>
<path
style="fill:none;stroke:#000000;stroke-width:0.264583px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-end:url(#Arrow2Lend)"
d="M 90.506705,170.55434 H 101.39839"
id="path8966" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-end:url(#Arrow2Lend-7)"
d="m 57.156648,151.19704 9.957227,11.4829"
id="path8966-3"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;marker-end:url(#Arrow2Lend-7-3)"
d="m 57.070207,134.41304 13.529066,8.95621"
id="path8966-3-0"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:0.529167, 0.264583;stroke-dashoffset:0;stroke-opacity:1;marker-end:url(#Arrow2Lend-7-2)"
d="m 79.884799,151.13428 -8.461285,11.75329"
id="path8966-3-1"
sodipodi:nodetypes="cc" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:0.529167, 0.264583;stroke-dashoffset:0;stroke-opacity:1;marker-end:url(#Arrow2Lend-7-27)"
d="m 99.450797,151.13241 -25.53153,11.89643"
id="path8966-3-9"
sodipodi:nodetypes="cc" />
<g
id="g19539"
transform="matrix(0.26458333,0,0,0.26458333,50.232802,175.83868)">
<g
id="g14281"
transform="translate(-30.526542,-27.424011)">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1.5;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect9442"
width="71.481941"
height="28.485205"
x="-14.786533"
y="-94.194954" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:13.3333px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none"
x="5.6842546"
y="-74.981659"
id="text11594"><tspan
sodipodi:role="line"
id="tspan11592"
x="5.6842546"
y="-74.981659">elmt</tspan></text>
</g>
</g>
<g
id="g19533"
transform="matrix(0.26458333,0,0,0.26458333,50.232802,175.83868)">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1.5;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect14815"
width="139.78581"
height="29.670931"
x="76.975639"
y="-122.71912" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:13.3333px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none"
x="90.747475"
y="-103.29707"
id="text16175"><tspan
sodipodi:role="line"
id="tspan16173"
x="90.747475"
y="-103.29707">tete</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:13.3333px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none"
x="167.53326"
y="-102.91296"
id="text16175-5"><tspan
sodipodi:role="line"
id="tspan16173-6"
x="167.53326"
y="-102.91296">debut</tspan></text>
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 146.86855,-122.04171 v 28.316121"
id="path17624" />
</g>
<g
id="g24610"
transform="matrix(0.26458333,0,0,0.26458333,50.232802,175.83868)">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect20383"
width="33.501225"
height="22.229925"
x="-7.6590633"
y="-178.79926" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:13.3333px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none"
x="3.1117313"
y="-162.71361"
id="text21875"><tspan
sodipodi:role="line"
id="tspan21873"
x="3.1117313"
y="-162.71361">fa</tspan></text>
</g>
<g
id="g25290"
transform="matrix(0.26458333,0,0,0.26458333,50.232802,175.83868)">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:3.77953;stroke-opacity:1"
id="rect9338"
width="25.912838"
height="48.277096"
x="197.49173"
y="-44.128456" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 198.44004,-28.851968 13.81444,-13.814444"
id="path25192" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 198.16174,-17.266685 24.00548,-24.005477"
id="path25194" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 198.02184,-6.705438 25.33761,-25.337608"
id="path25198" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 197.49173,4.1486397 223.4456,-21.805234"
id="path25200" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 208.19521,4.2844338 223.75226,-11.272625"
id="path25202" />
</g>
</g>
<path
style="fill:none;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 57.951433,161.18156 40.25922,21.83269"
id="path27602" />
<path
style="fill:none;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 98.210653,161.18156 -40.25922,21.83269"
id="path27602-6" />
</g>
</svg>
slides/figs/fig_queue_representation.png

7.08 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment