Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
algorithmique
lib_2021_malaspinas
stack
Commits
a6cadd6d
Commit
a6cadd6d
authored
Dec 02, 2021
by
tom.ryser
Committed by
orestis.malaspin
Dec 02, 2021
Browse files
Resolve "add makefile with structure"
parent
c4e5b4ad
Changes
7
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
a6cadd6d
# Object files
*.o
main
\ No newline at end of file
*.exe
# Executables
main
Makefile
0 → 100644
View file @
a6cadd6d
CC
:=
gcc
CFLAGS
:=
-g
-Wall
-Wextra
-pedantic
-fsanitize
=
address
LDFLGS
:=
-fsanitize
=
address
NAME
:=
stack
$(NAME)
:
main.o $(NAME).o
$(CC)
$(CFLAGS)
-o
$@
$^
$(LDFLAGS)
test
:
$(NAME)_test.o $(NAME).o
$(CC)
$(CFLAGS)
-o
$@
$^
./test
$(NAME).o
:
$(NAME).h
$(NAME)_test.o
:
$(NAME).h
.PHONY
:
clean
clean
:
rm
-f
*
.o
$(NAME)
test
README.md
View file @
a6cadd6d
# stack
# Stack
```
📦project
┣ 📜main.c
┣ 📜stack.c
┣ 📜stack.h
┣ 📜.gitignore
┣ 📜Makefile
┗ 📜README.md
```
main.c
0 → 100644
View file @
a6cadd6d
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
int
main
(
void
)
{
return
EXIT_SUCCESS
;
}
stack.c
View file @
a6cadd6d
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define DEFAULT_CAPACITY 4
void
stack_init
(
stack
*
s
)
{
s
->
top
=
-
1
;
void
stack_init
(
stack
*
s
)
{
s
->
top
=
-
1
;
s
->
capacity
=
DEFAULT_CAPACITY
;
s
->
data
=
malloc
(
sizeof
(
int
)
*
DEFAULT_CAPACITY
);
s
->
data
=
malloc
(
sizeof
(
int
)
*
DEFAULT_CAPACITY
);
}
void
stack_destroy
(
stack
*
s
){
void
stack_destroy
(
stack
*
s
)
{
free
(
s
->
data
);
s
->
data
=
NULL
;
s
->
data
=
NULL
;
s
->
capacity
=
-
1
;
s
->
top
=
-
1
;
s
->
top
=
-
1
;
}
void
stack_pop
(
stack
*
s
,
int
*
value
){
if
(
stack_is_empty
(
*
s
))
{
return
;
}
if
(
s
->
top
==
s
->
capacity
/
4
){
s
->
capacity
/=
2
;
s
->
data
=
realloc
(
s
->
data
,
sizeof
(
int
)
*
s
->
capacity
);
}
void
stack_pop
(
stack
*
s
,
int
*
value
)
{
if
(
stack_is_empty
(
*
s
))
{
return
;
}
if
(
s
->
top
==
s
->
capacity
/
4
)
{
s
->
capacity
/=
2
;
s
->
data
=
realloc
(
s
->
data
,
sizeof
(
int
)
*
s
->
capacity
);
}
*
value
=
s
->
data
[
s
->
top
];
s
->
top
-=
1
;
*
value
=
s
->
data
[
s
->
top
];
s
->
top
-=
1
;
}
void
stack_peek
(
stack
s
,
int
*
value
){
void
stack_peek
(
stack
s
,
int
*
value
)
{
if
(
!
stack_is_empty
(
s
))
{
*
value
=
s
.
data
[
s
.
top
];
}
}
void
stack_print
(
const
stack
s
)
{
//TODO: replace if statement with following as soon as relevant function is
implemented
//
if (!stack_is_empty()) {
if
(
s
.
top
>=
0
)
{
printf
(
" TOP
\n
--------------------
\n
"
);
for
(
int
*
spot
=
s
.
data
+
s
.
top
;
spot
>=
s
.
data
;
--
spot
)
{
printf
(
"%8d | %12d
\n
"
,
spot
-
s
.
data
,
*
spot
);
}
printf
(
"--------------------
\n
BOTTOM
\n
"
);
}
else
{
printf
(
"STACK EMPTY
\n
"
);
}
//
TODO: replace if statement with following as soon as relevant function is
// implemented
if (!stack_is_empty()) {
if
(
s
.
top
>=
0
)
{
printf
(
" TOP
\n
--------------------
\n
"
);
for
(
int
*
spot
=
s
.
data
+
s
.
top
;
spot
>=
s
.
data
;
--
spot
)
{
printf
(
"%8
l
d | %12d
\n
"
,
spot
-
s
.
data
,
*
spot
);
}
printf
(
"--------------------
\n
BOTTOM
\n
"
);
}
else
{
printf
(
"STACK EMPTY
\n
"
);
}
}
void
stack_clone
(
stack
s
,
stack
*
clone
)
{
clone
->
top
=
s
.
top
;
clone
->
top
=
s
.
top
;
clone
->
capacity
=
s
.
capacity
;
clone
->
data
=
malloc
(
sizeof
(
int
)
*
s
.
capacity
);
clone
->
data
=
malloc
(
sizeof
(
int
)
*
s
.
capacity
);
for
(
int
i
=
0
;
i
<=
s
.
top
&&
i
<
s
.
capacity
;
i
++
)
{
clone
->
data
[
i
]
=
s
.
data
[
i
];
}
}
int
get_length
(
stack
s
)
{
int
get_length
(
stack
s
)
{
return
s
.
top
+
1
;
}
stack.h
View file @
a6cadd6d
...
...
@@ -19,3 +19,4 @@ int get_length(stack s);
void
stack_print
(
const
stack
s
);
#endif
stack_test.c
View file @
a6cadd6d
#include "minunit.h"
#include "stack.h"
#include <stdlib.h>
MU_TEST
(
stack_init_test
)
{
// Arrange
...
...
@@ -20,8 +21,8 @@ MU_TEST_SUITE(stack_test_suite) {
MU_RUN_TEST
(
stack_init_test
);
}
int
main
()
{
int
main
()
{
MU_RUN_SUITE
(
stack_test_suite
);
MU_REPORT
();
return
MU_EXIT_CODE
;
}
\ No newline at end of file
}
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