Thursday, April 9, 2020

codingan double linked list sederhan

codingan double linked list sederhana

menu dream market

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct data{
 char nama[50];
 int banyak,harga;
 struct data *next;
 struct data *prev;
}*head=NULL,*tail=NULL,*curr;
struct data* createnode(char name[],int qnty){
 struct data *node = (struct data*)malloc(sizeof(struct data));
 strcpy(node->nama,name);
 node->banyak=qnty;
 node->harga=(rand()%(499000))+1000;
 node->next = node->prev=NULL;
 return node;
};
void pushhead(struct data *node){
 if(head==NULL){
  head=tail=node;
 }
 else{
  node->next=head;
  head->prev=node;
  head=node;
 }
}
void pushtail(struct data *node){
 tail->next=node;
 node->prev=tail;
 tail= node;
}
void insertdata(char name[],int qnty){
 struct data *node = createnode(name,qnty);
 if(head==NULL){
  pushhead(node);
 }
 else{
  if(strcmp(head->nama,name)>0){
   pushhead(node);
  }
  else if(strcmp(tail->nama,name)<0){
   pushtail(node);
  }
  else{
   struct data *temp = head;
   while(temp!=NULL){
    if(strcmp(temp->nama,name)>0) break;
    temp = temp->next;
   }
   node->next = temp;
   node->prev = temp->prev;
   temp->prev->next=node;
   temp->prev = node;
  }
 }
}
void printll(){
 struct data *node= head;
 int i=1;
 int total=0;
 printf("\nNO -    Nama (quantity)  -   Price\n");
 printf("===================================\n");
 while(node!=NULL){
 printf("%d - %s(%d) - %-7d\n",i,node->nama,node->banyak,node->harga);
  total+=(node->harga*node->banyak);
  node=node->next;
  i++;
 }
 printf("Total harga = Rp%d\n\n",total);
}
//void printbelakang(){
// struct data *node = tail;
// while(node!=NULL){
//  printf("%s  %d  %d\n",node->nama,node->banyak,node->harga);
//  node=node->prev;
// }
//}
void pophead(){
 if(!head){
  return;
 }
 else if(head==tail){
  head = tail =NULL;
  free(head);
 }
 else{
  data *temp = head->next;
  head->next=temp->prev=NULL;
  free(head);
  head=temp;
 }
}
void poptail(){
 struct data *temp = tail->prev;
 temp->next=tail->prev=NULL;
 free(tail);
 tail=temp;
}
void popmid(char temp[]){
 curr=head;
 while(curr!=NULL){
  if(strcmp(curr->nama,temp)==0){
   break;
  }
  curr=curr->next;
 }
 if(curr==NULL){
 }
 else{
  curr->prev->next=curr->next;
  curr->next->prev=curr->prev;
  free(curr);
 }
}
void deletes(char temp[]){
 if(strcmp(head->nama,temp)==0){
  pophead();
 }
 else if(strcmp(tail->nama,temp)==0){
  poptail();
 }
 else{
  popmid(temp);
 }
}
bool search (char temp[]){
 curr=head;
 while(curr!=NULL){
  if(strcmp(curr->nama,temp)==0){
   break;
  }
  else{
   curr=curr->next;
  }
 }
 if(curr==NULL){
  return false;
 }
 else{
  return true;
 }
}
int main(){
 char tempnama[50];
 int tempbanyak;
 int menu=0;
 char temp[50];
 int change;
 do{
  printf("    Dreamer Market\n");
  printf("=======================\n");
  printf("1. Add item\n");
  printf("2. View item\n");
  printf("3. Edit quantity\n");
  printf("4. Delete item\n");
  printf("5. Checkout\n");
  printf(">> ");
  scanf("%d",&menu);getchar();
  switch(menu){
   case 1:
    printf("\n  Add item\n");
    printf("=============\n");
    printf("Item name: ");
    scanf("%s",&tempnama);getchar();
    printf("Item quantity: ");
    scanf("%d",&tempbanyak);getchar();
    insertdata(tempnama,tempbanyak);
    printf("\nItem added\n\n");
    break;
   
   case 2:
    printll();
    break;
  
   case 3:
    printf("\n    Edit item\n");
    printf("=================\n");
    printf("Item name: ");
    scanf("%s",&temp);
    if(search(temp)){
     printf("Total Quantity: %d",curr->banyak);
     printf("Insert Quantity to be changed: ");
     scanf("%d",&change);getchar();
     curr->banyak=change;
     printf("\nItem updated\n\n");
    }
    else{
     printf("\nItem is not found\n\n");
    }
    break;
   
   case 4:
    printf("\n  Delete item\n");
    printf("================\n");
    printf("Item name: ");
    scanf("%s",&temp);
    deletes(temp);
    printf("\nItem deleted\n\n");
    break;
   
   case 5:
    break;
  }
 }while(menu!=5);
 printll();
 printf("It's on us, Kindness is Free, Stay safe from corona :)\n");
 return 0;
}