Last active
July 17, 2019 02:41
-
-
Save MrDave1999/dd9e7c734ceffe07e9c591149e780123 to your computer and use it in GitHub Desktop.
Colecciones genéricas en C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class ListP | |
{ | |
private int data; | |
public int getData() { return data; } | |
public void setData(int data) | |
{ | |
this.data = data; | |
} | |
public void Register(ListP obj, List<ListP> list) | |
{ | |
Console.Write("Ingrese un dato:\n"); | |
setData(Convert.ToInt32(Console.ReadLine())); | |
list.Add(obj); | |
} | |
public void Delete(List<ListP> list, int data) | |
{ | |
Console.Write("\n-> Elemento {0} eliminado.\n", (list.RemoveAll(x => x.getData() == data) != 0) ? ("SI") : ("NO")); | |
} | |
public void Clearall(List<ListP> list) | |
{ | |
list.Clear(); | |
Console.Write("-> Todos los elementos de la lista fue eliminado.\n"); | |
} | |
public void Search(List<ListP> list, int data) | |
{ | |
Console.Write("-> Elemento {0} encontrado", (list.Find(x => x.getData() == data) != null) ? ("SI") : ("NO")); | |
} | |
public void Update(List<ListP> list, int data) | |
{ | |
int newdata; | |
ListP find = list.Find(x => x.getData() == data); | |
if (find != null) | |
{ | |
Console.Write("\nIngrese el nuevo elemento a modificar:\n"); | |
newdata = Convert.ToInt32(Console.ReadLine()); | |
find.setData(newdata); | |
Console.Write("\n-> Elemento modificado.\n"); | |
} | |
else | |
Console.Write("Error: Elemento no se pudo modificar porque no esta en la lista\n"); | |
} | |
public void ShowData(List<ListP> list) | |
{ | |
Console.Write("\nElementos:\n\n"); | |
foreach(ListP aList in list) | |
Console.Write("{0}\n", aList.getData()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class Program | |
{ | |
static void Main() | |
{ | |
int data; | |
List<ListP> list = new List<ListP>(); | |
ListP obj = null; | |
int option; | |
do | |
{ | |
Console.Write("1. Registrar elemento\n2. Eliminar elemento\n3. Limpiar la lista\n4. Buscar elemento\n5. Modificar elemento\n6. Mostrar elementos\n7. Salir\n"); | |
Console.Write("Ingrese una opcion: <1-7>\n"); | |
option = Convert.ToInt32(Console.ReadLine()); | |
if (!(option >= 1 && option <= 7)) | |
{ | |
Console.Write("\nError: Ingrese una opcion valida: <1-7>"); | |
continue; | |
} | |
if ((option >= 2 && option <= 6) && (list.Count == 0)) | |
{ | |
Console.Write("\nError: La lista esta vacia\n"); | |
continue; | |
} | |
switch(option) | |
{ | |
case 1: | |
obj = new ListP(); | |
obj.Register(obj, list); | |
break; | |
case 2: | |
Console.Write("Ingrese el dato a eliminar:\n"); | |
data = Convert.ToInt32(Console.ReadLine()); | |
obj.Delete(list, data); | |
break; | |
case 3: | |
obj.Clearall(list); | |
break; | |
case 4: | |
Console.Write("Ingrese el dato a buscar:\n"); | |
data = Convert.ToInt32(Console.ReadLine()); | |
obj.Search(list, data); | |
break; | |
case 5: | |
Console.Write("Ingrese el dato a modificar:\n"); | |
data = Convert.ToInt32(Console.ReadLine()); | |
obj.Update(list, data); | |
break; | |
case 6: | |
obj.ShowData(list); | |
break; | |
case 7: | |
break; | |
} | |
if(option != 7) | |
{ | |
Console.Write("\n-> Presione enter para continuar...\n"); | |
Console.ReadLine(); | |
} | |
} while (option != 7); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment