-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.php
134 lines (111 loc) · 3.13 KB
/
solution.php
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
<?php
class Stack {
private $items;
public function __construct() {
$this->items = [];
}
public function push($element) {
array_push($this->items, $element);
}
public function pop() {
if ($this->isEmpty()) {
return 'La pila está vacía';
}
return array_pop($this->items);
}
public function isEmpty() {
return empty($this->items);
}
public function peek() {
return end($this->items);
}
public function clear() {
$this->items = [];
}
public function size() {
return count($this->items);
}
}
class Queue {
private $items;
public function __construct() {
$this->items = [];
}
public function enqueue($element) {
array_push($this->items, $element);
}
public function dequeue() {
if ($this->isEmpty()) {
return 'La cola está vacía';
}
return array_shift($this->items);
}
public function isEmpty() {
return empty($this->items);
}
public function size() {
return count($this->items);
}
}
$pila = new Stack();
$pila->push(1);
$pila->push(2);
$pila->push(3);
echo $pila->pop() . PHP_EOL; // 3
echo $pila->pop() . PHP_EOL; // 2
$cola = new Queue();
$cola->enqueue('a');
$cola->enqueue('b');
$cola->enqueue('c');
echo $cola->dequeue() . PHP_EOL; // a
echo $cola->dequeue() . PHP_EOL; // b
class Navegador {
private $history;
private $future;
private $currentPage;
public function __construct() {
$this->history = new Stack();
$this->future = new Stack();
$this->currentPage = null;
}
// navegar a una nueva página
public function goToPage($page) {
if ($this->currentPage !== null) {
$this->history->push($this->currentPage);
}
$this->currentPage = $page;
$this->future->clear();
echo 'Página actual: ' . $this->currentPage . PHP_EOL;
}
// retroceder a la página anterior
public function goBack() {
if ($this->history->isEmpty()) {
echo 'No hay páginas anteriores' . PHP_EOL;
return;
}
$this->future->push($this->currentPage);
$this->currentPage = $this->history->pop();
echo 'Página actual: ' . $this->currentPage . PHP_EOL;
}
// avanzar a la página siguiente
public function goForward() {
if ($this->future->isEmpty()) {
echo 'No hay páginas siguientes' . PHP_EOL;
return;
}
$this->history->push($this->currentPage);
$this->currentPage = $this->future->pop();
echo 'Página actual: ' . $this->currentPage . PHP_EOL;
}
}
$navegador = new Navegador();
$navegador->goToPage('google.com');
$navegador->goToPage('facebook.com');
$navegador->goToPage('twitter.com');
$navegador->goBack(); // regresa a facebook.com
$navegador->goBack(); // regresa a google.com
$navegador->goForward(); // avanza a facebook.com
$navegador->goToPage('instagram.com'); // instagram.com
$navegador->goBack(); // regresa a facebook.com
$navegador->goForward(); // avanza a instagram.com
?>