PILAS EN C++
1: //PILAS
2:
3: #include
4: #include
5: #include
6: #define max 5
7:
8: typedef int tipodatopila;
9:
10: tipodatopila pila[max];
11: int tope;
12:
13: void visualizarpila(tipodatopila *pila);
14: void inicializarpila(int *tope);
15: void menu(int *tope, tipodatopila *pila);
16: int pilavacia(int *tope);
17: int pilallena(int *tope);
18: void push(int *tope, tipodatopila *pila, tipodatopila *dato);
19: void alta(int *tope);
20: tipodatopila pop(int *tope, tipodatopila *pila)
21: {
22: tipodatopila b=*(tope)+*(pila);
23: *(tope)=*(tope)-1;
24: return(b);
25:
26: }
27: void baja(int *tope, tipodatopila *pila)
28: {
29: if(pilavacia(tope)==1)
30: {
31: gotoxy(40,4); cout<<"LA PILA ESTA VACIA";
32: }
33: else
34: {
35: gotoxy(40,4); cout<<"EL DATO ELIMINADO FUE "<<pop(tope,pila);
36: }
37: }
38:
39:
40: main()
41: {
42: menu(&tope,&pila[0]);
43: }
44:
45: void menu(int *tope, tipodatopila *pila)
46: {
47: int op;
48:
49: do
50: {
51: clrscr();
52: gotoxy(10,10); visualizarpila(pila);
53: gotoxy(30,2); cout<<"PILAS";
54: gotoxy(10,4); cout<<"1. INICIALIZAR PILA";
55: gotoxy(10,5); cout<<"2. ALTA";
56: gotoxy(10,6); cout<<"3. BAJA";
57: gotoxy(10,7); cout<<"4. SALIR";
58: gotoxy(10,8); cout<<"ELIGA UNA OPCION [ ]";
59: gotoxy(29,8); cin>>op;
60: switch(op)
61: {
62: case 1:
63: inicializarpila(tope);
64: break;
65: case 2:
66: alta(tope);
67: getch();
68: break;
69: case 3:
70: baja(tope,pila);
71: getch();
72: break;
73: case 4:
74: clrscr();
75: cout<<"ADIOS ;D";
76: getch;
77: break;
78:
79: default:
80: cout<<"HAZ TECLADO UNA LETRA";
81: break;
82:
83: }
84:
85: }while(op!=4);
86:
87: }
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109: int pilallena(int *tope)
110: {
111: if(*(tope)==max-1)
112: {
113: return(1);
114: }
115: else
116: {
117: return(0);
118: }
119: }
120:
121: //Pila vacia
122: int pilavacia(int *tope)
123: {
124: if(*(tope)==-1)
125: {
126: return(1);
127: }
128: else
129: {
130: return(0);
131: }
132: }
133:
134:
135:
136:
137:
138:
139:
140: //Funcion inicializar pila
141: void inicializarpila(int *tope)
142: {
143: *(tope)=-1;
144: }
145:
146: //Visualizar pila
147: void visualizarpila(tipodatopila *pila)
148: {
149: for(int f=0; f<max; f++)
150: {
151: cout<<"n"<<*(pila+f);
152: }
153: }
154:
155: void push(int *tope, tipodatopila *pila, tipodatopila *dato)
156: {
157: *(tope)=*(tope)+1;
158: *(pila+*tope)=*dato;
159: }
160:
161: void alta(int *tope)
162: {
163: tipodatopila dato;
164: if(pilallena(tope)==1)
165: {
166: gotoxy(40,4); cout<<"LA PILA ESTA LLENA";
167: }
168: else
169: {
170: gotoxy(40,5); cout<<"INSERTE EL DATO "; cin>>dato;
171: gotoxy(40,6); push(tope,pila,&dato);
172: }
173: }
Deja un comentario