-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_3_6.c
83 lines (74 loc) · 1.21 KB
/
crack_3_6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#define MAXLENGTH 100
typedef struct Stack {
int size;
int array[MAXLENGTH];
} Stack;
Stack *initStack();
void push(Stack *, int);
int pop(Stack *);
int empty(Stack *);
void sort(int[], int);
void swap(int *, int *);
main()
{
Stack *stack = initStack();
push(stack, 0);
push(stack, 3);
push(stack, 1);
int temp[MAXLENGTH] = {-1};
int n = 0;
while (empty(stack) == 1) {
temp[n++] = pop(stack);
}
sort(temp, n);
int m = 0;
for (; m < n; m++) {
push(stack, temp[m]);
printf("%d ", temp[m]);
}
printf("\n");
printf("%d\n", pop(stack));
return 0;
}
Stack *initStack()
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->size = 0;
return stack;
}
void push(Stack *stack, int data)
{
if (stack->size < MAXLENGTH)
(stack->array)[(stack->size)++] = data;
}
int pop(Stack *stack)
{
if (stack->size > 0)
return (stack->array)[--(stack->size)];
return -1;
}
int empty(Stack *stack)
{
return (stack->size) > 0 ? 1 : 0;
}
void sort(int temp[], int n)
{
int i = 0;
int j;
for (; i < n; i++) {
j = i + 1;
for (; j < n; j++) {
if (temp[i] > temp[j])
swap(&temp[i], &temp[j]);
}
}
i = 0;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}