-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcadastropessoasPilhaFlexivel.c
165 lines (126 loc) · 4.27 KB
/
cadastropessoasPilhaFlexivel.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
INFO:
- As pilhas são um Tipo Abstrato de Dados (TAD) no qual o primeiro elemento que entra é o último a sair
> First In, Last Out (FILO)
- Tem basicamente os métodos de inserir (empilhar, pop) e remover (desempilhar, push)
- Primeira solução IF e RF
> Por exemplo, inserindo o 1, 3, 5 e 7 e efetuando duas remoções teremos:
7 X X
5 5 5 X
3 3 3 3 3
1 1 1 1 1 1
- Segunda solução II e RI (inserção e remoção não eficientes)
> Em cada inserção ou remoção, movemos todos os elementos
> Por exemplo, inserindo o 1, 3, 5 e 7 e efetuando duas remoções teremos:
1 • Primeira remoção: Retorna o 7 e move todos os demais
1 3 • Segunda remoção: Retorna o 5 e move todos os demais
1 3 5 X X
1 3 5 7 1 X
3 1
5 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person{
char *name;
int age;
} Person;
typedef struct Celula {
Person pessoa;
struct Celula* prox;
} Celula;
Celula* newCelula(Person newPessoa) {
Celula* new = (Celula*) malloc(sizeof(Celula));
new->pessoa = newPessoa;
new->prox = NULL;
return new;
}
typedef struct FlexStack{
Celula *top;
} FlexStack;
void start(FlexStack *stack){
stack->top = NULL;
}
void inserir(FlexStack *stack, Person pessoa){
Celula *tmp = newCelula(pessoa);
tmp->prox = stack->top;
stack->top = tmp;
tmp = NULL;
}
Person remover(FlexStack *stack){
if(stack->top == NULL){
printf("Erro! Nao ha datas para remover");
exit(1);
}
Person removedData = stack->top->pessoa;
Celula *tmp = stack->top;
stack->top = stack->top->prox;
tmp->prox = NULL;
return removedData;
}
int sizeStack(FlexStack stack){
Celula *i = stack.top;
int j;
for(j = 0; i != NULL; i = i->prox, j++);
free(i);
return j;
}
void mostrar(FlexStack stack){
if(stack.top != NULL){
int size = sizeStack(stack) - 1;
printf("\nPeossas cadastradas na pilha:\n\n");
for(Celula *i = stack.top; i != NULL; i = i->prox)
printf("[Pessoa %d]:\nNome: %s\nIdade: %d\n\n", size--, i->pessoa.name,
i->pessoa.age);
}
else{
printf("Nao ha pessoas.\n");
}
}
char *strAloc(char *str){
char *str2 = (char*)malloc((strlen(str) + 1) * sizeof(char));
strcpy(str2, str);
return str2;
}
int main(){
FlexStack stack;
start(&stack);
char input[50];
int cadastros = 0;
printf("-------------------------------------------------------------\n");
printf("Preencha os dados das pessoas e digite \"FIM\" para finalizar\n");
printf("-------------------------------------------------------------\n");
printf("\n 0 Pessoas\nNome ou FIM: ");
scanf("%[^\n]", input);
while(strcmp(input, "FIM") != 0){
getchar();
Person newPessoa;
newPessoa.name = strAloc(input);
printf("Idade: ");
while(scanf("%d", &newPessoa.age) != 1){
printf("\nDigite uma idade valida: ");
while (getchar() != '\n');
}
getchar();
inserir(&stack, newPessoa);
printf("\n%d Pessoas\nNome ou FIM: ", cadastros);
scanf("%[^\n]", input);
cadastros++;
}
int removes;
printf("\nDigite quantas pessoas deseja remover: ");
while(scanf("%d", &removes) != 1){
printf("\nDigite um numero valido: ");
while (getchar() != '\n');
}
while(removes > cadastros && cadastros > 0){
printf("Nao e possivel remover %d pessoas, pois ha apenas %d.\nTente novamente: ", removes, cadastros);
while(scanf("%d", &removes) != 1){
printf("\nDigite um numero valido: ");
while (getchar() != '\n');
}
}
for(int i = 0; i < removes; ++i, remover(&stack));
mostrar(stack);
}