gonzalob

Untitled

Oct 17th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include "lista.h"
  2.  
  3. nodo * inicLista()
  4. {
  5.     return NULL;
  6. }
  7.  
  8. nodo * crearNodo(int dato)
  9. {
  10.     nodo * aux = (nodo*)malloc(sizeof(nodo));
  11.     aux->dato = dato;
  12.     aux->siguiente = NULL;
  13.     return aux;
  14. }
  15.  
  16. nodo * agregarPpio(nodo * lista, nodo * nuevoNodo)
  17. {
  18.  
  19.     if(lista == NULL)
  20.     {
  21.         lista = nuevoNodo;
  22.     }
  23.     else
  24.  
  25.     {
  26.         nuevoNodo->siguiente = lista;
  27.         lista = nuevoNodo;
  28.     }
  29.     return lista;
  30. }
  31.  
  32. nodo * buscarUltimo(nodo * lista)
  33. {
  34.     nodo * seg = lista;
  35.     if(seg != NULL)
  36.         while(seg->siguiente != NULL)
  37.         {
  38.             seg = seg->siguiente;
  39.         }
  40.     return seg;
  41. }
  42.  
  43. nodo * buscarNodo(int dato, nodo * lista)
  44.  
  45. {
  46.     nodo * seg;
  47.     seg = lista;
  48.  
  49.     while ((seg != NULL) && (seg->dato!=dato))
  50.     {
  51.         seg=seg->siguiente;
  52.     }
  53.  
  54.     return seg;
  55. }
  56.  
  57. nodo * agregarFinal(nodo * lista, nodo * nuevoNodo)
  58. {
  59.     if(lista == NULL)
  60.     {
  61.         lista = nuevoNodo;
  62.     }
  63.     else
  64.     {
  65.         nodo * ultimo = buscarUltimo(lista);
  66.         ultimo->siguiente = nuevoNodo;
  67.     }
  68.     return lista;
  69. }
  70.  
  71. nodo * borrarNodoBuscado(int dato, nodo * lista)
  72. {
  73.     nodo * seg;
  74.     nodo * ante;
  75.     if((lista != NULL) && (lista->dato==dato))
  76.     {
  77.         nodo * aux = lista;
  78.         lista = lista->siguiente;
  79.         free(aux);
  80.     }
  81.     else
  82.     {
  83.         seg = lista;
  84.         while((seg != NULL) && (seg->dato!=dato))
  85.         {
  86.             ante = seg;
  87.             seg = seg->siguiente;
  88.         }
  89.  
  90.         if(seg!=NULL)
  91.         {
  92.             ante->siguiente = seg->siguiente;
  93.  
  94.             free(seg);
  95.         }
  96.     }
  97.     return lista;
  98. }
  99.  
  100. nodo * agregarEnOrden(nodo * lista, nodo * nuevoNodo)
  101.  
  102. {
  103.     if(lista == NULL)
  104.     {
  105.         lista = nuevoNodo;
  106.     }
  107.     else
  108.     {
  109.  
  110.         if(nuevoNodo->dato < lista->dato)
  111.             lista = agregarPpio(lista, nuevoNodo);
  112.         else
  113.         {
  114.  
  115.             nodo * ante = lista;
  116.             nodo * seg = lista;
  117.             while(seg != NULL && nuevoNodo->dato > seg->dato)
  118.             {
  119.                 ante = seg;
  120.                 seg = seg->siguiente;
  121.             }
  122.  
  123.             ante->siguiente = nuevoNodo;
  124.             nuevoNodo->siguiente = seg;
  125.         }
  126.     }
  127.     return lista;
  128. }
  129.  
  130. nodo * borrarTodaLaLista(nodo * lista)
  131. {
  132.     nodo * proximo;
  133.     nodo * seg;
  134.     seg = lista;
  135.     while(seg != NULL)
  136.     {
  137.         proximo = seg->siguiente;
  138.         free(seg);
  139.         seg = proximo;
  140.     }
  141.     return seg;
  142. }
  143.  
  144. nodo * borrarPrimerNodo(nodo * lista)
  145. {
  146.     nodo * aux=lista;
  147.     lista=lista->siguiente;
  148.     free(aux);
  149.     return lista;
  150. }
  151.  
  152. nodo * borrarUltimoNodo(nodo * lista)
  153. {
  154.     nodo * aux=lista;
  155.     nodo * ante;
  156.     while(aux->siguiente!=NULL)
  157.     {
  158.         ante=aux;
  159.         aux=aux->siguiente;
  160.     }
  161.     free(aux);
  162.     ante->siguiente=NULL;
  163.     return lista;
  164. }
  165.  
  166. int sumarEdadesLista(nodo * lista)
  167. {
  168.     int suma = 0;
  169.     nodo * seg = lista;
  170.     while (seg != NULL)
  171.     {
  172.         suma = suma + seg->dato;
  173.         seg = seg->siguiente;
  174.     }
  175.     return suma;
  176. }
  177.  
  178. void mostrar(nodo * aux)
  179. {
  180.     printf("%d ",aux->dato);
  181. }
  182.  
  183. void recorrerYmostrar(nodo * lista)
  184. {
  185.     nodo * aux=lista;
  186.     while(aux!=NULL)
  187.     {
  188.         mostrar(aux);
  189.         aux=aux->siguiente;
  190.     }
  191.     puts("\n");
  192. }
  193.  
  194. nodo * IngresarDatosAlFinal(nodo * lista)
  195. {
  196.     int dato;
  197.     int cant;
  198.     nodo * aux;
  199.     printf("ingrese cantidad ingresar: ");
  200.     scanf("%d",&cant);
  201.     while(cant>0)
  202.     {
  203.         printf("ingrese un dato: ");
  204.         scanf("%d",&dato);
  205.         aux=crearNodo(dato);
  206.         lista=agregarFinal(lista,aux);
  207.         cant--;
  208.     }
  209.     return lista;
  210. }
  211.  
  212. nodo * IngresarDatosAlPpio(nodo * lista)
  213. {
  214.     int dato;
  215.     int cant;
  216.     nodo * aux;
  217.     printf("ingrese cantidad ingresar: ");
  218.     scanf("%d",&cant);
  219.     while(cant>0)
  220.     {
  221.         printf("ingrese un dato: ");
  222.         scanf("%d",&dato);
  223.         aux=crearNodo(dato);
  224.         lista=agregarPpio(lista,aux);
  225.         cant--;
  226.     }
  227.     return lista;
  228. }
  229.  
  230. nodo * AgregarUnNodoEnOrden(nodo * lista)
  231. {
  232.     int dato;
  233.     nodo * aux=lista;
  234.     printf("ingrese un dato: ");
  235.     scanf("%d",&dato);
  236.     aux=agregarEnOrden(lista,crearNodo(dato));
  237.     return aux;
  238. }
  239.  
  240. nodo * CrearListaOrdenada(nodo * lista)
  241. {
  242.     int cant;
  243.     nodo * aux=lista;
  244.     printf("ingrese cantidad a ingresar: ");
  245.     scanf("%d",&cant);
  246.     while(cant>0)
  247.     {
  248.         aux=AgregarUnNodoEnOrden(aux);
  249.         cant--;
  250.     }
  251.     return aux;
  252. }
  253.  
  254. void BusquedaDeUnNodo(nodo * lista)
  255. {
  256.     int dato;
  257.     printf("ingrese dato a buscar: ");
  258.     scanf("%d",&dato);
  259.     nodo * aux=inicLista();
  260.     aux=buscarNodo(dato,lista);
  261.     if(aux!=NULL)
  262.         printf("%d",aux->dato);
  263. }
  264.  
  265. nodo * BorrarNodo(nodo * lista){
  266.     int dato;
  267.     printf("ingrese dato a borrar de la lista: ");
  268.     scanf("%d",&dato);
  269.     lista=borrarNodoBuscado(dato,lista);
  270.     return lista;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment