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;
}
#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;
}