Advertisement
pmanriquez93

Problema 2 - Lista, Pilas y Colas

May 25th, 2014
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. /*
  2.  * File:   preg2.h
  3.  * Author: alulab11
  4.  *
  5.  * Created on 23 de mayo de 2014, 03:46 PM
  6.  */
  7.  
  8. #ifndef PREG2_H
  9. #define PREG2_H
  10.  
  11. typedef char TElemento;
  12.  
  13. typedef struct nodo{
  14.     TElemento elem;
  15.     struct nodo* ptrSig;
  16. }TNodo;
  17.  
  18. typedef struct{
  19.     TNodo *inicio;    
  20. }TLista;
  21.  
  22.  
  23. void apilaOpuesto(TLista *, char);
  24.  
  25. int es_cierre(char);
  26.  
  27. void imprimeLista(TLista *lista);
  28.  
  29. void crearLista(TLista *lista);
  30.  
  31. TElemento desapilaOpuesto(TLista *);
  32.  
  33. int esVacio(TLista *);
  34.  
  35. #endif  /* PREG2_H */
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include "lista.h"
  46.  
  47.  
  48.  
  49. int es_cierre(char c){
  50.     if (c == ']' || c == ')')
  51.         return 1;
  52.     return 0;
  53. }
  54.  
  55. void apilaOpuesto (TLista *lista, char car){
  56.     TNodo *ptrNuevo;
  57.     ptrNuevo = (TNodo*)malloc(sizeof(TNodo));
  58.    
  59.     ptrNuevo->ptrSig = NULL;
  60.    
  61.     if (car == '['){
  62.         ptrNuevo->elem = ']';
  63.     }
  64.     else if (car == '('){
  65.         ptrNuevo->elem = ')';
  66.     }
  67.    
  68.     ptrNuevo->ptrSig = lista->inicio;
  69.     lista->inicio = ptrNuevo;        
  70. }
  71.  
  72. void imprimeLista(TLista *lista){
  73.     TNodo *ptrRec;
  74.    
  75.     ptrRec = lista->inicio;
  76.     while (ptrRec != NULL){
  77.         printf("%c ",ptrRec->elem);
  78.         ptrRec = ptrRec->ptrSig;
  79.     }
  80.     printf("NULL\n");
  81. }
  82.  
  83. void crearLista(TLista *lista){
  84.     lista->inicio = NULL;
  85. }
  86.  
  87.  
  88. TElemento desapilaOpuesto(TLista * lista){
  89.     TNodo *ptrEliminar;
  90.    
  91.     TElemento car;
  92.     ptrEliminar = lista->inicio;
  93.     car = ptrEliminar->elem;
  94.     lista->inicio = lista->inicio->ptrSig;
  95.     free(ptrEliminar);
  96.     return car;  
  97. }
  98.  
  99. int esVacio(TLista *lista){
  100.     return (lista->inicio != NULL);
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /*
  110.  * File:   main.c
  111.  * Author: alulab11
  112.  *
  113.  * Created on 23 de mayo de 2014, 03:45 PM
  114.  */
  115.  
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118. #include <string.h>
  119. #include "lista.h"
  120.  
  121. #define MAX 12
  122.  
  123. /*
  124.  *
  125.  */
  126. int main(int argc, char** argv) {
  127.    
  128.     TLista lista;
  129.    
  130.     //crearLista(&lista);
  131.    
  132.     char cadena[MAX];
  133.     TElemento car, car2;
  134.     scanf("%s",cadena);
  135.    
  136.     int i;
  137.     for(i=0;i<(strlen(cadena));i++){
  138.         car = cadena[i];
  139.         if (es_cierre(car)==0){
  140.             apilaOpuesto(&lista,car);        
  141.         }
  142.         else{
  143.             if(esVacio(&lista)){
  144.                 printf("------> NO ES CORRECTA\n");
  145.                 return 0;
  146.             }
  147.             car2 = desapilaOpuesto(&lista);
  148.             if (car2 != cadena[i]){
  149.                 printf("------> NO ES CORRECTA\n");
  150.                 return 0;
  151.             }
  152.         }      
  153.     }
  154.    
  155.     if (esVacio(&lista)){
  156.         printf("------> NO ES CORRECTA\n");
  157.     }
  158.     else{
  159.         printf("------> ES CORRECTA\n");
  160.     }
  161.    
  162.     printf("\nLo que queda de la lista: \n");
  163.     imprimeLista(&lista);
  164.  
  165.     return (EXIT_SUCCESS);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement