Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct wagonTrain{
  5.     char cargoType;
  6.     int cargoID;
  7.     struct wagonTrain *next;
  8. };
  9.  
  10. struct wagonTrain *pushCargo(struct wagonTrain *WT, char cargoType, int cargoID){
  11.     struct wagonTrain *newCargo = (struct wagonTrain*)malloc(sizeof(struct wagonTrain));
  12.     newCargo->cargoType = cargoType;
  13.     newCargo->cargoID = cargoID;
  14.     newCargo->next = NULL;
  15.    
  16.     if(WT == NULL){
  17.         WT = newCargo;     
  18.     }
  19.     else{
  20.         struct wagonTrain *curr = WT;
  21.         while(curr->next != NULL) curr = curr->next;
  22.         curr->next = newCargo; 
  23.     }
  24.     return WT;
  25. }
  26.  
  27. void printWagonTrain(struct wagonTrain *WT){
  28.     struct wagonTrain *curr = WT;
  29.     while(curr != NULL){
  30.         if(curr == WT)
  31.             printf("%c%d", curr->cargoType, curr->cargoID);
  32.         else if(curr->next == NULL)
  33.             printf(" :: %c%d\n", curr->cargoType, curr->cargoID);
  34.         else
  35.             printf(" :: %c%d", curr->cargoType, curr->cargoID);
  36.         curr = curr->next;
  37.     }
  38. }
  39.  
  40. void printAllTrainSet(struct wagonTrain *polarExpress, struct wagonTrain *bachmannPenn, struct wagonTrain *santaFe, struct wagonTrain *hogwartsExpress){
  41.     printf("LIST OF TRAIN SET\n");
  42.     printf("=================\n");
  43.    
  44.     printf("%-17s %s", "Polar Express", "= ");
  45.     printWagonTrain(polarExpress);
  46.  
  47.     printf("%-17s %s", "Bachmann Penn", "= ");
  48.     printWagonTrain(bachmannPenn);
  49.  
  50.     printf("%-17s %s", "Santa Fe", "= ");
  51.     printWagonTrain(santaFe);
  52.  
  53.     printf("%-17s %s", "Hogwarts Express", "= ");
  54.     printWagonTrain(hogwartsExpress);
  55. }
  56.  
  57. // [#] INSERT YOUR CODE HERE, YOU NEED TO COMPLETE THIS FUNCTION WITH YOUR ANSWER
  58. int checkWagonTrainCargo(struct wagonTrain *A, struct wagonTrain *B){
  59.     int count=0;
  60.     int countBaru=9;
  61.     struct wagonTrain *curr = A;
  62.     struct wagonTrain *currTwo = B;
  63.  
  64.     for(int i = 0 ; i<9;i++){
  65.         if(curr->cargoID==currTwo->cargoID){
  66.             curr = curr->next;
  67.             currTwo = currTwo->next;
  68.             count++;
  69.         }else{
  70.             countBaru--;
  71.         }
  72.     }
  73.    
  74.     return countBaru;
  75. }
  76.  
  77. int main(){
  78.     int i = 0, j = 0;
  79.     char cargoType[3] = {'A', 'B', 'C'};
  80.     int cargoIDType[10] = {12, 23, 35, 47, 56, 66, 78, 84, 92};
  81.  
  82.     // TRAIN SET 1 - Polar Express
  83.     struct wagonTrain *polarExpress = NULL;
  84.     for(i=0 ; i<9 ; i++)
  85.         polarExpress = pushCargo(polarExpress, cargoType[i%3], cargoIDType[i]);
  86.  
  87.     // TRAIN SET 2 = Bachmann Penn
  88.     struct wagonTrain *bachmannPenn = NULL;
  89.     for(i=0 ; i<9 ; i+=2)
  90.         bachmannPenn = pushCargo(bachmannPenn, cargoType[i%3], cargoIDType[i]);
  91.     for(j=1 ; j<9 ; j+=2)
  92.         bachmannPenn = pushCargo(bachmannPenn, cargoType[j%3], cargoIDType[j]);
  93.        
  94.     // TRAIN SET 3 = Santa Fe
  95.     struct wagonTrain *santaFe = NULL;
  96.     for(i=8 ; i>=0 ; i--)
  97.         santaFe = pushCargo(santaFe, cargoType[i%3], cargoIDType[i]);
  98.        
  99.     // TRAIN SET 4 = Hogwarts Express
  100.     struct wagonTrain *hogwartsExpress = NULL;
  101.     hogwartsExpress = pushCargo(hogwartsExpress, 'A', 12);
  102.     hogwartsExpress = pushCargo(hogwartsExpress, 'B', 23);
  103.     hogwartsExpress = pushCargo(hogwartsExpress, 'C', 35);
  104.     for(i=3 ; i<9 ; i++)
  105.         hogwartsExpress = pushCargo(hogwartsExpress, cargoType[i%3], cargoIDType[i]);
  106.  
  107.     // A FUNCTION TO SEE ALL TRAIN SET
  108.     printAllTrainSet(polarExpress, bachmannPenn, santaFe, hogwartsExpress);
  109.  
  110.     // POLAR EXPRESS & BACHMANN PENN
  111.     printf("%d\n", checkWagonTrainCargo(polarExpress, bachmannPenn));
  112.    
  113.     // SANTA FE & HOGWARTS EXPRESS
  114.     printf("%d\n", checkWagonTrainCargo(santaFe, hogwartsExpress));
  115.    
  116.     // HOGWARTS EXPRESS & POLAR EXPRESS
  117.     printf("%d\n", checkWagonTrainCargo(polarExpress, hogwartsExpress));
  118.  
  119.     getchar();
  120.     return (0);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement