Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #include <stdlib.h>
- struct beras{
- char tipeBeras[10];
- int stok;
- struct beras *next;
- };
- struct beras *head = NULL;
- void clearbuffer();
- void menu(struct beras *);
- struct beras *insertKeAwal(struct beras *);
- struct beras *hapusAwal(struct beras *);
- int cekTipe(const char tipeBeras[]);
- int main() {
- int pilihanMenu;
- do{
- system("clear");
- menu(head);
- printf("masukkan pilihan : ");
- scanf("%d", &pilihanMenu); clearbuffer();
- if (pilihanMenu == 1)
- head = insertKeAwal(head);
- if (pilihanMenu == 2)
- head = hapusAwal(head);
- }while (pilihanMenu != 3);
- return 0;
- }
- void clearbuffer(){
- char ch;
- while ((ch = getchar()) != '\n' && ch != EOF);
- }
- void menu(struct beras *head){
- printf("blue rice stock\n");
- printf("rice stock <STACK>\n\n");
- struct beras *ptr;
- ptr = head;
- while (ptr != NULL){
- if (ptr == head)
- printf("[%s grain] : %d kg(s) -> [top]\n", ptr->tipeBeras, ptr->stok);
- else
- printf("[%s grain] : %d kg(s)\n", ptr->tipeBeras, ptr->stok);
- ptr = ptr->next;
- }
- printf("\noption : \n");
- printf("1. Stok Beras\n");
- printf("2. Jual Beras\n");
- printf("3. keluar\n\n");
- }
- struct beras *insertKeAwal(struct beras *head) {
- struct beras *berasBaru; //inisialisasi tempat buat menyimpat data beras
- char tipeBeras[10];
- int stok;
- int cek;
- do {
- printf("masukkan tipe beras[short/medium/long] : ");
- scanf("%s", tipeBeras); clearbuffer();
- cek = cekTipe(tipeBeras);
- }while (cek == 0);
- do {
- printf("masukkan stok [10-100] kg : ");
- scanf("%d", &stok); clearbuffer();
- } while (stok < 10 || stok > 100);
- berasBaru = (struct beras *) malloc(sizeof(struct beras)); //alokasikan memory
- strcpy(berasBaru->tipeBeras, tipeBeras); //masukkan nilai
- berasBaru->stok = stok; //masukkan nilai
- /* beras baru
- * ---------------
- * | | |
- * | data | |
- * | | |
- * ---------------
- */
- if (head == NULL){
- berasBaru->next = NULL; //set pointer next jadi null karena ini adalah node terakhir
- head = berasBaru;
- /* beras baru
- * ---------------
- * | | |
- * head -> | data | X |
- * | | |
- * ---------------
- */
- } else {
- berasBaru->next = head;
- head = berasBaru;
- /* head
- * |
- * beras baru V beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * | data | | | data | ----->| data | X |
- * | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- /* head
- * |
- * beras baru V beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * | data | ---->| data | ----->| data | X |
- * | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- /* beras baru beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * head -> | data | ---->| data | ----->| data | X |
- * | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- }
- printf("sukses menambah beras...."); clearbuffer();
- return head;
- }
- struct beras *hapusAwal(struct beras *head){
- struct beras *temp;
- temp = head;
- /* beras baru beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * temp -> | data | ---->| data | ----->| data | X |
- * head -> | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- if (temp == NULL){
- printf("\ntidak ada data...");
- clearbuffer();
- } else {
- head = head->next;
- free(temp);
- printf("sukses menjual beras...."); clearbuffer();
- /* beras baru beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * temp -> | data | ---->| data | ----->| data | X |
- * head -> | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- /* head
- * |
- * beras baru V beras lama1 beras lama2
- * --------------- ---------------- ------------------
- * | | | | | | | | |
- * temp -> | data | ---->| data | ----->| data | X |
- * | | | | | | | | |
- * --------------- ---------------- ------------------
- */
- /* head
- * |
- * V beras lama1 beras lama2
- * ---------------- ------------------
- * | | | | | |
- * | data | ----->| data | X |
- * | | | | | |
- * ---------------- ------------------
- */
- }
- return head;
- }
- int cekTipe(const char tipeBeras[]){
- if (strcmp(tipeBeras, "long") == 0)
- return 1;
- if (strcmp(tipeBeras, "medium") == 0)
- return 1;
- if (strcmp(tipeBeras, "short") == 0)
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement