Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node {
- int entry = 0; // valores por defecto
- node *next = nullptr;
- };
- struct lista {
- node* root = nullptr;
- };
- node* create_node (int a)
- {
- node* nuevo = new node;
- nuevo->entry = a;
- return nuevo;
- }
- void insertar_nodo_en_orden (lista& l, int a)
- {
- node* nuevo = create_node(a);
- node* p = l.root;
- if(l.root == 0)
- {
- l.root = nuevo;
- return;
- }
- if (a <= p->entry)
- {
- nuevo->next = p;
- l.root = nuevo;
- return;
- }
- node* previous = 0;
- while (p != 0 and a > p->entry) //se termina cuando se acaba la lista o encontro un valor menor a a
- {
- previous = p;
- p = p->next;
- }
- nuevo->next = p;
- previous->next = nuevo;
- }
- void insertar_nodo_en_orden_decreciente (lista& l, int a)
- {
- node* nuevo = create_node(a);
- node* p = l.root;
- if(l.root == 0)
- {
- l.root = nuevo;
- return;
- }
- if (a >= p->entry)
- {
- nuevo->next = p;
- l.root = nuevo;
- return;
- }
- node* previous = 0;
- while (p != 0 and a < p->entry) //se termina cuando se acaba la lista o encontro un valor menor a a
- {
- previous = p;
- p = p->next;
- }
- nuevo->next = p;
- previous->next = nuevo;
- }
- int get_size_of_list (lista& a)
- {
- node* p = a.root; int c = 0;
- while (p != 0)
- {
- c++;
- p = p-> next;
- }
- return c;
- }
- void swap_entries (node*& a, node*& b)
- {
- int aux = a->entry;
- a->entry = b->entry;
- b->entry = aux;
- }
- void ordenar_lista (lista& a)
- {
- node* p = a.root;
- node* q = a.root;
- while (p != 0)
- {
- q = q->next;
- while (q != 0)
- {
- if (p->entry > q->entry)
- swap_entries (p,q);
- q = q->next;
- }
- p = p->next;
- q = p;
- }
- }
- bool is_list_sorted (lista& a)
- {
- node *p = a.root;
- int aux = p->entry;
- p = p->next;
- while (p!=0)
- {
- if(p->entry < aux)
- return false;
- aux = p->entry;
- p = p->next;
- }
- return true;
- }
- bool is_vacia (lista &l) // funciona
- {
- return l.root == 0;
- }
- void mostrar_lista (lista& l) // funciona
- {
- if(not is_vacia(l))
- {
- for (node *p = l.root; p != 0; p = p->next)
- cout << p->entry << ' ';
- cout << endl;
- }
- else
- cout << "Lista vacia." << endl;
- }
- void mostrar_vacia (lista& l) // funciona
- {
- if (is_vacia(l))
- cout << "esta vacia" << endl;
- else
- cout << "no esta vacia" << endl;
- }
- int get_by_index (lista& l, int index) // funciona
- {
- node* p = l.root;
- for (int i=0; i < index; i++)
- {
- p = p->next;
- }
- return p->entry;
- }
- 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 insertar_nodo_en_index(lista& l, int index, int valor) // funciona
- {
- node* nuevo = new node;
- nuevo->entry = valor;
- if (index != 0)
- {
- node* p = l.root;
- for (int i = 0; i < index - 1; i++)
- {
- p = p->next;
- }
- nuevo->next = p->next;
- p->next = nuevo;
- }
- else
- {
- nuevo->next = l.root;
- l.root = nuevo;
- }
- }
- void eliminar_nodo_en_index (lista& l, int index) // funciona
- {
- node* p = l.root;
- if (index == -1)
- return;
- if (index != 0)
- {
- node* q = l.root;
- for (int i = 0; i < index - 1; i++)
- {
- p = p->next;
- }
- q = p->next->next;
- delete p->next;
- p->next = q;
- }
- else
- {
- l.root = p->next;
- delete p;
- }
- }
- void eliminar_lista (lista& l) // funciona
- {
- while (l.root != 0)
- eliminar_nodo_en_index(l,0);
- }
- int buscar_elemento (lista& l, int e) // funciona
- {
- int index = 0;
- node* p = l.root;
- while (p->entry != e and p->next != 0) // p->next == 0 => es el ultimo
- {
- p = p->next;
- index++;
- }
- if (p->entry == e)
- return index;
- else
- return -1;
- }
- void eliminar_nodo_segun_entry (lista& l, int a)
- {
- int i = buscar_elemento(l,a);
- eliminar_nodo_en_index(l,i);
- }
- void buscar_o_insertar (lista& l, node*& ptr, int valor)
- {
- node* p = l.root;
- node* previous;
- while (p != 0 and p->entry != valor)
- {
- previous = p;
- p = p->next;
- }
- if (p == 0)
- {
- node* nuevo = create_node(valor);
- previous->next = nuevo;
- ptr = nuevo;
- }
- else if (p->entry == valor)
- ptr = p;
- }
- void insertar_despues (lista& l, int nodo, int valor)
- {
- int index = buscar_elemento(l, nodo);
- if (index != -1)
- insertar_nodo_en_index(l,index+1,valor);
- }
- void insertar_antes (lista& l, int nodo, int valor)
- {
- int index = buscar_elemento(l, nodo);
- if (index != -1)
- insertar_nodo_en_index(l,index,valor);
- }
- void insertar_primero (lista& l, int valor)
- {
- node* p = l.root;
- node* nuevo = create_node(valor);
- nuevo->next = p;
- l.root = nuevo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement