Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
PREMIERE
ALGO
algorithme
Commits
9584ab55
Commit
9584ab55
authored
Nov 17, 2021
by
jonas.stirnema
Browse files
Realloc in push
parent
1b8ce2a5
Changes
2
Hide whitespace changes
Inline
Side-by-side
stack/include/stack.h
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
);
...
...
stack/src/stack.c
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment