Advertisement
pmanriquez93

Pregunta 15 - Listas, Pilas y Colas

Jul 2nd, 2014
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. /* CABECERA */
  2.  
  3.  
  4. /*
  5.  * File:   preg15.h
  6.  * Author: alulab11
  7.  *
  8.  * Created on 2 de julio de 2014, 03:40 PM
  9.  */
  10.  
  11. #ifndef PREG15_H
  12. #define PREG15_H
  13. #define MAX 10
  14.  
  15. typedef int TElemento;
  16.  
  17. typedef struct nodoPila{
  18.     TElemento elem;
  19.     struct nodoPila* ptrSig;
  20. }TNodoPila;
  21.  
  22. typedef struct nodoCjtoPilas{
  23.     TNodoPila *cima;
  24.     struct nodoCjtoPilas* ptrSig;
  25.     int cantElem;
  26.     int capacidad;
  27. }TNodoCjtoPilas;
  28.  
  29. typedef struct CjtoPilas{
  30.     TNodoCjtoPilas *inicio;
  31. }TCjtoPilas;
  32.  
  33.  
  34.  
  35. void crearCjtoPilas(TCjtoPilas*);
  36.  
  37. void push(TCjtoPilas*,TElemento);
  38.  
  39. void pop(TCjtoPilas*);
  40.  
  41. int esCjtoPilasVacio(TCjtoPilas*);
  42.  
  43. #endif  /* PREG15_H */
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /* IMPLEMENTACION */
  50.  
  51.  
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include "preg15.h"
  55.  
  56. #define MAX 10
  57.  
  58. void crearCjtoPilas(TCjtoPilas* cjtoPilas){
  59.     cjtoPilas->inicio = NULL;    
  60. }
  61.  
  62.  
  63. void push(TCjtoPilas* cjtoPilas,TElemento elem){
  64.     TNodoPila *ptrNuevo;
  65.     ptrNuevo = (TNodoPila*)malloc(sizeof(TNodoPila));
  66.     ptrNuevo->elem = elem;
  67.     ptrNuevo->ptrSig = NULL;
  68.    
  69.    
  70.     TNodoCjtoPilas *ptrRec;
  71.     ptrRec = cjtoPilas->inicio;
  72.     if (ptrRec == NULL){
  73.         TNodoCjtoPilas *pilaNueva;
  74.         cjtoPilas->inicio = pilaNueva;
  75.        
  76.         pilaNueva->cantElem = 1;
  77.         pilaNueva->capacidad = MAX;
  78.        
  79.         pilaNueva->cima = ptrNuevo;
  80.     }
  81.     else{
  82.         while(ptrRec->cantElem < ptrRec->capacidad)
  83.                 ptrRec = ptrRec->ptrSig;
  84.         if (ptrRec == NULL){
  85.             TNodoCjtoPilas *pilaNueva2;
  86.             ptrRec->ptrSig = pilaNueva2;
  87.             pilaNueva2->cantElem = 1;
  88.             pilaNueva2->capacidad = MAX;
  89.            
  90.             pilaNueva2->cima = ptrNuevo;
  91.         }
  92.         else{
  93.             ptrNuevo->ptrSig = ptrRec->cima;
  94.             ptrRec->cima = ptrNuevo;
  95.             (ptrRec->cantElem)++;
  96.         }
  97.     }
  98.    
  99. }
  100.  
  101. void pop(TCjtoPilas* cjtoPilas){
  102.     if(cjtoPilas->inicio == NULL)
  103.         printf("No se puede desapilar en una pila vacía\n");
  104.     else{
  105.         TNodoPila* ptrEliminar;
  106.         ptrEliminar = cjtoPilas->inicio->cima;
  107.         cjtoPilas->inicio->cima = cjtoPilas->inicio->cima->ptrSig;
  108.         if (cjtoPilas->inicio->cantElem == 1){
  109.             TNodoCjtoPilas * cjtoEliminar;
  110.             cjtoEliminar = cjtoPilas->inicio;
  111.             cjtoPilas->inicio = cjtoPilas->inicio->ptrSig;
  112.             free(cjtoEliminar);
  113.         }else{
  114.             (cjtoPilas->inicio->cantElem)--;
  115.         }
  116.         free(ptrEliminar);
  117.     }
  118. }
  119.  
  120. int esCjtoPilasVacio(TCjtoPilas* cjtoPilas){
  121.     if(cjtoPilas->inicio == NULL)
  122.         return 1;
  123.     else
  124.         return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement