-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpilhaSeq.c
92 lines (81 loc) · 2.38 KB
/
pilhaSeq.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
/*
* File: pilhaSeq.c
* Author: Fábio Tramasoli (0619132)
* About: Pilha Seqüencial agnóstica a tipos.
*
*/
#include <string.h>
#include "pilhaSeq.h"
int inicializaPilhaSeq(tPilhaSeq *pilha, int bytes){
pilha->topo = -1;
pilha->bytes=bytes;
pilha->conteudo = malloc(sizeof(char)*PILHASEQ_TAMANHO*pilha->bytes);
}
int finalizaPilhaSeq(tPilhaSeq *pilha) {
free(pilha->conteudo);
pilha->topo = -1;
return PILHASEQ_OPERACAO_OK;
}
int vaziaPilhaSeq (tPilhaSeq *pilha){
if ((pilha->topo)==-1)
return PILHASEQ_VAZIA;
else
return PILHASEQ_OPERACAO_OK;
}
int cheiaPilhaSeq (tPilhaSeq *pilha){
if ((pilha->topo)==(PILHASEQ_TAMANHO-1))
return PILHASEQ_CHEIA;
return PILHASEQ_OPERACAO_OK;
}
int pushPilhaSeq(tPilhaSeq *pilha, void *valor){
if(cheiaPilhaSeq(pilha)==PILHASEQ_CHEIA){
return PILHASEQ_OPERACAO_ERR;
} else{
(pilha->topo)++;
memcpy((pilha->conteudo+(pilha->topo*pilha->bytes)),valor,pilha->bytes);
return PILHASEQ_OPERACAO_OK;//indica sucesso
}
}
int popPilhaSeq (tPilhaSeq *pilha, void *valor){
if(vaziaPilhaSeq(pilha)!=PILHASEQ_VAZIA){
memcpy(valor,(pilha->conteudo+(pilha->topo*pilha->bytes)),pilha->bytes);
(pilha->topo)--;
return PILHASEQ_OPERACAO_OK;
} else{
return PILHASEQ_OPERACAO_ERR;
}
}
int primeiroPilhaSeq (tPilhaSeq *pilha, void *valor){
if(vaziaPilhaSeq(pilha)!=PILHASEQ_VAZIA){
memcpy(valor,(pilha->conteudo+(pilha->topo*pilha->bytes)),pilha->bytes);
return PILHASEQ_OPERACAO_OK;
} else{
return PILHASEQ_OPERACAO_ERR;
}
}
int tamanhoPilhaSeq(tPilhaSeq *pilha){
int status;
char *aux;
tPilhaSeq *pAux = malloc(sizeof(tPilhaSeq));
inicializaPilhaSeq(&pAux,pilha->bytes);
int cont=0;
while(!vaziaPilhaSeq(pilha)){
aux=malloc(pilha->bytes);
status=popPilhaSeq(pilha,aux);
if(status==PILHASEQ_OPERACAO_ERR){//algo ruim aconteceu
printf("***Erro!***");
}else{
cont++;
pushPilhaSeq(pAux,aux);
}
free(aux);
}
while(!vaziaPilhaSeq(pAux)){
aux=malloc(pilha->bytes);
popPilhaSeq(pAux,aux);
pushPilhaSeq(pilha,aux);
free(aux);
}
free(pAux);
return cont;
}