-
Lenguaje
Pascal
-
Descripción
Implementa la estructura de datos de tipo pila de números enteros.
Contiene funciones como:- Insertar un número al final
- Quitar el último número
- Listar los elementos de la pila
- Obtener el tamaño de la pila
- Vaciar la pila
- Insertar números aleatorios
- Insertar un número al principio
- Consular si contiene un número
- Quitar un número a buscar
- Quitar el primer número
- Ordenar la pila
- Invertir la pila
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
program prog_pila;
uses crt;
{$ifdef LINUX}
const FLECHA = #226#148#128#226#150#186;
const AACUTE = #195#161;
const IACUTE = #195#173;
const OACUTE = #195#179;
const UACUTE = #195#186;
const AACUTE_= #195#129;
const NTILDE = #195#177;
{$else}
const FLECHA = #196#16;
const AACUTE = #160;
const IACUTE = #161;
const OACUTE = #162;
const UACUTE = #163;
const AACUTE_= #181;
const NTILDE = #164;
{$endif}
type Pila = ^TDAPila;
TDAPila = object
liga : Pila;
dato : integer;
end;
function pila_create : Pila;
begin
pila_create := nil;
end;
function pila_peek (nodo : Pila) : integer;
begin
pila_peek := nodo^.dato;
end;
function pila_empty (nodo : Pila) : boolean;
begin
pila_empty := nodo = nil;
end;
function pila_push (cabecera : Pila; dato : integer) : Pila;
var nodo : Pila;
begin
new (nodo);
nodo^.liga := cabecera;
nodo^.dato := dato;
pila_push := nodo;
end;
function pila_pop (nodo : Pila) : Pila;
var liga : Pila;
begin
if not pila_empty(nodo) then
begin
liga := nodo^.liga;
dispose (nodo);
pila_pop := liga;
end
else
pila_pop := pila_create;
end;
function pila_remove (nodo : Pila; dato : integer) : Pila;
begin
if not pila_empty(nodo) then
begin
if pila_peek (nodo) = dato then
begin
pila_remove := pila_pop (nodo);
exit;
end;
nodo^.liga := pila_remove (nodo^.liga, dato);
end;
pila_remove := nodo;
end;
function pila_contains (nodo : Pila; dato : integer) : boolean;
begin
pila_contains := (not pila_empty(nodo)) and ((pila_peek (nodo) = dato) or pila_contains (nodo^.liga, dato));
end;
procedure pila_list (nodo : Pila);
begin
if not pila_empty(nodo) then
begin
pila_list (nodo^.liga);
writeln (pila_peek (nodo));
end;
end;
function pila_sort (nodo : Pila) : boolean;
var dato : integer;
var cambio : boolean;
begin
cambio := false;
if (not pila_empty(nodo)) and (not pila_empty(nodo^.liga)) then
begin
if pila_peek (nodo) < pila_peek (nodo^.liga) then
begin
dato := pila_peek (nodo);
nodo^.dato := pila_peek (nodo^.liga);
nodo^.liga^.dato := dato;
cambio := true;
end;
cambio := cambio or pila_sort (nodo^.liga);
if cambio then
begin
cambio := pila_sort (nodo);
cambio := true;
end;
end;
pila_sort := cambio;
end;
procedure pila_print (nodo : Pila);
begin
if not pila_empty(nodo) then
begin
write (pila_peek (nodo), ' ', FLECHA, ' ');
pila_print (nodo^.liga);
end
else
writeln ('nil'#10#13);
end;
function pila_size (nodo : Pila) : integer;
begin
if pila_empty(nodo) then
pila_size := 0
else
pila_size := 1 + pila_size (nodo^.liga);
end;
function pila_reverse (nodo : Pila; inversa : Pila) : Pila;
begin
if not pila_empty(nodo) then
begin
inversa := pila_reverse (nodo^.liga, pila_push (inversa, pila_peek (nodo)));
dispose (nodo);
end;
pila_reverse := inversa;
end;
function pila_insert_first (nodo : Pila; dato : integer) : Pila;
begin
if pila_empty(nodo) then
pila_insert_first := pila_push (pila_create, dato)
else
begin
nodo^.liga := pila_insert_first (nodo^.liga, dato);
pila_insert_first := nodo;
end;
end;
function pila_remove_first (nodo : Pila) : Pila;
begin
if not pila_empty(nodo) then
begin
if pila_empty(nodo^.liga) then
begin
pila_remove_first := pila_pop (nodo);
exit;
end;
nodo^.liga := pila_remove_first (nodo^.liga);
end;
pila_remove_first := nodo;
end;
function leer_entero (mensaje : string) : integer;
var entero : integer;
begin
write ('Ingrese ', mensaje, ': ');
readln (entero);
writeln;
leer_entero := entero;
end;
var cabecera : Pila;
var dato : integer;
var opcion, tecla : char;
var cambio : boolean;
begin
cabecera := pila_create;
randomize;
repeat
clrscr;
writeln ('OPERACIONES B', AACUTE_, 'SICAS DE PILAS');
writeln ('a) Insertar un n', UACUTE, 'mero al final');
writeln ('b) Quitar el ', UACUTE, 'ltimo n', UACUTE, 'mero');
writeln ('c) Listar n', UACUTE, 'meros');
writeln ('d) Tama', NTILDE, 'o de la pila');
writeln ('e) Vaciar pila');
writeln ('OPERACIONES COMPLEMENTARIAS');
writeln ('f) Insertar n', UACUTE, 'meros aleatorios');
writeln ('g) Insertar un n', UACUTE, 'mero al principio');
writeln ('h) Consular si contiene un n', UACUTE, 'mero');
writeln ('i) Quitar un n', UACUTE, 'mero a buscar');
writeln ('j) Quitar el primer n', UACUTE, 'mero');
writeln ('k) Ordenar pila');
writeln ('l) Ivertir pila');
writeln ('m) Salir'#10#13);
write ('Seleccione una opci', OACUTE, 'n: ');
repeat
opcion := upcase (readkey);
until (opcion >= 'A') and (opcion <= 'M');
opcion := chr(ord(opcion) + 32);
if opcion <> 'm' then
begin
writeln (opcion + #10#13);
if pila_empty(cabecera) and (opcion <> 'a') and (opcion <> 'd') and (opcion <> 'f') and (opcion <> 'g') then
writeln ('La pila est', AACUTE, ' vac', IACUTE, 'a.'#10#13)
else
begin
if (opcion <> 'c') and (opcion <> 'd') and (opcion <> 'h') then
begin
writeln ('Pila original:');
pila_print (cabecera);
end;
case opcion of
'a': { Insertar un numero al final }
cabecera := pila_push (cabecera, leer_entero ('el n' + UACUTE + 'mero a insertar'));
'b': { Quitar el ultimo numero }
cabecera := pila_pop (cabecera);
'c': { Listar numeros }
begin
pila_list (cabecera);
writeln;
end;
'd': { Tamano de la pila }
writeln ('Tama', NTILDE, 'o de la pila: ', pila_size (cabecera), '.'#10#13);
'e': { Vaciar pila }
while not pila_empty(cabecera) do
cabecera := pila_pop (cabecera);
'f': { Insertar numeros aleatorios }
begin
dato := leer_entero ('la cantidad de n' + UACUTE + 'meros aleatorios a insertar');
while dato>0 do
begin
cabecera := pila_push (cabecera, random (1000));
dec (dato);
end;
end;
'g': { Insertar un numero al principio }
begin
dato := leer_entero ('el n' + UACUTE + 'mero a insertar');
cabecera := pila_insert_first (cabecera, dato);
end;
'h': { Consular si existe un numero }
begin
dato := leer_entero ('el n' + UACUTE + 'mero a consultar');
if pila_contains (cabecera, dato) then
writeln ('El n', UACUTE, 'mero ', dato, ' s', IACUTE, ' est', AACUTE,
' en la pila.'#10#13)
else
writeln ('El n', UACUTE, 'mero ', dato, ' no est', AACUTE, ' en la pila.'#10#13);
end;
'i': { Quitar un numero a buscar }
cabecera := pila_remove (cabecera, leer_entero ('el n' + UACUTE + 'mero a remove'));
'j': { Quitar el primer numero }
cabecera := pila_remove_first (cabecera);
'k': { Ordenar pila }
cambio := pila_sort (cabecera);
'l': { Ivertir pila }
cabecera := pila_reverse (cabecera, pila_create);
end;
if (opcion <> 'c') and (opcion <> 'd') and (opcion <> 'h') then
begin
writeln ('Pila actualizada:');
pila_print (cabecera);
end;
end;
write ('Presione una tecla para continuar . . . ');
tecla := readkey;
end;
until opcion = 'm';
end.
uses crt;
{$ifdef LINUX}
const FLECHA = #226#148#128#226#150#186;
const AACUTE = #195#161;
const IACUTE = #195#173;
const OACUTE = #195#179;
const UACUTE = #195#186;
const AACUTE_= #195#129;
const NTILDE = #195#177;
{$else}
const FLECHA = #196#16;
const AACUTE = #160;
const IACUTE = #161;
const OACUTE = #162;
const UACUTE = #163;
const AACUTE_= #181;
const NTILDE = #164;
{$endif}
type Pila = ^TDAPila;
TDAPila = object
liga : Pila;
dato : integer;
end;
function pila_create : Pila;
begin
pila_create := nil;
end;
function pila_peek (nodo : Pila) : integer;
begin
pila_peek := nodo^.dato;
end;
function pila_empty (nodo : Pila) : boolean;
begin
pila_empty := nodo = nil;
end;
function pila_push (cabecera : Pila; dato : integer) : Pila;
var nodo : Pila;
begin
new (nodo);
nodo^.liga := cabecera;
nodo^.dato := dato;
pila_push := nodo;
end;
function pila_pop (nodo : Pila) : Pila;
var liga : Pila;
begin
if not pila_empty(nodo) then
begin
liga := nodo^.liga;
dispose (nodo);
pila_pop := liga;
end
else
pila_pop := pila_create;
end;
function pila_remove (nodo : Pila; dato : integer) : Pila;
begin
if not pila_empty(nodo) then
begin
if pila_peek (nodo) = dato then
begin
pila_remove := pila_pop (nodo);
exit;
end;
nodo^.liga := pila_remove (nodo^.liga, dato);
end;
pila_remove := nodo;
end;
function pila_contains (nodo : Pila; dato : integer) : boolean;
begin
pila_contains := (not pila_empty(nodo)) and ((pila_peek (nodo) = dato) or pila_contains (nodo^.liga, dato));
end;
procedure pila_list (nodo : Pila);
begin
if not pila_empty(nodo) then
begin
pila_list (nodo^.liga);
writeln (pila_peek (nodo));
end;
end;
function pila_sort (nodo : Pila) : boolean;
var dato : integer;
var cambio : boolean;
begin
cambio := false;
if (not pila_empty(nodo)) and (not pila_empty(nodo^.liga)) then
begin
if pila_peek (nodo) < pila_peek (nodo^.liga) then
begin
dato := pila_peek (nodo);
nodo^.dato := pila_peek (nodo^.liga);
nodo^.liga^.dato := dato;
cambio := true;
end;
cambio := cambio or pila_sort (nodo^.liga);
if cambio then
begin
cambio := pila_sort (nodo);
cambio := true;
end;
end;
pila_sort := cambio;
end;
procedure pila_print (nodo : Pila);
begin
if not pila_empty(nodo) then
begin
write (pila_peek (nodo), ' ', FLECHA, ' ');
pila_print (nodo^.liga);
end
else
writeln ('nil'#10#13);
end;
function pila_size (nodo : Pila) : integer;
begin
if pila_empty(nodo) then
pila_size := 0
else
pila_size := 1 + pila_size (nodo^.liga);
end;
function pila_reverse (nodo : Pila; inversa : Pila) : Pila;
begin
if not pila_empty(nodo) then
begin
inversa := pila_reverse (nodo^.liga, pila_push (inversa, pila_peek (nodo)));
dispose (nodo);
end;
pila_reverse := inversa;
end;
function pila_insert_first (nodo : Pila; dato : integer) : Pila;
begin
if pila_empty(nodo) then
pila_insert_first := pila_push (pila_create, dato)
else
begin
nodo^.liga := pila_insert_first (nodo^.liga, dato);
pila_insert_first := nodo;
end;
end;
function pila_remove_first (nodo : Pila) : Pila;
begin
if not pila_empty(nodo) then
begin
if pila_empty(nodo^.liga) then
begin
pila_remove_first := pila_pop (nodo);
exit;
end;
nodo^.liga := pila_remove_first (nodo^.liga);
end;
pila_remove_first := nodo;
end;
function leer_entero (mensaje : string) : integer;
var entero : integer;
begin
write ('Ingrese ', mensaje, ': ');
readln (entero);
writeln;
leer_entero := entero;
end;
var cabecera : Pila;
var dato : integer;
var opcion, tecla : char;
var cambio : boolean;
begin
cabecera := pila_create;
randomize;
repeat
clrscr;
writeln ('OPERACIONES B', AACUTE_, 'SICAS DE PILAS');
writeln ('a) Insertar un n', UACUTE, 'mero al final');
writeln ('b) Quitar el ', UACUTE, 'ltimo n', UACUTE, 'mero');
writeln ('c) Listar n', UACUTE, 'meros');
writeln ('d) Tama', NTILDE, 'o de la pila');
writeln ('e) Vaciar pila');
writeln ('OPERACIONES COMPLEMENTARIAS');
writeln ('f) Insertar n', UACUTE, 'meros aleatorios');
writeln ('g) Insertar un n', UACUTE, 'mero al principio');
writeln ('h) Consular si contiene un n', UACUTE, 'mero');
writeln ('i) Quitar un n', UACUTE, 'mero a buscar');
writeln ('j) Quitar el primer n', UACUTE, 'mero');
writeln ('k) Ordenar pila');
writeln ('l) Ivertir pila');
writeln ('m) Salir'#10#13);
write ('Seleccione una opci', OACUTE, 'n: ');
repeat
opcion := upcase (readkey);
until (opcion >= 'A') and (opcion <= 'M');
opcion := chr(ord(opcion) + 32);
if opcion <> 'm' then
begin
writeln (opcion + #10#13);
if pila_empty(cabecera) and (opcion <> 'a') and (opcion <> 'd') and (opcion <> 'f') and (opcion <> 'g') then
writeln ('La pila est', AACUTE, ' vac', IACUTE, 'a.'#10#13)
else
begin
if (opcion <> 'c') and (opcion <> 'd') and (opcion <> 'h') then
begin
writeln ('Pila original:');
pila_print (cabecera);
end;
case opcion of
'a': { Insertar un numero al final }
cabecera := pila_push (cabecera, leer_entero ('el n' + UACUTE + 'mero a insertar'));
'b': { Quitar el ultimo numero }
cabecera := pila_pop (cabecera);
'c': { Listar numeros }
begin
pila_list (cabecera);
writeln;
end;
'd': { Tamano de la pila }
writeln ('Tama', NTILDE, 'o de la pila: ', pila_size (cabecera), '.'#10#13);
'e': { Vaciar pila }
while not pila_empty(cabecera) do
cabecera := pila_pop (cabecera);
'f': { Insertar numeros aleatorios }
begin
dato := leer_entero ('la cantidad de n' + UACUTE + 'meros aleatorios a insertar');
while dato>0 do
begin
cabecera := pila_push (cabecera, random (1000));
dec (dato);
end;
end;
'g': { Insertar un numero al principio }
begin
dato := leer_entero ('el n' + UACUTE + 'mero a insertar');
cabecera := pila_insert_first (cabecera, dato);
end;
'h': { Consular si existe un numero }
begin
dato := leer_entero ('el n' + UACUTE + 'mero a consultar');
if pila_contains (cabecera, dato) then
writeln ('El n', UACUTE, 'mero ', dato, ' s', IACUTE, ' est', AACUTE,
' en la pila.'#10#13)
else
writeln ('El n', UACUTE, 'mero ', dato, ' no est', AACUTE, ' en la pila.'#10#13);
end;
'i': { Quitar un numero a buscar }
cabecera := pila_remove (cabecera, leer_entero ('el n' + UACUTE + 'mero a remove'));
'j': { Quitar el primer numero }
cabecera := pila_remove_first (cabecera);
'k': { Ordenar pila }
cambio := pila_sort (cabecera);
'l': { Ivertir pila }
cabecera := pila_reverse (cabecera, pila_create);
end;
if (opcion <> 'c') and (opcion <> 'd') and (opcion <> 'h') then
begin
writeln ('Pila actualizada:');
pila_print (cabecera);
end;
end;
write ('Presione una tecla para continuar . . . ');
tecla := readkey;
end;
until opcion = 'm';
end.