-
Notifications
You must be signed in to change notification settings - Fork 0
/
pila_estatica.cpp
executable file
·69 lines (55 loc) · 1.3 KB
/
pila_estatica.cpp
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMS 10
typedef struct pila {
int cima;
int valores[MAX_ELEMS];
} tipo_pila;
//Inicializar pila estatica
void inicializar(tipo_pila *p){
p->cima=0;
}
//Apilar pila estatica
void push(tipo_pila *p, int i, int *error){
*error = 0;
if (p->cima < MAX_ELEMS)
p->valores[p->cima++] = i;
else
*error = -1;
}
//Desapilar pila estatica
void pop(tipo_pila *p, int * error){
*error = 0;
if (p->cima > 0)
p->valores[--p->cima];
else
*error = -1;
}
//Busqueda en pila estatica
int busqueda(tipo_pila *p,int valor){
int i=0;
for(;p->cima>=0;p->valores[--p->cima]) {
i++;
if (p->valores[p->cima] == valor) return i;
}
return -1;
}
int main()
{
tipo_pila *pila = (tipo_pila*)malloc(sizeof(tipo_pila));
int error;
//Inicializar
inicializar(pila);
//Apilar
push(pila,7,&error);
push(pila,2,&error);
push(pila,4,&error);
//Buscar un valor
printf("Num. operaciones: %i \n",busqueda(pila,2));
//Desapilar
pop(pila,&error);
pop(pila,&error);
pop(pila,&error);
system("pause");
return 0;
}