Advertisement
Arael_Alvarez_Curiel

Tarea 3 MR1001 Source

Jan 23rd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "VenturaLibrary.h"
  5.  
  6.  
  7.  
  8. Ins* anadirInstruccion(Ins *head , char instruccion[20], int tiempo) //Ins *rootFromMain
  9. {
  10.     if(head == NULL) //si no existe la lista, esto es, no hay memoria asignada, se crea el primer elemento 1 sola vez
  11.     {
  12.         head = (Ins *)malloc(sizeof(Ins));
  13.         if(head == NULL) //opcional pero una buena práctica es preguntar si fue posible porque sí hubo memoria disponible
  14.         {
  15.             return NULL; // será la convención (en esta clase) de que no hubo éxito
  16.         }
  17.         palindromo(instruccion, tiempo);
  18.         strcpy(head->command,instruccion);
  19.         head->tiempo = tiempo;
  20.         head->Next = NULL;
  21.         return head; // 1 será la convención (en esta clase) de que sí hubo éxito
  22.     }
  23.     else //si ya existe el head se estarán agregando (este código es el que se estará ejecutando la mayoría de las veces
  24.     {
  25.  
  26.         Ins *temporal = (Ins *)malloc(sizeof(Ins));
  27.         Ins *current = head;
  28.         if(temporal == NULL) //opcional pero una buena práctica es preguntar si fue posible porque sí hubo memoria disponible
  29.         {
  30.             return NULL; // será la convención (en esta clase) de que no hubo éxito
  31.         }
  32.         palindromo(instruccion,tiempo);
  33.         strcpy(temporal->command,instruccion);
  34.         temporal->tiempo = tiempo;
  35.         temporal->Next = NULL;
  36.  
  37.         while(current->Next != NULL) //esto es para ir "caminando" y añadir al final el elemento
  38.         {
  39.             current=current->Next;
  40.         }
  41.         current->Next = temporal;
  42.  
  43.         return head;
  44.     }
  45.  
  46. }
  47. void recorrer (Ins* head)
  48. {
  49.     Ins *temp = head;
  50.     printf("Recorriendo...\n");
  51.     while(temp) {
  52.       printf("Nombre: %s\n", temp->command);
  53.       printf("Tiempo: %d\n", temp->tiempo);
  54.       temp = temp->Next ;
  55.    }
  56. }
  57.  
  58. Ins* mandarInstrucciones(Ins *head) //en otras palabras, borrar
  59. {
  60.     Ins *temp = head;
  61.     printf("Recorriendo...\n");
  62.     while(temp) {
  63.       printf("Borrando{ Nombre: %s\n }\n", temp->command);
  64.       head = temp->Next;
  65.  
  66.       free(temp);
  67.       temp = head;
  68.    }
  69.    return NULL;
  70. }
  71.  
  72. void palindromo(char instruccion[20], int tiempo){
  73.  
  74.     char reves[30];
  75.     strcpy(reves,instruccion);
  76.     strrev(reves);
  77.     int a=0;
  78.     for(int i=0;i<strlen(instruccion);i++){
  79.         if(instruccion[i]==reves[i]){
  80.             a++;
  81.             if(a==strlen(instruccion)){
  82.                 if(tiempo>5){
  83.                    return 0;
  84.                     }else{
  85.                     exit(1);
  86.                     }
  87.                 }
  88.             }else{
  89.                 exit(1);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement