Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
algorithmique
cours
Commits
30f00d78
Verified
Commit
30f00d78
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added insert for sorted list
parent
5420735e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/cours_11.md
+120
-5
120 additions, 5 deletions
slides/cours_11.md
with
120 additions
and
5 deletions
slides/cours_11.md
+
120
−
5
View file @
30f00d78
...
@@ -47,8 +47,7 @@ patat:
...
@@ -47,8 +47,7 @@ patat:
*
Entre les deux, les éléments sont stockés dans une liste chaînée (comme une
*
Entre les deux, les éléments sont stockés dans une liste chaînée (comme une
pile).
pile).

d'attente.
](
figs/fig_queue_representation.png
)
## Structure de données en C?
## Structure de données en C?
...
@@ -269,7 +268,7 @@ typedef struct _queue {
...
@@ -269,7 +268,7 @@ typedef struct _queue {
# Complexité
# Complexité
## Quelle sont les complexité de:
## Quelle sont les complexité
s
de:
*
Initialisation?
*
Initialisation?
...
@@ -336,6 +335,52 @@ typedef struct _queue {
...
@@ -336,6 +335,52 @@ typedef struct _queue {
} queue;
} queue;
```
```
# Une file plus efficace (implémentation)
## Enfilage
```
C
void queue_enqueue(queue *fa, int val) {
if ((fa->head == 0 && fa->tail == fa->capacity-1) ||
(fa->tail == (fa->head-1) % (fa->capacity-1))) {
return; // queue is full
}
if (fa->head == -1) { // queue was empty
fa->head = fa->tail = 0;
fa->data[fa->tail] = val;
} else if (fa->tail == fa->capacity-1 && fa->head != 0) {
// the tail reached the end of the array
fa->tail = 0;
fa->data[fa->tail] = val;
} else {
// nothing particular
fa->tail += 1;
fa->data[fa->tail] = val;
}
}
```
# Une file plus efficace (implémentation)
## Défilage
```
C
// Function to delete element from Circular Queue
void queue_dequeue(queue *fa, int *val) {
if (queue_is_empty(*fa)) {
return; // queue is empty
}
*val = fa->data[fa->head];
if (fa->head == fa->tail) { // that was the last element
fa->head = fa->tail = -1;
} else if (fa->head == fa->capacity-1) {
fa->head = 0;
} else {
fa->head += 1;
}
}
```
# File basée sur un tableau (git)
# File basée sur un tableau (git)
...
@@ -351,8 +396,7 @@ typedef struct _queue {
...
@@ -351,8 +396,7 @@ typedef struct _queue {
Une liste chaînée triée est:
Une liste chaînée triée est:
*
une liste chaînée
*
une liste chaînée
*
dont les éléments sont insérés dans
*
dont les éléments sont insérés dans l'ordre.
l'ordre.


...
@@ -400,6 +444,77 @@ element* sorted_list_search(sorted_list list, int val);
...
@@ -400,6 +444,77 @@ element* sorted_list_search(sorted_list list, int val);
# L'insertion
# L'insertion
## Trois cas
1.
La liste est vide.
. . .

{width=30%}
. . .
```
C
sorted_list sorted_list_push(sorted_list list, int val) {
if (sorted_list_is_empty(list)) {
list = malloc(sizeof(*list));
list->data = val;
list->next = NULL;
return list;
}
}
```
# L'insertion
2.
L'insertion se fait en première position.
. . .

{width=80%}
. . .
```
C
sorted_list sorted_list_push(sorted_list list, int val) {
if (list->data >= val) {
element *tmp = malloc(sizeof(*tmp));
tmp->data = val;
tmp->next = list;
list = tmp;
return list;
}
}
```
# L'insertion
3.
L'insertion se fait sur une autre position que la première.
. . .

{width=70%}
. . .
\f
ootnotesize
```
C
sorted_list sorted_list_push(sorted_list list, int val) {
element *tmp = malloc(sizeof(*tmp));
tmp->data = val;
element *crt = list;
while (NULL != crt->next && val > crt->next->data) {
crt = crt->next;
}
tmp->next = crt->next;
crt->next = tmp;
return list;
}
```
# L'extraction
# L'extraction
# La recherche
# La recherche
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment