Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_142 - Travail Pratique 008
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_142 - Travail Pratique 008
Commits
8a391167
Commit
8a391167
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring
parent
0513733e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.c
+45
-34
45 additions, 34 deletions
main.c
stack.c
+8
-0
8 additions, 0 deletions
stack.c
stack_tests.c
+30
-4
30 additions, 4 deletions
stack_tests.c
with
83 additions
and
38 deletions
main.c
+
45
−
34
View file @
8a391167
...
...
@@ -14,59 +14,65 @@
#include
"stack.h"
void
sort_array
(
int
*
array
,
int
array_length
)
{
stack
s_a
;
stack_init
(
&
s_a
,
array_length
);
stack
s_b
;
stack_init
(
&
s_b
,
array_length
);
for
(
int
i
=
0
;
i
<
array_length
;
i
+=
1
)
{
// Pop values from left stack to right stack.
while
(
!
stack_is_empty
(
s_a
))
{
int
tmp
;
stack_peek
(
s_a
,
&
tmp
);
if
(
array
[
i
]
<
tmp
)
{
break
;
}
void
pop_left_push_right
(
stack
*
left_stack
,
stack
*
right_stack
,
int
current_value
)
{
while
(
!
stack_is_empty
(
*
left_stack
))
{
int
tmp
;
stack_peek
(
*
left_stack
,
&
tmp
);
stack_pop
(
&
s_a
,
&
tmp
)
;
stack_push
(
&
s_b
,
tmp
)
;
if
(
current_value
<
tmp
)
{
break
;
}
// Pop values from right stack to
left
stack
.
while
(
!
stack_is_empty
(
s_b
))
{
int
tmp
;
stack_peek
(
s_b
,
&
tmp
);
stack_pop
(
left
_
stack
,
&
tmp
);
stack_push
(
right_stack
,
tmp
);
}
}
if
(
array
[
i
]
>
tmp
)
{
break
;
}
void
pop_right_push_left
(
stack
*
right_stack
,
stack
*
left_stack
,
int
current_value
)
{
while
(
!
stack_is_empty
(
*
right_stack
))
{
int
tmp
;
stack_peek
(
*
right_stack
,
&
tmp
);
stack_pop
(
&
s_b
,
&
tmp
)
;
stack_push
(
&
s_a
,
tmp
)
;
if
(
current_value
>
tmp
)
{
break
;
}
stack_pop
(
right_stack
,
&
tmp
);
stack_push
(
left_stack
,
tmp
);
}
}
void
sort_array
(
int
*
array
,
int
array_length
)
{
stack
left_stack
;
stack_init
(
&
left_stack
,
array_length
);
stack
right_stack
;
stack_init
(
&
right_stack
,
array_length
);
for
(
int
i
=
0
;
i
<
array_length
;
i
+=
1
)
{
// Pop values from left stack to right stack.
pop_left_push_right
(
&
left_stack
,
&
right_stack
,
array
[
i
]);
// Pop values from right stack to left stack.
pop_right_push_left
(
&
right_stack
,
&
left_stack
,
array
[
i
]);
// Push to left stack.
stack_push
(
&
s_a
,
array
[
i
]);
stack_push
(
&
left_stack
,
array
[
i
]);
}
// Until right stack is empty pop value from right stack and push it to left stack.
while
(
!
stack_is_empty
(
s_b
))
{
while
(
!
stack_is_empty
(
right_stack
))
{
int
tmp
;
stack_pop
(
&
s_b
,
&
tmp
);
stack_push
(
&
s_a
,
tmp
);
stack_pop
(
&
right_stack
,
&
tmp
);
stack_push
(
&
left_stack
,
tmp
);
}
// Until the left stack is empty, pop the value and place it in the array.
for
(
int
i
=
0
;
i
<
array_length
;
i
+=
1
)
{
int
tmp
;
stack_pop
(
&
s_a
,
&
tmp
);
stack_pop
(
&
left_stack
,
&
tmp
);
array
[
i
]
=
tmp
;
}
stack_destroy
(
&
s_a
);
stack_destroy
(
&
s_b
);
stack_destroy
(
&
left_stack
);
stack_destroy
(
&
right_stack
);
}
void
print_array
(
int
*
array
,
int
array_length
)
{
...
...
@@ -77,9 +83,14 @@ void print_array(int *array, int array_length) {
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
2
)
{
printf
(
"Too few arguments.
\n
"
);
return
EXIT_FAILURE
;
}
int
array_length
=
atoi
(
argv
[
1
]);
if
(
array_length
!=
argc
-
2
)
{
if
(
array_length
!=
argc
-
2
||
array_length
==
0
)
{
printf
(
"The length of the array does not correspond to the number of elements.
\n
"
);
return
EXIT_FAILURE
;
}
...
...
This diff is collapsed.
Click to expand it.
stack.c
+
8
−
0
View file @
8a391167
...
...
@@ -34,11 +34,19 @@ void stack_push(stack *s, int val) {
}
void
stack_pop
(
stack
*
s
,
int
*
val
)
{
if
(
stack_is_empty
(
*
s
))
{
return
;
}
stack_peek
(
*
s
,
val
);
s
->
top
-=
1
;
}
void
stack_peek
(
stack
s
,
int
*
val
)
{
if
(
stack_is_empty
(
s
))
{
return
;
}
*
val
=
s
.
data
[
s
.
top
];
}
...
...
This diff is collapsed.
Click to expand it.
stack_tests.c
+
30
−
4
View file @
8a391167
...
...
@@ -9,12 +9,11 @@
*
*/
#include
"stack.h"
#include
<assert.h>
#include
<stdlib.h>
#include
"stack.h"
int
main
()
{
const
int
STACK_LENGTH
=
10
;
...
...
@@ -45,6 +44,17 @@ int main() {
assert
(
s2
.
top
==
2
);
stack_destroy
(
&
s2
);
// Test : stack_push (size overflow)
stack
s2b
;
stack_init
(
&
s2b
,
2
);
stack_push
(
&
s2b
,
1
);
stack_push
(
&
s2b
,
2
);
// Overflow from here.
stack_push
(
&
s2b
,
3
);
stack_push
(
&
s2b
,
4
);
assert
(
s2b
.
top
==
1
);
stack_destroy
(
&
s2b
);
// Test : stack_is_empty
stack
s3
;
stack_init
(
&
s3
,
STACK_LENGTH
);
...
...
@@ -53,7 +63,7 @@ int main() {
assert
(
!
stack_is_empty
(
s3
));
stack_destroy
(
&
s3
);
// Test : stack_p
ush
// Test : stack_p
op
stack
s4
;
stack_init
(
&
s4
,
STACK_LENGTH
);
stack_push
(
&
s4
,
1
);
...
...
@@ -67,6 +77,14 @@ int main() {
assert
(
s4
.
top
==
-
1
);
stack_destroy
(
&
s4
);
// Test : stack_pop (stack is empty)
stack
s4b
;
stack_init
(
&
s4b
,
STACK_LENGTH
);
int
val1b
;
stack_pop
(
&
s4b
,
&
val1b
);
assert
(
s4b
.
top
==
-
1
);
stack_destroy
(
&
s4b
);
// Test : stack_peek
stack
s5
;
stack_init
(
&
s5
,
STACK_LENGTH
);
...
...
@@ -76,4 +94,12 @@ int main() {
assert
(
val2
==
1
);
assert
(
s5
.
top
==
0
);
stack_destroy
(
&
s5
);
// Test : stack_peek (stack is empty)
stack
s5b
;
stack_init
(
&
s5b
,
STACK_LENGTH
);
int
val2b
;
stack_peek
(
s5b
,
&
val2b
);
assert
(
s5b
.
top
==
-
1
);
stack_destroy
(
&
s5b
);
}
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