This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercicio_pilha_9.c
103 lines (90 loc) · 1.55 KB
/
exercicio_pilha_9.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool int
#define true 1
#define false 0
#define MAX 10
typedef struct stack{
int vetor[MAX];
int bottom;
int top;
int devide;
}stack;
bool empty(stack *s);
bool full (stack *s);
bool push (stack *s, int *c);
void pop (stack *s, int *c);
void listar(stack *s);
void ordena(stack *s);
int main(){
stack *S = (stack*) malloc(sizeof(stack));
int aux,c;
for(c=0;c<5;c++){
scanf("%d",&aux);
push(S,&aux);
}
ordena(S);
printf("----ORDENADA----\n");
listar(S);
free(S);
return 0;
}
void ordena(stack *s){
stack *temp = (stack*) malloc (sizeof(stack));
int aux_s,aux_temp;
while(!empty(s)){
pop(s,&aux_s);
pop(temp,&aux_temp);
if(aux_s >= aux_temp){
if(aux_temp!=0){
push(temp,&aux_temp);
}
push(temp,&aux_s);
}
else{
while(aux_s < aux_temp && !empty(temp)){
push(s,&aux_temp);
pop(temp,&aux_temp);
}
push(s,&aux_temp);
push(temp,&aux_s);
}
}
while(!empty(temp)){
pop(temp,&aux_s);
push(s,&aux_s);
}
free(temp);
}
void listar(stack *s){
int aux;
while(!empty(s)){
pop(s,&aux);
printf(" %d ",aux);
}
printf("\n");
}
void pop (stack *s, int *c){
if(empty(s)){
//printf("EMPTY STACK \n");
return;
}
s->top--;
*c = s->vetor[s->top];
}
bool push (stack *s, int *c){
if(full(s)){
printf("STACK FULL. EXIT REQUIRED \n");
return false;
}
s->vetor[s->top] = *c;
s->top++;
return true;
}
bool empty(stack *s){
return s->top == 0;
}
bool full (stack *s){
return s->top == MAX;
}