Lista GenericaEjemplo de una Lista Generica en Visual Basic.NET 2005Fecha: 13/Dic/2005 (09/12/2005)
|
Este ejemplo es muy sencillo consta de cuatro Clases, Cliente, Principal, Lista y Nodo.
La idea de este ejemplo es poder insertar en forma ordenada o no, cualquier tipo de Objetos que sea comparable, para que se pueda tener una lista ordenada.
La Clase Cliente cuenta con atributos y métodos referidos al cliente. Esta clase implementa IComparable para redefinir el método CompareTo, de acuerdo al atributo que usemos para comparar. Esta comparación se realiza en el método insertarOrdenado de la Clase Lista.
Clase Cliente
Public Class Cliente Implements IComparable 'Metodo estatico para incrementar el Id del Cliente 'a medida que se crean instancias de él Private Shared id As Integer Private clienteId As Integer Private telefono As Long Public Sub New(ByVal telefono As Long) id += 1 Me.clienteId = id Me.telefono = telefono End Sub Public Sub New(ByVal id As Integer) Me.clienteId = id End Sub Public Function getId() As Integer Return Me.clienteId End Function Public Function getTelefono() As Long Return Me.telefono End Function Public Function setTelefono(ByVal telefono As Long) As Boolean Me.telefono = telefono End Function 'Metodo CompareTo que compara por el Id del Cliente 'se utiliza para el Metodo insertarOrdenado de la Lista Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim c As Cliente = CType(obj, Cliente) If c.getId = Me.getId Then Return 0 ElseIf c.getId > Me.getId Then Return 1 Else Return -1 End If End Function Public Overrides Function toString() As String Return "Cliente Numero: " & Me.clienteId & ", Telefono: " & Me.telefono End Function End ClassClase Lista
Public Class Lista Private frente As Nodo Public Sub New() frente = Nothing End Sub Public Function getFrente() As Nodo Return Me.frente End Function Public Sub setFrente(ByVal frente As Nodo) Me.frente = frente End Sub 'Metodo que inserta un nuevo cliente en la Lista 'siempre al frente sin importar el orden Public Sub insertar(ByVal comparable As IComparable) If comparable IsNot Nothing Then Dim nodoNuevo As New Nodo(comparable, frente) frente = nodoNuevo End If End Sub 'Metodo que inserta un nuevo cliente en la Lista 'en forma Ordenada, de acuerdo a lo que se haya 'especificado en el Metodo CompareTo Public Sub insertarOrdenado(ByVal comparable As IComparable) If comparable IsNot Nothing Then Dim p As Nodo = frente Dim q As Nodo = Nothing While p IsNot Nothing AndAlso comparable.CompareTo(p.getInfo) <= 0 q = p p = p.getNodoNext End While Dim nodoNuevo As New Nodo(comparable, p) If q IsNot Nothing Then q.setNodoNet(nodoNuevo) Else frente = nodoNuevo End If End If End Sub 'Metodo que borra el primer elemento de la Lista 'Retornandolo Public Function borrar() As IComparable Dim p As IComparable = Nothing If frente IsNot Nothing Then p = frente.getInfo frente = frente.getNodoNext End If Return p End Function 'Metodo que busca un elemento de la Lista Public Function buscar(ByVal comparableBuscar As IComparable) As Nodo Dim p As Nodo = Nothing If comparableBuscar IsNot Nothing Then If frente IsNot Nothing And comparableBuscar IsNot frente.getInfo Then Return p End If p = frente While p IsNot Nothing Dim i As IComparable = p.getInfo If i.CompareTo(comparableBuscar) = 0 Then Exit While End If p = p.getNodoNext End While End If Return p End Function 'Metodo que un elemento especifico de la Lista Public Sub borrar(ByVal comparableBorrar As IComparable) If comparableBorrar IsNot Nothing Then If frente IsNot Nothing And comparableBorrar IsNot frente.getInfo Then Return End If Dim p As Nodo = frente Dim q As Nodo = Nothing While p IsNot Nothing And p.getInfo.CompareTo(comparableBorrar) <> 0 q = p p = p.getNodoNext End While If p IsNot Nothing Then If q IsNot Nothing Then q.setNodoNet(p.getNodoNext) Else frente = p.getNodoNext End If End If End If End Sub Public Overrides Function ToString() As String Dim cadena As String = Nothing Dim p As Nodo = frente While p IsNot Nothing cadena += CType(p.getInfo, Object).ToString & Environment.NewLine p = p.getNodoNext End While Return cadena End Function End ClassClase Nodo
Public Class Nodo Private info As IComparable Private nodoNext As Nodo Public Sub New(ByVal info As IComparable, ByVal nodoNext As Nodo) Me.info = info Me.nodoNext = nodoNext End Sub Public Function getInfo() As IComparable Return info End Function Public Sub setInfo(ByVal info As IComparable) Me.info = info End Sub Public Function getNodoNext() As Nodo Return nodoNext End Function Public Sub setNodoNet(ByVal nodoNext As Nodo) Me.nodoNext = nodoNext End Sub End ClassClase Principal
Public Class Principal Public Shared Sub main() 'Creo una instancia de la Clase Lista Dim lista As New Lista 'Creo tres instancia de la Clase Cliente 'lo que equivale a tener tres clientes Dim c1 As New Cliente(4511254) Dim c2 As New Cliente(5844412) Dim c3 As New Cliente(4688552) 'inserto en la Lista los tres Clientes lista.insertar(c1) lista.insertar(c2) lista.insertar(c3) 'Imprimo en Consola el Resultado de la Insercion Console.WriteLine("Listado de Clientes:") Console.Write(lista.ToString) 'Reseteo la Lista lista.setFrente(Nothing) 'Inserto nuevamente los Clientes en Orden Decresiente lista.insertarOrdenado(c3) lista.insertarOrdenado(c2) lista.insertarOrdenado(c1) 'Imprimo los clientes en forma ordenada Console.WriteLine("Listado de Clientes:") Console.Write(lista.ToString) Console.Read() End Sub End Class
Fichero con el código de ejemplo: Marcos_Ejemplo_de_una_Lista_Generica.zip - 29 KB