1+ /*
2+ * EJERCICIO:
3+ * Explora el concepto de clase y crea un ejemplo que implemente un inicializador,
4+ * atributos y una función que los imprima (teniendo en cuenta las posibilidades
5+ * de tu lenguaje).
6+ * Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos
7+ * utilizando su función.
8+ */
9+
10+ class User {
11+ name : string
12+ age : number
13+ email : string
14+ constructor ( name : string , age : number , email : string ) {
15+ this . name = name
16+ this . age = age
17+ this . email = email
18+ }
19+
20+ print ( ) :void {
21+ console . log ( `Nombre: ${ this . name } , Edad: ${ this . age } , Email: ${ this . email } ` )
22+ }
23+ }
24+
25+ const user1 = new User ( 'Victor' , 21 , '[email protected] ' ) 26+ user1 . print ( )
27+
28+ /*
29+ * DIFICULTAD EXTRA (opcional):
30+ * Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas
31+ * en el ejercicio número 7 de la ruta de estudio)
32+ * - Deben poder inicializarse y disponer de operaciones para añadir, eliminar,
33+ * retornar el número de elementos e imprimir todo su contenido.
34+ */
35+
36+ // Pilas - LIFO
37+
38+ class Stack < T > {
39+ private listStack : T [ ]
40+
41+ constructor ( ) {
42+ this . listStack = [ ]
43+ }
44+
45+ adding ( input : T ) : void {
46+ this . listStack . push ( input )
47+ }
48+
49+ removing ( ) : T | undefined {
50+ return this . listStack . pop ( )
51+ }
52+
53+ numberOfElements ( ) : number {
54+ return this . listStack . length
55+ }
56+
57+ printStackContent ( ) : void {
58+ console . table ( this . listStack )
59+ }
60+ }
61+
62+ const numberStack = new Stack < number > ( )
63+ numberStack . adding ( 1 )
64+ numberStack . adding ( 2 )
65+ numberStack . adding ( 3 )
66+ numberStack . removing ( )
67+ console . log ( numberStack . numberOfElements ( ) )
68+ numberStack . printStackContent ( )
69+
70+ // Colas - FIFO
71+
72+ class Queue < T > {
73+ private listQueue : T [ ]
74+
75+ constructor ( ) {
76+ this . listQueue = [ ]
77+ }
78+ adding ( input : T ) : void {
79+ this . listQueue . push ( input )
80+ }
81+
82+ removing ( ) : T | undefined {
83+ return this . listQueue . shift ( )
84+ }
85+
86+ numberOfElements ( ) : number {
87+ return this . listQueue . length
88+ }
89+
90+ printStackContent ( ) : void {
91+ console . table ( this . listQueue )
92+ }
93+ }
94+
95+ const myQueue : Queue < number > = new Queue ( )
96+ myQueue . adding ( 1 )
97+ myQueue . adding ( 2 )
98+ myQueue . adding ( 3 )
99+ myQueue . removing ( )
100+ console . log ( myQueue . numberOfElements ( ) )
101+ myQueue . printStackContent ( )
0 commit comments