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
8e944960
Verified
Commit
8e944960
authored
4 months ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added stack live code
parent
ae282f32
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#36435
passed
4 months ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
slides/exemples/dyn_stack.c
+139
-0
139 additions, 0 deletions
slides/exemples/dyn_stack.c
slides/exemples/stack.c
+4
-0
4 additions, 0 deletions
slides/exemples/stack.c
with
143 additions
and
0 deletions
slides/exemples/dyn_stack.c
0 → 100644
+
139
−
0
View file @
8e944960
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
#include
<assert.h>
#define MIN_CAPACITY 5
struct
stack
{
int
top
;
int
capacity
;
int
*
data
;
};
void
stack_init
(
struct
stack
*
s
,
int
capacity
)
{
s
->
top
=
-
1
;
s
->
data
=
malloc
(
capacity
*
sizeof
(
int
));
s
->
capacity
=
capacity
;
}
bool
stack_is_empty
(
struct
stack
s
)
{
return
s
.
top
==
-
1
;
}
void
stack_push
(
struct
stack
*
s
,
int
val
)
{
if
(
s
->
top
!=
s
->
capacity
-
1
)
{
s
->
top
+=
1
;
s
->
data
[
s
->
top
]
=
val
;
}
else
{
s
->
capacity
*=
2
;
s
->
data
=
realloc
(
s
->
data
,
s
->
capacity
*
sizeof
(
int
));
s
->
top
+=
1
;
s
->
data
[
s
->
top
]
=
val
;
}
}
/*int stack_pop(struct stack *s) {*/
/* if (!stack_is_empty(*s)) {*/
/* int tmp = s->data[s->top];*/
/* s->top -= 1;*/
/* return tmp;*/
/* }*/
/*}*/
/*void stack_pop(struct stack *s, int *val) {*/
/* if (!stack_is_empty(*s)) {*/
/* *val = s->data[s->top];*/
/* s->top -= 1;*/
/* }*/
/*}*/
int
*
stack_pop
(
struct
stack
*
s
)
{
if
(
!
stack_is_empty
(
*
s
))
{
int
*
val
=
&
(
s
->
data
[
s
->
top
]);
s
->
top
-=
1
;
if
(
s
->
top
<
s
->
capacity
/
4
&&
(
s
->
capacity
/
2
)
>
MIN_CAPACITY
)
{
s
->
capacity
/=
2
;
s
->
data
=
realloc
(
s
->
data
,
s
->
capacity
*
sizeof
(
int
));
}
return
val
;
}
else
{
return
NULL
;
}
}
int
*
stack_peek
(
struct
stack
s
)
{
if
(
!
stack_is_empty
(
s
))
{
int
*
val
=
&
(
s
.
data
[
s
.
top
]);
return
val
;
}
else
{
return
NULL
;
}
}
void
stack_print
(
struct
stack
s
)
{
printf
(
"capacity = %d
\n
"
,
s
.
capacity
);
for
(
int
i
=
s
.
top
;
i
>=
0
;
--
i
)
{
printf
(
"%d
\n
"
,
s
.
data
[
i
]);
}
}
int
main
()
{
struct
stack
s
;
stack_init
(
&
s
,
5
);
stack_print
(
s
);
printf
(
"s.top = %d
\n
"
,
s
.
top
);
printf
(
"is_empty(): %s
\n
"
,
stack_is_empty
(
s
)
?
"True"
:
"False"
);
stack_push
(
&
s
,
10
);
stack_push
(
&
s
,
20
);
stack_push
(
&
s
,
30
);
stack_push
(
&
s
,
40
);
stack_push
(
&
s
,
50
);
stack_push
(
&
s
,
60
);
stack_push
(
&
s
,
70
);
stack_push
(
&
s
,
80
);
stack_push
(
&
s
,
90
);
stack_push
(
&
s
,
100
);
stack_push
(
&
s
,
110
);
printf
(
"is_empty(): %s
\n
"
,
stack_is_empty
(
s
)
?
"True"
:
"False"
);
stack_print
(
s
);
/*int val = -1;*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
printf
(
"peeked value = %d
\n
"
,
*
stack_peek
(
s
));
printf
(
"peeked value = %d
\n
"
,
*
stack_peek
(
s
));
stack_print
(
s
);
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
printf
(
"popped value = %d
\n
"
,
*
stack_pop
(
&
s
));
/*printf("popped value = %d\n", *stack_pop(&s));*/
stack_print
(
s
);
printf
(
"is_empty(): %s
\n
"
,
stack_is_empty
(
s
)
?
"True"
:
"False"
);
return
EXIT_SUCCESS
;
}
This diff is collapsed.
Click to expand it.
slides/exemples/stack.c
+
4
−
0
View file @
8e944960
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
#include
<assert.h>
#define MAX_CAPACITY 5
...
...
@@ -18,6 +19,7 @@ bool stack_is_empty(struct stack s) {
}
void
stack_push
(
struct
stack
*
s
,
int
val
)
{
assert
(
s
->
top
<
MAX_CAPACITY
-
1
);
if
(
s
->
top
!=
MAX_CAPACITY
-
1
)
{
s
->
top
+=
1
;
s
->
data
[
s
->
top
]
=
val
;
...
...
@@ -40,6 +42,8 @@ void stack_push(struct stack *s, int val) {
/*}*/
int
*
stack_pop
(
struct
stack
*
s
)
{
assert
(
!
stack_is_empty
(
*
s
));
if
(
!
stack_is_empty
(
*
s
))
{
int
*
val
=
&
(
s
->
data
[
s
->
top
]);
s
->
top
-=
1
;
...
...
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