Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- int entry = 0; // valores por defecto
- node *next = nullptr;
- };
- struct lista {
- node* root = nullptr;
- };
- void mostrar_lista (lista& l) // funciona
- {
- for (node *p = l.root; p != 0; p = p->next)
- cout << p->entry << ' ';
- cout << endl << endl;
- }
- void insertar_nodo_al_final (lista &l, int a) // funciona
- {
- node* nuevo = new node;
- nuevo->entry = a;
- if (l.root != 0) // apunta a algo?
- {
- node* p = l.root;
- while (p->next != 0)
- p = p->next;
- p->next = nuevo;
- }
- else
- l.root = nuevo;
- }
- void swap_entries (node*& a, node*& b)
- {
- int aux = a->entry;
- a->entry = b->entry;
- b->entry = aux;
- }
- void bubble_sort (lista& a)
- {
- node* p; node* q; bool swapped = true;
- while (swapped)
- {
- swapped = false;
- p = a.root;
- q = a.root;
- q = q->next;
- while (q != 0)
- {
- if (p->entry > q->entry)
- {
- swap_entries (p,q);
- swapped = true;
- }
- p = p->next;
- q = q->next;
- }
- }
- }
- int main()
- {
- lista L;
- for (int i=10; i>0; i--)
- insertar_nodo_al_final (L, (i+3)%10);
- mostrar_lista(L);
- bubble_sort(L);
- mostrar_lista(L);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement