Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: neo_angel_xxx en 27 Octubre 2010, 18:50 pm



Título: AYUDA C#!!! COMO ORDENAR UAN LISTA
Publicado por: neo_angel_xxx en 27 Octubre 2010, 18:50 pm
POZ TENGO UN TRABAJO PARA LA UNIVERSIDAD DE HACER UN MENU CON ALGUNOS METODOS TALES COMO ORDENAR LISTAR ELIMINAR BUSCAR POSICION Y ORDENAR UNA LISTA... YA LOS HICE TODOS PERO NOCE COMO HACER EL ULTIMO QUE ES ORDENAR HMM... ES UNA LISTA SIMPLE (ENLAZADA).. AVER SI ME PUEDEN AYDUAR PORFAVRO CON UN MODULITO PARA ORDENAR... LES PASO HASTA EL MOMENTO MI PROGRAMA(CONSOLA)^^,!!
1....Cree un nuevo proyecto Biblioteca de Clases en Visual C#.Net con el nombre de “Clase_Lista” :
 Agregue una clase con el nombre “CLista.cs” con el siguiente código dentro de la clase.

namespace CLASE_LISTA
{
    public class cLista
    {
        //---atributo
        private CNodo aPrimerNodo;
        private CNodo aUltimoNodo;
        private string dato;
      // referencia a la cabeza de la lista
        //---constructor: construye una lista vacia
        public cLista()
        {
            aPrimerNodo = null;
            aUltimoNodo = null;
           
        } //fin del constructor
        //Propiedad
        public CNodo PrimerNodo
        {
            get
            {
                return aPrimerNodo;
            }
            set
            {
                aPrimerNodo = value;
            }
        }
        public CNodo UltimoNodo
        {
            get
            {
                return aUltimoNodo;
            }
            set
            {
                aUltimoNodo = value;
            }
        }
        //Metodos
        //---determina si la lista esta vacia
        public bool estaVacia()
        {
                         
            return aPrimerNodo == null;
           
        }
        public void InsertarP(string item, int pos)
        {
            CNodo aux = new CNodo();
            aux.Elemento = item;
            aux.SgteNodo = null;
            if (PrimerNodo == null)
            {
                Console.WriteLine(" LISTA VACIA,SE INSERTA EN LA 1ºPOSICION");
                PrimerNodo = aux;
            }
            else
            {
                CNodo puntero;
                puntero = PrimerNodo;
                if (pos == 1)
                {
                    PrimerNodo = aux;
                    aux.SgteNodo = puntero;
                }
                else
                {
                    for (int i = 1; i < pos - 1; i++)
                    {
                        puntero = puntero.SgteNodo;
                        if (puntero.SgteNodo == null)
                            break;
                    }
                    CNodo punteronext;
                    punteronext = puntero.SgteNodo;
                    puntero.SgteNodo = aux;
                    aux.SgteNodo = punteronext;
                }
            }
        }
        //insertar final de la lista
        public void agregarf(string item)
        {
            CNodo aux = new CNodo();
            aux.Elemento = item;
            aux.SgteNodo = null;
            if (PrimerNodo == null)
                PrimerNodo = aux;
            else
            {
                CNodo puntero;
                puntero = PrimerNodo;
                while (puntero.SgteNodo != null)
                {
                    puntero = puntero.SgteNodo;
                }
                puntero.SgteNodo = aux;
            }
        }
        public int longitud()
        {
            CNodo nodoAux; //nodo auxiliar
            int i = 0; //contador de nodos
            nodoAux = aPrimerNodo;
            while (nodoAux != null)
            {
                i++;
                nodoAux = nodoAux.SgteNodo;
            }
            //---devuelve la longitud de la lista
            return i;
        } // fin del metodo longitud
        //---agrega un elemento al frente de la lista
        public void agregari(string elemento)
        {
            aPrimerNodo = new CNodo(elemento, aPrimerNodo);
        }
        //Eliminar inicio de la lista
        public void eliminarI()
        {
            if (PrimerNodo == null)
                Console.WriteLine("Lista vacía, no se puede eliminar");
            else
                PrimerNodo = PrimerNodo.SgteNodo;
        }
        //Eliminar final de la lista
        public void eliminarF()
        {
            if (PrimerNodo == null)
                Console.WriteLine("Lista vacía, no se puede eliminar");
            else
                if (PrimerNodo.SgteNodo == null)
                    PrimerNodo = null;
                else
                {
                    CNodo punteroant, punteronext;
                    punteroant = PrimerNodo;
                    punteronext = PrimerNodo;
                    while (punteronext.SgteNodo != null)
                    {
                        punteroant = punteronext;
                        punteronext = punteronext.SgteNodo;
                    }
                    punteroant.SgteNodo = null;
                }
        }
 
       
        public Object contenido(CNodo ubicacion)
        {
            return ubicacion.Elemento;
        }// fin de contenido
        public CNodo acceso(int i)
        {
            CNodo nodoAux; //nodo auxiliar
            nodoAux = null;
            if (!(estaVacia()) && (i <= longitud()))
            { //---la lista no es vacia
                //---nodoAux toma el primer nodo de la lista
                nodoAux = PrimerNodo;
                //---ubicar n-esimo nodo
                while (i > 1)
                {
                    --i;
                    nodoAux = nodoAux.SgteNodo;
                }
            }

            return nodoAux; //valor que retorna el metodo
        }//fin del metodo acceso
        public Object iesimo(int i)
        {
            return contenido(acceso(i));
        } //fin de iesimo
        public void listar()
        {
            if (longitud() == 0)
                Console.WriteLine("lista vacia");
            else
            {
                int i;
                //---imprimir elementos de la lista
                for (i = 1; i <= longitud(); i++)
                {
                    Console.WriteLine(iesimo(i));
                }
            }
        }// fin de listar
     
       
        }

2.... Agregue una clase con el nombre “CNodo.cs” con el siguiente código dentro de la clase.

namespace CLASE_LISTA
{
    public class CNodo
    {
        //---atributos
        private string dato;
        private CNodo aSgteNodo;
   
        //---constructor: crea un nodo que contiene elemento
        public CNodo()
        {
            dato = null;
            aSgteNodo = null;
         
        }
        public CNodo(string pElemento, CNodo pSgteNodo)
        {
            dato= pElemento;
            aSgteNodo = pSgteNodo;
       
        }
        //Propiedades
        public string Elemento
        {
            get
            {
                return dato;
            }
            set
            {
                dato = value;
            }
        }
        //---metodo retorna siguiente nodo
        public CNodo SgteNodo
        {
            get
            {
                return aSgteNodo;
            }
            set
            {
                aSgteNodo = value;
            }
        }
       
    }
3............ Cree un nuevo proyecto de Aplicación de Consola, agregue una Referencia a su proyecto (Clase_Lista) y agregue el siguiente código dentro de la clase Program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLASE_LISTA;

namespace listas1._.codificacion
{
    class Program
    {

        static void Menu()
        {
            Console.WriteLine("M E N U");
            Console.WriteLine("==================================");
            Console.WriteLine("1. Agregar al inicio.");
            Console.WriteLine("2.Agregar al final");
            Console.WriteLine("3. Mostrar lista.");
            Console.WriteLine("4.ingresar elemento en una posicion determinada");
            Console.WriteLine("5.Borrar del final");
            Console.WriteLine("6.Borrar del inicio");
            Console.WriteLine("7. Salir");
            Console.WriteLine("ingrese una opcion");
            Console.WriteLine("==================================");
        }
        static void Pausa()
        {
            Console.WriteLine("\nPulse [ENTER] para continuar ingresando otra opcion...");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            cLista OLista = new cLista();
            //---crear los objetos que contendra la lista
            int opcion = 0;
         
            while (opcion != 7)
            {
                Console.Clear();
                Menu();

                opcion = int.Parse(Console.ReadLine());

                switch (opcion)
                {
                    case 1:
                        Console.WriteLine("ingrese elemento:");
                        string el = Console.ReadLine();
                        OLista.agregari(el);
                        Pausa();
                        break;
                    case 2:
                        Console.WriteLine("ingrese elemento:");
                        string ele = Console.ReadLine();
                        OLista.agregarf(ele);
                        Pausa();
                        break;
                    case 3:
                        Console.WriteLine("la lista es:");
                        OLista.listar();
                        Console.WriteLine("longitud: " + OLista.longitud());
                                           
                        Pausa();
                        break;
                    case 4:
                        Console.WriteLine("ingrese la posicion en la que desea ingresar elemento");
                        int posi = int.Parse(Console.ReadLine());
                        Console.WriteLine("ingrese el elemento a ingresar");
                        string ele1 = Console.ReadLine();
                        OLista.InsertarP(ele1,posi);
                        Pausa();
                        break;
                    case 5:
                        Console.WriteLine("Eliminando de la posicion final");
                        OLista.eliminarF();
                        Pausa();
                        break;
                    case 6:
                        Console.WriteLine("eliminando de la primera posicion");
                        OLista.eliminarI();
                        Pausa();
                        break;
                 
                }
            }
}
}


«-------------AYUDA PLEASe!!!!!!!!!!1


Título: Re: AYUDA C#!!! COMO ORDENAR UAN LISTA
Publicado por: Khronos14 en 27 Octubre 2010, 19:42 pm
C# al igual que Java tiene clases para todo, es programación para tontos, todo te viene hecho e implementado. Los arrays y listas tienen un método llamado Sort() que te ordena los elementos.

De todas formas, no es muy difícil implementar un algoritmo de ordenación: tienes el método de la burbuja, el quicksort, el shell, etc..

PD: Léete las normas y utiliza Geshi para resaltar el código.

Saludos.


Título: AYUDA C#!!! COMO ORDENAR UAN LISTA
Publicado por: [D4N93R] en 27 Octubre 2010, 19:43 pm
El mensaje 'AYUDA C#!!! COMO ORDENAR UAN LISTA (http://foro.elhacker.net/index.php?topic=308994)' fue bloqueado
No se hacen tareas, leer las normas.
Leer reglas:
http://foro.elhacker.net/reglas