Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TDALista.h
- #ifndef TDALISTA_H_INCLUDED
- #define TDALISTA_H_INCLUDED
- struct Nodo;
- typedef struct Nodo tNodo;
- struct Lista {
- tNodo* ppio; //puntero al primer nodo de la lista
- tNodo* fin; //puntero al último nodo de la lista
- int cant; // cantidad de elementos de la lista
- int (*comparador)(void*,void*);
- };
- typedef struct Lista tListaOrdenada;
- tListaOrdenada crearLista(int (*f)(void*,void*));
- void insertar(tListaOrdenada* L, void* x);
- tNodo* siguiente(tNodo* pos);
- tNodo* primera(tListaOrdenada L);
- tNodo* ultima(tListaOrdenada L);
- void* elemento(tNodo* pos);
- int listaVacia(tListaOrdenada L);
- #endif // TDALISTA_H_INCLUDED
- /////////////////////////
- TDALista.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "TDALista.h"
- struct Nodo {
- void * elem;
- struct Nodo* next;
- };
- tListaOrdenada crearLista(int (*f)(void*,void*)){
- tListaOrdenada Lista;
- Lista.ppio=NULL;
- Lista.fin=NULL;
- Lista.cant=0;
- Lista.comparador = f;
- return Lista;
- }
- void insertar(tListaOrdenada* L, void* x) {
- tNodo* nuevoNodo = (tNodo*) malloc(sizeof(tNodo));
- nuevoNodo->elem = x;
- nuevoNodo->next= NULL;
- tNodo* aux= L->ppio;
- if (aux==NULL) {
- L->ppio = nuevoNodo;
- L->fin = nuevoNodo;
- L->cant = 1;
- nuevoNodo->next= NULL;
- }
- else {
- if (L->cant==1){
- if (L->comparador(nuevoNodo->elem, aux->elem < 0)){
- nuevoNodo->next=aux;
- L->ppio=nuevoNodo;
- L->cant+=1;
- }
- else{
- aux->next=nuevoNodo;
- L->fin=nuevoNodo;
- L->cant+=1;
- }
- }
- else{
- int encontre=0;
- while ((aux!=NULL)&&!encontre) {
- if (aux->next!=NULL){
- if (L->comparador(nuevoNodo->elem, aux->next->elem < 0)) {
- nuevoNodo->next=aux->next;
- aux->next=nuevoNodo;
- L->cant+=1;
- encontre=1;
- }
- else {
- aux=aux->next;
- }
- }
- }
- if (!encontre) {
- aux->next=nuevoNodo;
- L->fin=nuevoNodo;
- L->cant+=1;
- }
- }
- }
- }
- tNodo* siguiente(tNodo* pos) {
- if (pos==NULL) return NULL;
- else return pos->next;
- }
- tNodo* primera(tListaOrdenada L) {
- return L.ppio;
- }
- tNodo* ultima(tListaOrdenada L) {
- return L.fin;
- }
- void* elemento(tNodo* pos) {
- return pos->elem;
- }
- int listaVacia(tListaOrdenada L) {
- if (L.cant==0) return 1;
- else return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement