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
ae282f32
Verified
Commit
ae282f32
authored
4 months ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added stack live code
parent
fc6a3724
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#36433
passed
4 months ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/exemples/stack.c
+117
-0
117 additions, 0 deletions
slides/exemples/stack.c
with
117 additions
and
0 deletions
slides/exemples/stack.c
0 → 100644
+
117
−
0
View file @
ae282f32
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
#define MAX_CAPACITY 5
struct
stack
{
int
top
;
int
data
[
MAX_CAPACITY
];
};
void
stack_init
(
struct
stack
*
s
)
{
s
->
top
=
-
1
;
}
bool
stack_is_empty
(
struct
stack
s
)
{
return
s
.
top
==
-
1
;
}
void
stack_push
(
struct
stack
*
s
,
int
val
)
{
if
(
s
->
top
!=
MAX_CAPACITY
-
1
)
{
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
;
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
)
{
for
(
int
i
=
s
.
top
;
i
>=
0
;
--
i
)
{
printf
(
"%d
\n
"
,
s
.
data
[
i
]);
}
}
int
main
()
{
struct
stack
s
;
stack_init
(
&
s
);
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
);
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));*/
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.
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