Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-stack_sort
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
progseq-stack_sort
Commits
f6fc779c
Commit
f6fc779c
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add stack push, pop and peek
Also fixed various errors in the test file and is_stack_empty methods.
parent
a863042b
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.c
+20
-2
20 additions, 2 deletions
stack.c
test.c
+14
-16
14 additions, 16 deletions
test.c
with
34 additions
and
18 deletions
stack.c
+
20
−
2
View file @
f6fc779c
...
...
@@ -15,19 +15,37 @@ void stack_init(stack *s, int size) {
bool
stack_is_empty
(
stack
s
)
{
bool
is_empty
=
true
;
if
(
s
.
top
>=
0
&&
s
.
size
>=
0
&&
s
.
data
[
0
]
>=
0
)
{
is_empty
=
false
;
}
return
is_empty
;
}
void
stack_push
(
stack
*
s
,
int
val
)
{
int
next_top
=
s
->
top
+
1
;
// Push the value if the stack is not full
if
(
next_top
<
s
->
size
&&
s
->
data
[
next_top
]
<
0
)
{
s
->
data
[
next_top
]
=
val
;
s
->
top
=
next_top
;
}
}
void
stack_pop
(
stack
*
s
,
int
*
val
)
{
if
(
s
->
top
>=
0
)
{
*
val
=
s
->
data
[
s
->
top
];
s
->
data
[
s
->
top
]
=
-
1
;
s
->
top
-=
1
;
s
->
size
-=
1
;
}
}
void
stack_peek
(
stack
s
,
int
*
val
)
{
if
(
s
.
size
>
0
)
{
*
val
=
s
.
data
[
s
.
top
];
}
}
void
stack_destroy
(
stack
*
s
)
{
...
...
This diff is collapsed.
Click to expand it.
test.c
+
14
−
16
View file @
f6fc779c
...
...
@@ -18,7 +18,6 @@ void test_stack_init(stack *s, int size) {
void
test_stack_destroy
(
stack
*
s
)
{
printf
(
"Testing stack_destroy()...
\n
"
);
stack_destroy
(
s
);
// TODO: Verify if s is already null or not
assert
(
s
->
data
==
NULL
);
assert
(
s
->
top
==
-
1
);
printf
(
"OK.
\n
"
);
...
...
@@ -40,7 +39,7 @@ void test_stack_peek(stack s, int *val) {
printf
(
"Testing stack_peek()...
\n
"
);
stack_peek
(
s
,
val
);
// TODO: Verify test
assert
(
val
==
&
s
.
top
);
assert
(
*
val
==
s
.
data
[
s
.
top
]
);
printf
(
"OK.
\n
"
);
}
...
...
@@ -48,14 +47,14 @@ void test_stack_push(stack *s, int val) {
printf
(
"Testing stack_push()...
\n
"
);
stack_push
(
s
,
val
);
// Testing push with remaining spaces
assert
(
s
->
top
==
val
);
assert
(
s
->
data
[
s
->
top
]
==
val
);
// Testing push on full stack
stack
*
stack_full
;
stack
stack_full
;
int
value_top_full_stack_value
=
100
;
stack_init
(
stack_full
,
1
);
stack_push
(
stack_full
,
value_top_full_stack_value
);
stack_push
(
stack_full
,
val
);
assert
(
stack_full
->
top
==
value_top_full_stack_value
);
stack_init
(
&
stack_full
,
1
);
stack_push
(
&
stack_full
,
value_top_full_stack_value
);
stack_push
(
&
stack_full
,
val
);
assert
(
stack_full
.
data
[
stack_full
.
top
]
==
value_top_full_stack_value
);
printf
(
"OK.
\n
"
);
}
...
...
@@ -67,13 +66,12 @@ void test_stack_pop(stack *s, int *val) {
// Add a value if the stack is empty
stack_push
(
s
,
5
);
}
top_before_pop
=
s
->
top
;
top_before_pop
=
s
->
data
[
s
->
top
]
;
stack_pop
(
s
,
val
);
// TODO: Verify test
assert
(
val
==
&
top_before_pop
);
assert
(
*
val
==
top_before_pop
);
// Clear the stack
while
(
s
->
size
!
=
0
)
{
while
(
s
->
top
>
=
0
)
{
stack_pop
(
s
,
val
);
}
// Test pop when stack is empty
...
...
@@ -87,7 +85,7 @@ void test_stack_pop(stack *s, int *val) {
int
main
()
{
stack
s
,
non_empty_stack
;
int
size
=
10
,
value_to_add
=
7
;
int
*
val
;
int
val
;
printf
(
"Starting the tests...
\n
"
);
// Test init and destroy
test_stack_init
(
&
s
,
size
);
...
...
@@ -97,14 +95,14 @@ int main() {
stack_init
(
&
s
,
size
);
stack_init
(
&
non_empty_stack
,
3
);
non_empty_stack
.
data
[
0
]
=
1
;
non_empty_stack
.
top
=
non_empty_stack
.
data
[
0
]
;
non_empty_stack
.
top
=
0
;
// Test the other functions
test_stack_is_empty
(
&
s
);
test_stack_is_not_empty
(
&
non_empty_stack
);
test_stack_peek
(
s
,
val
);
test_stack_peek
(
s
,
&
val
);
test_stack_push
(
&
s
,
value_to_add
);
test_stack_pop
(
&
s
,
val
);
test_stack_pop
(
&
s
,
&
val
);
// Free the memory
stack_destroy
(
&
s
);
...
...
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