Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
algorithme
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PREMIERE
ALGO
algorithme
Commits
9584ab55
Project 'stirnemann/algorithme' was moved to 'HEPIA-PREMIERE/ALGO/algorithme'. Please update any links and bookmarks that may still have the old path.
Commit
9584ab55
authored
3 years ago
by
jonas.stirnema
Browse files
Options
Downloads
Patches
Plain Diff
Realloc in push
parent
1b8ce2a5
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
stack/include/stack.h
+3
-4
3 additions, 4 deletions
stack/include/stack.h
stack/src/stack.c
+9
-4
9 additions, 4 deletions
stack/src/stack.c
with
12 additions
and
8 deletions
stack/include/stack.h
+
3
−
4
View file @
9584ab55
...
...
@@ -10,22 +10,21 @@
#include
<stdint.h>
#include
<stdlib.h>
#ifndef _JSTACK_H_
#define _JSTACK_H_
// DEFINES
#define
MAX_CAPACITY 5
00
#define
INCREMENT_STACK 1
00
typedef
struct
{
uint64_t
max
;
int64_t
top
;
int32_t
*
data
;
}
stack_t
;
// PROTOTYPE FUNCTIONS
int32_t
*
create_stack
();
int32_t
*
create_stack
(
uint64_t
capa
);
void
stack_push
(
stack_t
*
st
,
int32_t
val
);
int32_t
stack_pop
(
stack_t
*
st
);
int32_t
stack_peek
(
stack_t
st
);
...
...
This diff is collapsed.
Click to expand it.
stack/src/stack.c
+
9
−
4
View file @
9584ab55
...
...
@@ -7,21 +7,26 @@
#include
"../include/stack.h"
int32_t
*
create_stack
()
int32_t
*
create_stack
(
uint64_t
capa
)
{
stack_t
*
st
=
malloc
(
sizeof
(
stack_t
));
st
->
data
=
malloc
(
MAX_CAPACITY
*
sizeof
(
int32_t
));
st
->
max
=
capa
;
st
->
data
=
malloc
(
st
->
max
*
sizeof
(
int32_t
));
st
->
top
=
-
1
;
}
void
stack_push
(
stack_t
*
st
,
int32_t
val
)
{
// UP CAPACITY WHEN MAX IS REACHED
if
(
st
->
top
==
st
->
max
)
{
realloc
(
st
->
data
,
st
->
max
+
INCREMENT_STACK
);
}
st
->
data
[
++
st
->
top
]
=
val
;
}
int32_t
stack_pop
(
stack_t
*
st
)
{
return
st
->
data
[
st
->
top
--
];
}
...
...
@@ -32,7 +37,7 @@ int32_t stack_peek(stack_t st)
bool
stack_is_full
(
stack_t
st
)
{
return
st
.
top
==
MAX_CAPACITY
;
return
st
.
top
==
st
.
max
;
}
bool
stack_is_empty
(
stack_t
st
)
...
...
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