1
+ <?php
2
+
3
+ class Pila {
4
+
5
+ private $ pila ;
6
+
7
+ private $ attribute1 ;
8
+ private $ attribute2 ;
9
+
10
+ public function __construct (array $ pila , int $ attribute1 , string $ attribute2 )
11
+ {
12
+ $ this ->pila = $ pila ;
13
+ $ this ->attribute1 = $ attribute1 ;
14
+ $ this ->attribute2 = $ attribute2 ;
15
+ }
16
+
17
+ public function getPila (): array {
18
+ return $ this ->pila ;
19
+ }
20
+
21
+ public function getPilaString (): string {
22
+ return json_encode ($ this ->pila );
23
+ }
24
+
25
+ public function getAttribute1 (): int {
26
+ return $ this ->attribute1 ;
27
+ }
28
+
29
+ public function getAttribute2 (): string {
30
+ return $ this ->attribute2 ;
31
+ }
32
+
33
+ public function setPila (array $ pila ) {
34
+ $ this ->pila = $ pila ;
35
+ }
36
+
37
+ public function setAttribute1 (int $ attribute1 ) {
38
+ $ this ->attribute1 = $ attribute1 ;
39
+ }
40
+
41
+ public function setAttribute2 (string $ attribute2 ) {
42
+ $ this ->attribute2 = $ attribute2 ;
43
+ }
44
+
45
+ public function __toString (): string
46
+ {
47
+ return 'pila: ' . json_encode ($ this ->pila ) .
48
+ ', attribute1: ' . $ this ->attribute1 .
49
+ ', attribute2: ' . $ this ->attribute2 ;
50
+ }
51
+ }
52
+
53
+ $ pila1 = new Pila ([1 , 2 , 3 , 4 , 5 ], 1 , 'world ' );
54
+
55
+ echo $ pila1 ->__toString ().'<br /> ' ;
56
+
57
+ echo $ pila1 ->getPilaString ().'<br /> ' ;
58
+
59
+ echo $ pila1 ->getAttribute1 ().'<br /> ' ;
60
+
61
+ echo $ pila1 ->getAttribute2 ().'<br /> ' ;
62
+
63
+ $ pila1 ->setPila (['hello ' , 'world ' ]);
64
+
65
+ $ pila1 ->setAttribute1 (2 );
66
+
67
+ $ pila1 ->setAttribute2 ('bye ' );
68
+
69
+ echo $ pila1 ->__toString ().'<br /> ' ;
70
+
71
+ echo $ pila1 ->getPilaString ().'<br /> ' ;
72
+
73
+ echo $ pila1 ->getAttribute1 ().'<br /> ' ;
74
+
75
+ echo $ pila1 ->getAttribute2 ().'<br /> ' ;
76
+
77
+ /**
78
+ * Clase para la dificultad extra.
79
+ */
80
+ class PilaOpt {
81
+
82
+ private $ pila ;
83
+
84
+ public function __construct (array $ pila )
85
+ {
86
+ $ this ->pila = $ pila ;
87
+ }
88
+
89
+ public function getPila (): array {
90
+ return $ this ->pila ;
91
+ }
92
+
93
+ public function getPilaString (): string {
94
+ return json_encode ($ this ->pila );
95
+ }
96
+
97
+ public function setPila (array $ pila ) {
98
+ $ this ->pila = $ pila ;
99
+ }
100
+
101
+ /**
102
+ * Método para introducir un elemento en la pila.
103
+ */
104
+ public function push ($ element ) {
105
+ array_push ($ this ->pila , $ element );
106
+ }
107
+
108
+ /**
109
+ * Método para eliminar un elemento de la pila.
110
+ * Último en entrar, primero en salir.
111
+ */
112
+ public function pop () {
113
+ return array_pop ($ this ->pila );
114
+ }
115
+
116
+ /**
117
+ * Método para conseguir la cantidad de elementos.
118
+ */
119
+ public function counter () {
120
+ return count ($ this ->pila );
121
+ }
122
+
123
+ public function __toString (): string
124
+ {
125
+ return 'pila: ' . json_encode ($ this ->pila );
126
+ }
127
+ }
128
+
129
+ $ pilaOpt = new PilaOpt ([1 , 2 , 3 ]);
130
+
131
+ echo 'Mostramos los elementos del array: ' .$ pilaOpt ->getPilaString ().'<br /> ' ;
132
+
133
+ echo 'Mostramos la cantidad de elementos en la pila(3): ' .$ pilaOpt ->counter ();
134
+
135
+ echo 'Añadimos un nuevo elemento al array.<br /> ' ;
136
+
137
+ $ pilaOpt ->push (4 );
138
+
139
+ echo 'Mostramos los elementos del array: ' .$ pilaOpt ->getPilaString ().'<br /> ' ;
140
+
141
+ echo 'Mostramos la cantidad de elementos en la pila(4): ' .$ pilaOpt ->counter ();
142
+
143
+ echo 'Eliminamos el último elemento del array que se introdujo(4): ' .$ pilaOpt ->pop ().'<br /> ' ;
144
+
145
+ echo 'Mostramos los elementos del array: ' .$ pilaOpt ->getPilaString ().'<br /> ' ;
146
+
147
+ echo 'Mostramos la cantidad de elementos en la pila(3): ' .$ pilaOpt ->counter ();
148
+
149
+ echo 'Eliminamos el último elemento del array que se introdujo(3): ' .$ pilaOpt ->pop ().'<br /> ' ;
150
+
151
+ echo 'Mostramos los elementos del array: ' .$ pilaOpt ->getPilaString ().'<br /> ' ;
152
+
153
+ echo 'Mostramos la cantidad de elementos en la pila(2): ' .$ pilaOpt ->counter ();
154
+
155
+ /**
156
+ * Clase para la dificultad extra.
157
+ */
158
+ class ColaOpt {
159
+
160
+ private $ cola ;
161
+
162
+ public function __construct (array $ cola )
163
+ {
164
+ $ this ->cola = $ cola ;
165
+ }
166
+
167
+ public function getCola (): array {
168
+ return $ this ->cola ;
169
+ }
170
+
171
+ public function getcolaString (): string {
172
+ return json_encode ($ this ->cola );
173
+ }
174
+
175
+ public function setCola (array $ cola ) {
176
+ $ this ->cola = $ cola ;
177
+ }
178
+
179
+ /**
180
+ * Método para introducir un elemento en la pila.
181
+ */
182
+ public function push ($ element ) {
183
+ array_push ($ this ->cola , $ element );
184
+ }
185
+
186
+ /**
187
+ * Método para eliminar un elemento de la pila.
188
+ * Último en entrar, primero en salir.
189
+ */
190
+ public function shift () {
191
+ return array_shift ($ this ->cola );
192
+ }
193
+
194
+ public function counter () {
195
+ return count ($ this ->cola );
196
+ }
197
+
198
+ public function __toString (): string
199
+ {
200
+ return 'cola: ' . json_encode ($ this ->cola );
201
+ }
202
+ }
203
+
204
+ $ colaOpt = new ColaOpt ([1 , 2 , 3 ]);
205
+
206
+ echo 'Mostramos los elementos del array: ' .$ colaOpt ->getcolaString ().'<br /> ' ;
207
+
208
+ echo 'Mostramos la cantidad de elementos en la pila(3): ' .$ colaOpt ->counter ();
209
+
210
+ echo 'Añadimos un nuevo elemento al array.<br /> ' ;
211
+
212
+ $ colaOpt ->push (4 );
213
+
214
+ echo 'Mostramos los elementos del array: ' .$ colaOpt ->getcolaString ().'<br /> ' ;
215
+
216
+ echo 'Mostramos la cantidad de elementos en la pila(4): ' .$ colaOpt ->counter ();
217
+
218
+ echo 'Eliminamos el primer elemento del array que se introdujo(1): ' .$ colaOpt ->shift ().'<br /> ' ;
219
+
220
+ echo 'Mostramos los elementos del array: ' .$ colaOpt ->getcolaString ().'<br /> ' ;
221
+
222
+ echo 'Mostramos la cantidad de elementos en la pila(3): ' .$ colaOpt ->counter ();
223
+
224
+ echo 'Eliminamos el segundo elemento del array que se introdujo(2): ' .$ colaOpt ->shift ().'<br /> ' ;
225
+
226
+ echo 'Mostramos los elementos del array: ' .$ colaOpt ->getcolaString ().'<br /> ' ;
227
+
228
+ echo 'Mostramos la cantidad de elementos en la pila(2): ' .$ colaOpt ->counter ();
229
+
230
+ ?>
0 commit comments