elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Lista doblemente enlazada ordenada por método de selección
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Lista doblemente enlazada ordenada por método de selección  (Leído 4,580 veces)
Abril7

Desconectado Desconectado

Mensajes: 22


Ver Perfil
Lista doblemente enlazada ordenada por método de selección
« en: 6 Abril 2017, 05:54 am »

Hola, tengo que hacer una lista doblemente enlazada por el método de selección, para prácticar haré así por los 4 métodos pero aun tengo muchas dudas de como hacerlo. En internet no encontré mucha información de como hacerlo, de lo único que medio encontré fue del método burbuja y con modificaciones lo logré hacer funcionar. Ahora estoy con el método de selección, hicé un intento pero no funciona, no me marca error, ni deja de funcionar, simplemente se queda en la consola y no hace nada. Debo decir que tenía estos métodos implementados para arreglos y estuvé intentando convertirlos para mi lista doblemente enlazada. Por lo mismo hay unas lineas de código comentadas para guiarme.

Sería de muchisima ayuda si alguien me explica lo que hago mal en el código, que puedo leer para aprender más del tema o alguna sugerencia para mejorarlo. Muchas gracias de antemano.  :)

Aquí esta mi código:
(Lo programo en inglés porque me ayuda a practicar)

Código:
#include <iostream>
#include <stdlib.h>
using namespace std;

class Node{
    public:
        int element;
        Node *next;
        Node *prev;
        Node(int element){this->element=element; next=NULL; prev=NULL;}
};

class doubleList{
   private:
    Node *first;
    Node *last;
   public:
    doubleList(){first=last=NULL;}
    void addFirst(int element);
    void remove(int p);
    void show();
    int size();
    bool isEmpty();
    void orderBubble();
    void orderSelection();
    void orderInsertion();
    void orderQuickSort();
};

void doubleList::orderBubble()
{
    Node *p = first;
    while(p != NULL){
        Node *j = p ->next;
        while(j != NULL){
            if(p->element > j->element){
                int aux = j->element;
                j->element = p->element;
                p->element = aux;
            }
            j = j->next;
        }
        p = p->next;
    }
}
void doubleList::orderSelection()
{   int min, aux;
//for(i=0;i<data;i++)
    Node *p = first;
        while(p != NULL){
            min = p->element;
    Node *j = p ->next;
//for(j=i+1;j<data;j++)
while(j !=NULL){
//if(a[j] < a[min])
            if(j->element < min){
min = j->element;
}
}
aux = p->element;
p->element = min;
min = aux;
}

show();
}
void doubleList::addFirst(int element)
{
    Node *aux = new Node(element);

    if(isEmpty()){
        first=last=aux;
        cout<<"\tFirs element '"<<aux->element<<"' was inserted successfully."<<endl;
        cout<<endl;
    }
    else{
        aux->next=first;
        first->prev=aux;
        first=aux;
        cout<<"\tElement '"<<aux->element<<"' was inserted successfully."<<endl;
        cout<<endl;
    }
}
void doubleList::remove(int p)
{
    Node *aux;

    if(p == 1)
    {
        aux = first;
        first = first->next;
        delete aux;
    }
    else if(p == size())
    {
        aux = last;
        last = last->prev;
        delete aux;
    }
    else if (p > 1 || p < size())
    {
        aux = first;
        for(int i=1;i<p;i++)
        {
            aux = aux->next;
        }
        aux->prev->next = aux->next;
        last->prev = aux->prev;
        delete aux;
    }
    else{
        cout<<"Error: invalid position"<<endl;
    }

}

void doubleList::show()
{
    if(isEmpty())
    {
        cout<<"List empty."<<endl;
    }
    else
    {
        Node *aux = first;
        while(aux != NULL)
        {
            cout<<aux->element<<" ";
            aux = aux->next;
        }
    }
    cout<<endl;
}
int doubleList::size()
{
    Node *aux;
    aux = last;
    int c = 0;
    while(aux != NULL)
    {
        aux = aux->prev;
        c++;
    }
}
bool doubleList::isEmpty()
{
    return(first == NULL && last==NULL)? true : false;
}
//__________________________________________________________________________________

int main ()
{
    doubleList a;
    int op=0;
    int x=0;
    int name;

    while(x==0){
        cout<<"What do you wanna do?\n \t 1) Add 2) Delete 3) Show 4) Order 5) Exit"<<endl;
        cin>>op;

    switch(op)
    {
        case 1:
            system("cls");
            cout<<"Type a name to add to the list: ";
            cin>>name;
            a.addFirst(name);
            break;
        case 2:
            system("cls");
            int p;
            cout<<"Type the position that you want to remove"<<endl;
            cin>>p;
            a.remove(p);
            break;
        case 3:
            system("cls");
            a.show();
            break;
        case 4:
            system("cls");
            cout<<"Which method do you want to use? "<<endl;
            cout<<"\t1)Bubble \n\t2)Selection \n\t3)Insertion \n\t4)QuickSort"<<endl;
            cin>>op;
                switch(op){
                    case 1:
                        a.orderBubble();
                        a.show();
                        break;
                    case 2:
                        a.orderSelection();
                        a.show();
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                }
            break;
        case 5:
            system("cls");
            cout<<"Program ended..."<<endl;
            x=1;
    }
}
return 0;
}


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
una lista circular doblemente enlazada en c sharp c#
.NET (C#, VB.NET, ASP)
neo_angel_xxx 2 8,970 Último mensaje 29 Octubre 2010, 01:48 am
por [D4N93R]
ayuda...Lista doblemente enlazada
Java
goll9d 2 3,610 Último mensaje 22 Enero 2012, 04:50 am
por goll9d
ayuda con lista doblemente enlazada
Programación C/C++
gibi77 3 3,673 Último mensaje 7 Marzo 2012, 07:47 am
por nirvguy
Ayuda con lista doblemente enlazada
Programación C/C++
falconez 2 8,992 Último mensaje 16 Diciembre 2013, 01:35 am
por falconez
lista doblemente enlazada
Programación C/C++
d91 1 1,905 Último mensaje 19 Octubre 2015, 04:06 am
por d91
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines