Advertisement
asrori

linkedlist - beras

Mar 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5.  
  6. struct beras{
  7.     char tipeBeras[10];
  8.     int stok;
  9.     struct beras *next;
  10. };
  11.  
  12. struct beras *head = NULL;
  13.  
  14. void clearbuffer();
  15. void menu(struct beras *);
  16. struct beras *insertKeAwal(struct beras *);
  17. struct beras *hapusAwal(struct beras *);
  18. int cekTipe(const char tipeBeras[]);
  19.  
  20. int main() {
  21.     int pilihanMenu;
  22.  
  23.     do{
  24.         system("clear");
  25.         menu(head);
  26.         printf("masukkan pilihan : ");
  27.         scanf("%d", &pilihanMenu); clearbuffer();
  28.         if (pilihanMenu == 1)
  29.             head = insertKeAwal(head);
  30.         if (pilihanMenu == 2)
  31.             head = hapusAwal(head);
  32.     }while (pilihanMenu != 3);
  33.  
  34.     return 0;
  35. }
  36.  
  37. void clearbuffer(){
  38.     char ch;
  39.     while ((ch = getchar()) != '\n' && ch != EOF);
  40. }
  41.  
  42. void menu(struct beras *head){
  43.     printf("blue rice stock\n");
  44.     printf("rice stock <STACK>\n\n");
  45.  
  46.     struct beras *ptr;
  47.     ptr = head;
  48.  
  49.     while (ptr != NULL){
  50.  
  51.         if (ptr == head)
  52.             printf("[%s grain] : %d kg(s) -> [top]\n", ptr->tipeBeras, ptr->stok);
  53.         else
  54.             printf("[%s grain] : %d kg(s)\n", ptr->tipeBeras, ptr->stok);
  55.  
  56.         ptr = ptr->next;
  57.     }
  58.  
  59.     printf("\noption : \n");
  60.     printf("1. Stok Beras\n");
  61.     printf("2. Jual Beras\n");
  62.     printf("3. keluar\n\n");
  63. }
  64.  
  65. struct beras *insertKeAwal(struct beras *head) {
  66.     struct beras *berasBaru; //inisialisasi tempat buat menyimpat data beras
  67.     char tipeBeras[10];
  68.     int stok;
  69.     int cek;
  70.  
  71.     do {
  72.         printf("masukkan tipe beras[short/medium/long] : ");
  73.         scanf("%s", tipeBeras); clearbuffer();
  74.         cek = cekTipe(tipeBeras);
  75.     }while (cek == 0);
  76.  
  77.     do {
  78.         printf("masukkan stok [10-100] kg : ");
  79.         scanf("%d", &stok); clearbuffer();
  80.     } while (stok < 10 || stok > 100);
  81.  
  82.     berasBaru = (struct beras *) malloc(sizeof(struct beras)); //alokasikan memory
  83.     strcpy(berasBaru->tipeBeras, tipeBeras); //masukkan nilai
  84.     berasBaru->stok = stok; //masukkan nilai
  85.  
  86.     /*   beras baru
  87.      *   ---------------
  88.      *   |        |    |
  89.      *   |  data  |    |
  90.      *   |        |    |
  91.      *   ---------------
  92.      */
  93.  
  94.     if (head == NULL){
  95.         berasBaru->next = NULL; //set pointer next jadi null karena ini adalah node terakhir
  96.         head = berasBaru;
  97.  
  98.         /*              beras baru
  99.          *           ---------------
  100.          *           |        |    |
  101.          *   head -> |  data  | X  |
  102.          *           |        |    |
  103.          *           ---------------
  104.          */
  105.  
  106.     } else {
  107.         berasBaru->next = head;
  108.         head = berasBaru;
  109.  
  110.  
  111.         /*                              head
  112.          *                               |
  113.          *               beras baru      V beras lama1      beras lama2
  114.          *           ---------------  ----------------   ------------------
  115.          *           |        |    |  |         |    |   |          |     |
  116.          *           |  data  |    |  |   data  |  ----->|   data   |  X  |
  117.          *           |        |    |  |         |    |   |          |     |
  118.          *           ---------------  ----------------   ------------------
  119.          */
  120.  
  121.  
  122.         /*                              head
  123.          *                               |
  124.          *               beras baru      V beras lama1      beras lama2
  125.          *           ---------------  ----------------   ------------------
  126.          *           |        |    |  |         |    |   |          |     |
  127.          *           |  data  |  ---->|   data  |  ----->|   data   |  X  |
  128.          *           |        |    |  |         |    |   |          |     |
  129.          *           ---------------  ----------------   ------------------
  130.          */
  131.  
  132.  
  133.         /*              beras baru      beras lama1          beras lama2
  134.          *           ---------------  ----------------   ------------------
  135.          *           |        |    |  |         |    |   |          |     |
  136.          *   head -> |  data  |  ---->|   data  |  ----->|   data   |  X  |
  137.          *           |        |    |  |         |    |   |          |     |
  138.          *           ---------------  ----------------   ------------------
  139.          */
  140.     }
  141.  
  142.     printf("sukses menambah beras...."); clearbuffer();
  143.     return head;
  144. }
  145.  
  146. struct beras *hapusAwal(struct beras *head){
  147.     struct beras *temp;
  148.     temp = head;
  149.  
  150.     /*              beras baru      beras lama1          beras lama2
  151.      *           ---------------  ----------------   ------------------
  152.      *           |        |    |  |         |    |   |          |     |
  153.      *   temp -> |  data  |  ---->|   data  |  ----->|   data   |  X  |
  154.      *   head -> |        |    |  |         |    |   |          |     |
  155.      *           ---------------  ----------------   ------------------
  156.      */
  157.  
  158.  
  159.     if (temp == NULL){
  160.         printf("\ntidak ada data...");
  161.         clearbuffer();
  162.     } else {
  163.         head = head->next;
  164.         free(temp);
  165.         printf("sukses menjual beras...."); clearbuffer();
  166.         /*              beras baru      beras lama1          beras lama2
  167.          *           ---------------  ----------------   ------------------
  168.          *           |        |    |  |         |    |   |          |     |
  169.          *   temp -> |  data  |  ---->|   data  |  ----->|   data   |  X  |
  170.          *   head -> |        |    |  |         |    |   |          |     |
  171.          *           ---------------  ----------------   ------------------
  172.          */
  173.  
  174.         /*                              head
  175.          *                               |
  176.          *               beras baru      V   beras lama1          beras lama2
  177.          *           ---------------  ----------------   ------------------
  178.          *           |        |    |  |         |    |   |          |     |
  179.          *   temp -> |  data  |  ---->|   data  |  ----->|   data   |  X  |
  180.          *           |        |    |  |         |    |   |          |     |
  181.          *           ---------------  ----------------   ------------------
  182.          */
  183.  
  184.         /*    head
  185.          *    |
  186.          *    V   beras lama1      beras lama2
  187.          *   ----------------   ------------------
  188.          *   |         |    |   |          |     |
  189.          *   |   data  |  ----->|   data   |  X  |
  190.          *   |         |    |   |          |     |
  191.          *   ----------------   ------------------
  192.          */
  193.     }
  194.  
  195.     return head;
  196. }
  197.  
  198.  
  199. int cekTipe(const char tipeBeras[]){
  200.     if (strcmp(tipeBeras, "long") == 0)
  201.         return 1;
  202.     if (strcmp(tipeBeras, "medium") == 0)
  203.         return 1;
  204.     if (strcmp(tipeBeras, "short") == 0)
  205.         return 1;
  206.  
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement