1+ import java .util .Scanner ;
2+
3+ // Implementación de la Pila (Stack - LIFO)
4+ class Stack {
5+ private String [] array ;
6+ private int top ;
7+ private int capacity ;
8+
9+ // Constructor
10+ public Stack (int size ) {
11+ array = new String [size ];
12+ capacity = size ;
13+ top = -1 ;
14+ }
15+
16+ // Método para añadir un elemento a la pila
17+ public void push (String x ) {
18+ if (isFull ()) {
19+ System .out .println ("Error: Pila llena" );
20+ return ;
21+ }
22+ array [++top ] = x ;
23+ }
24+
25+ // Método para quitar un elemento de la pila
26+ public String pop () {
27+ if (isEmpty ()) {
28+ System .out .println ("Error: Pila vacía" );
29+ return null ;
30+ }
31+ return array [top --];
32+ }
33+
34+ // Método para verificar si la pila está vacía
35+ public boolean isEmpty () {
36+ return top == -1 ;
37+ }
38+
39+ // Método para verificar si la pila está llena
40+ public boolean isFull () {
41+ return top == capacity - 1 ;
42+ }
43+
44+ // Método para obtener el elemento en la cima de la pila sin quitarlo
45+ public String peek () {
46+ if (isEmpty ()) {
47+ System .out .println ("Error: Pila vacía" );
48+ return null ;
49+ }
50+ return array [top ];
51+ }
52+ }
53+
54+ // Implementación de la Cola (Queue - FIFO)
55+ class Queue {
56+ private String [] array ;
57+ private int front ;
58+ private int rear ;
59+ private int capacity ;
60+ private int count ;
61+
62+ // Constructor
63+ public Queue (int size ) {
64+ array = new String [size ];
65+ capacity = size ;
66+ front = 0 ;
67+ rear = -1 ;
68+ count = 0 ;
69+ }
70+
71+ // Método para añadir un elemento a la cola
72+ public void enqueue (String x ) {
73+ if (isFull ()) {
74+ System .out .println ("Error: Cola llena" );
75+ return ;
76+ }
77+ rear = (rear + 1 ) % capacity ;
78+ array [rear ] = x ;
79+ count ++;
80+ }
81+
82+ // Método para quitar un elemento de la cola
83+ public String dequeue () {
84+ if (isEmpty ()) {
85+ System .out .println ("Error: Cola vacía" );
86+ return null ;
87+ }
88+ String x = array [front ];
89+ front = (front + 1 ) % capacity ;
90+ count --;
91+ return x ;
92+ }
93+
94+ // Método para verificar si la cola está vacía
95+ public boolean isEmpty () {
96+ return count == 0 ;
97+ }
98+
99+ // Método para verificar si la cola está llena
100+ public boolean isFull () {
101+ return count == capacity ;
102+ }
103+
104+ // Método para obtener el primer elemento de la cola sin quitarlo
105+ public String peek () {
106+ if (isEmpty ()) {
107+ System .out .println ("Error: Cola vacía" );
108+ return null ;
109+ }
110+ return array [front ];
111+ }
112+ }
113+
114+ // Clase principal que contiene los programas de navegador web e impresora
115+ public class eulogioep {
116+ public static void main (String [] args ) {
117+ Scanner scanner = new Scanner (System .in );
118+
119+ // Programa de navegador web
120+ Stack backStack = new Stack (100 );
121+ Stack forwardStack = new Stack (100 );
122+ String currentPage = "" ;
123+
124+ System .out .println ("Simulador de navegador web (escribe 'salir' para terminar):" );
125+ while (true ) {
126+ System .out .print ("Ingresa una acción o nombre de página web: " );
127+ String input = scanner .nextLine ().trim ().toLowerCase ();
128+
129+ if (input .equals ("salir" )) {
130+ break ;
131+ } else if (input .equals ("atrás" )) {
132+ if (!backStack .isEmpty ()) {
133+ forwardStack .push (currentPage );
134+ currentPage = backStack .pop ();
135+ System .out .println ("Página actual: " + currentPage );
136+ } else {
137+ System .out .println ("No hay páginas anteriores" );
138+ }
139+ } else if (input .equals ("adelante" )) {
140+ if (!forwardStack .isEmpty ()) {
141+ backStack .push (currentPage );
142+ currentPage = forwardStack .pop ();
143+ System .out .println ("Página actual: " + currentPage );
144+ } else {
145+ System .out .println ("No hay páginas siguientes" );
146+ }
147+ } else {
148+ if (!currentPage .isEmpty ()) {
149+ backStack .push (currentPage );
150+ }
151+ forwardStack = new Stack (100 ); // Limpia la pila de adelante
152+ currentPage = input ;
153+ System .out .println ("Página actual: " + currentPage );
154+ }
155+ }
156+
157+ // Programa de impresora compartida
158+ Queue printQueue = new Queue (100 );
159+
160+ System .out .println ("\n Simulador de impresora compartida (escribe 'salir' para terminar):" );
161+ while (true ) {
162+ System .out .print ("Ingresa un nombre de documento o 'imprimir': " );
163+ String input = scanner .nextLine ().trim ().toLowerCase ();
164+
165+ if (input .equals ("salir" )) {
166+ break ;
167+ } else if (input .equals ("imprimir" )) {
168+ String document = printQueue .dequeue ();
169+ if (document != null ) {
170+ System .out .println ("Imprimiendo: " + document );
171+ }
172+ } else {
173+ printQueue .enqueue (input );
174+ System .out .println ("Documento añadido a la cola: " + input );
175+ }
176+ }
177+
178+ scanner .close ();
179+ }
180+ }
0 commit comments