|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +func main() { |
| 6 | + var lifo sliceStack |
| 7 | + var fifo sliceQueue |
| 8 | + |
| 9 | + for { |
| 10 | + var opt uint8 |
| 11 | + fmt.Println("Selecciona uan opcion:") |
| 12 | + fmt.Println("1. Ejecuta las funciones de Pilas.") |
| 13 | + fmt.Println("2. Ejecuta las funciones de Colas.") |
| 14 | + fmt.Println("3. Navegador.") |
| 15 | + fmt.Println("4. Impresora.") |
| 16 | + fmt.Println("5. Salir.") |
| 17 | + fmt.Scan(&opt) |
| 18 | + |
| 19 | + switch opt { |
| 20 | + case 1: |
| 21 | + // LIFO (Last In, First Out). |
| 22 | + lifo.push("MiguelP-Dev") |
| 23 | + lifo.push("master") |
| 24 | + |
| 25 | + fmt.Println(lifo.pop()) |
| 26 | + fmt.Println(lifo.pop()) |
| 27 | + fmt.Println(lifo.pop()) |
| 28 | + |
| 29 | + case 2: |
| 30 | + // FIFO (First In, First Out). |
| 31 | + |
| 32 | + fifo.push("0") |
| 33 | + fifo.push("1") |
| 34 | + fifo.push("2") |
| 35 | + fifo.push("3") |
| 36 | + |
| 37 | + fmt.Println(fifo.pop()) |
| 38 | + fmt.Println(fifo.pop()) |
| 39 | + fmt.Println(fifo.pop()) |
| 40 | + fmt.Println(fifo.pop()) |
| 41 | + fmt.Println(fifo.pop()) |
| 42 | + |
| 43 | + case 3: |
| 44 | + webNavigator(&navigator) |
| 45 | + case 4: |
| 46 | + docPrinter(&printerv) |
| 47 | + case 5: |
| 48 | + return |
| 49 | + default: |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// LIFO (Last In, First Out). |
| 57 | +// sliceStack Es un slice que funcionará como una pila(Stack). |
| 58 | +type sliceStack []string |
| 59 | + |
| 60 | +// push Método para agregar datos a un slice que funciona como pila. |
| 61 | +func (s *sliceStack) push(v string) { |
| 62 | + *s = append(*s, v) |
| 63 | +} |
| 64 | + |
| 65 | +// popSlice Método pop para sacar elementos de uns slice que sirve como pila(Los slices no tienen una función 'Delete', pero son ideales como pilas). |
| 66 | +func (s *sliceStack) pop() string { |
| 67 | + if len(*s) == 0 { |
| 68 | + fmt.Println("La pila está vacía.") |
| 69 | + return "" |
| 70 | + } |
| 71 | + index := len(*s) - 1 |
| 72 | + element := (*s)[index] |
| 73 | + *s = (*s)[:index] |
| 74 | + return element |
| 75 | +} |
| 76 | + |
| 77 | +// FIFO (First In, First Out). |
| 78 | +// sliceQueue Es un slice que funcionará como una cola. |
| 79 | +type sliceQueue []string |
| 80 | + |
| 81 | +// push Método para encolar elementos |
| 82 | +func (q *sliceQueue) push(v string) { |
| 83 | + *q = append(*q, v) |
| 84 | +} |
| 85 | + |
| 86 | +// pop Método para deencolar elementos |
| 87 | +func (q *sliceQueue) pop() string { |
| 88 | + if len(*q) == 0 { |
| 89 | + fmt.Println("La cola está vacía.") |
| 90 | + return "" |
| 91 | + } |
| 92 | + pop := (*q)[0] |
| 93 | + *q = (*q)[1:] |
| 94 | + return pop |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * DIFICULTAD EXTRA (opcional): |
| 99 | + * - Utilizando la implementación de pila y cadenas de texto, simula el mecanismo adelante/atrás |
| 100 | + * de un navegador web. Crea un programa en el que puedas navegar a una página o indicarle |
| 101 | + * que te quieres desplazar adelante o atrás, mostrando en cada caso el nombre de la web. |
| 102 | + * Las palabras "adelante", "atrás" desencadenan esta acción, el resto se interpreta como |
| 103 | + * el nombre de una nueva web. |
| 104 | + */ |
| 105 | +// navStack pila para la función que simula un navegador |
| 106 | +type navStack []string |
| 107 | + |
| 108 | +var navigator navStack |
| 109 | +var position = 0 |
| 110 | + |
| 111 | +func webNavigator(n *navStack) { |
| 112 | + for { |
| 113 | + var option string |
| 114 | + fmt.Printf("\n") |
| 115 | + fmt.Println("adelante", " - ", "Viajar a la página siguiente.") |
| 116 | + fmt.Println("atras", " - ", "Viajar a la página anterior.") |
| 117 | + fmt.Println("salir", " - ", "Salir de la app.") |
| 118 | + fmt.Println("borrar", " - ", "Borra el historial.") |
| 119 | + fmt.Printf("Ingresa una de las opciones: ") |
| 120 | + fmt.Scan(&option) |
| 121 | + |
| 122 | + switch option { |
| 123 | + case "salir": |
| 124 | + fmt.Println("Saliendo... Bye!") |
| 125 | + return |
| 126 | + case "adelante": |
| 127 | + if position < len(*n)-1 { |
| 128 | + position++ |
| 129 | + fmt.Println("Viajando a:", (*n)[position]) |
| 130 | + } else { |
| 131 | + fmt.Println("No hay páginas delante.") |
| 132 | + } |
| 133 | + case "atras": |
| 134 | + if position > 0 { |
| 135 | + position-- |
| 136 | + fmt.Println("Viajando a:", (*n)[position]) |
| 137 | + } else { |
| 138 | + fmt.Println("No hay páginas hacia atrás.") |
| 139 | + } |
| 140 | + case "borrar": |
| 141 | + *n = []string{} |
| 142 | + position = 0 |
| 143 | + fmt.Println("Histarial borrado.") |
| 144 | + default: |
| 145 | + if len(*n) == 0 { |
| 146 | + *n = append(*n, option) |
| 147 | + position = 0 |
| 148 | + } else { |
| 149 | + *n = append((*n)[:position+1], option) |
| 150 | + position++ |
| 151 | + } |
| 152 | + fmt.Println("Navegando a:", (*n)[position]) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +type queue []string |
| 159 | + |
| 160 | +var printerv queue |
| 161 | + |
| 162 | +func docPrinter(q *queue) { |
| 163 | + var option string |
| 164 | + fmt.Scan(&option) |
| 165 | + |
| 166 | + switch option { |
| 167 | + case "imprimir": |
| 168 | + if len(*q) == 0 { |
| 169 | + fmt.Println("La cola de impresión está vacía.") |
| 170 | + return |
| 171 | + } |
| 172 | + fmt.Println("Imprimiendo: " + (*q)[0]) |
| 173 | + *q = (*q)[1:] |
| 174 | + case "Salir": |
| 175 | + return |
| 176 | + default: |
| 177 | + *q = append(*q, option) |
| 178 | + fmt.Println("Documento agregado a la cola") |
| 179 | + } |
| 180 | + docPrinter(&printerv) |
| 181 | + |
| 182 | +} |
0 commit comments