Advertisement
regzarr

frogs_lakes_WIP_Calin

Dec 4th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 32
  6.  
  7. typedef struct _cargo {
  8.   char code[SIZE];
  9.   struct _cargo *next;
  10. }cargo;
  11.  
  12. typedef struct _ship {
  13.   char name[SIZE];
  14.   cargo *cargoList;
  15.   struct _ship *next;
  16. }ship;
  17.  
  18. void showAll(ship **primeShip) {
  19.   ship *primary = *primeShip;
  20.   cargo *secondary = NULL;
  21.  
  22.   while (primary != NULL) {
  23.     printf("%s\n", primary->name);
  24.     secondary = primary->cargoList;
  25.     while (secondary != NULL) {
  26.       printf("  --- %s\n", secondary->code);
  27.       secondary = secondary->next;
  28.     }
  29.     primary = primary->next;
  30.   }
  31. }
  32.  
  33. // add ship in primary list
  34. void addPrimary(ship **first, ship *toAdd) {
  35.   ship **ptr = first;
  36.   while ((*ptr)->next != NULL) {
  37.     // iterate through list until last element is found
  38.     ptr = &(*ptr)->next;
  39.   }
  40.   (*ptr)->next = toAdd;
  41. }
  42.  
  43. // wrapper for addPrimary()
  44. void addPrimaryFromStdin(ship **first) {
  45.   ship *toAdd = malloc(sizeof(ship));
  46.   *toAdd = (ship){0};
  47.  
  48.   printf("Name for ship:\n");
  49.   fgets(toAdd->name, SIZE, stdin);
  50.   toAdd->name[strlen(toAdd->name) - 1] = 0; // get rid of newline char
  51.   addPrimary(first, toAdd);
  52. }
  53.  
  54. // add cargo in secondary list
  55. void addSecondary(cargo **first, cargo *toAdd) {
  56.   cargo **ptr = first;
  57.   while ((*ptr) != NULL) {
  58.     // iterate through list until last element is found
  59.     ptr = &(*ptr)->next;
  60.   }
  61.  
  62.   (*ptr) = toAdd;
  63. }
  64.  
  65. // wrapper for addSecondary()
  66. void addSecondaryFromStdin(cargo **first) {
  67.   cargo *toAdd = malloc(sizeof(cargo));
  68.   *toAdd = (cargo){0};
  69.  
  70.   printf("Name for cargo:\n");
  71.   fgets(toAdd->code, SIZE, stdin);
  72.   toAdd->code[strlen(toAdd->code) - 1] = 0; // to get rid of newline char
  73.   addSecondary(first, toAdd);
  74. }
  75.  
  76. // deletes all lists
  77. void init(ship **first) {
  78.   cargo **cargoPtr = &(*first)->cargoList;
  79.   ship **ptr = first;
  80.   while ((*ptr) != NULL) {
  81.     while ((*cargoPtr) != NULL) {
  82.       free(*cargoPtr);
  83.       cargoPtr = &(*cargoPtr)->next;
  84.     }
  85.     free(*ptr);
  86.     ptr = &(*ptr)->next;
  87.   }
  88. }
  89.  
  90. int main() {
  91.   ship *primeShip = malloc(sizeof(ship));
  92.   *primeShip = (ship){0};
  93.  
  94.   addPrimaryFromStdin(&primeShip);
  95.   addPrimaryFromStdin(&primeShip);
  96.  
  97.   addSecondaryFromStdin(&(primeShip->cargoList));
  98.   addSecondaryFromStdin(&(primeShip->cargoList));
  99.  
  100.   showAll(&primeShip);
  101.   init(&primeShip);
  102.   showAll(&primeShip);
  103.   return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement