daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest May 16th, 2018 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<malloc.h>
  5.  
  6. struct node{
  7.   int code,price;
  8.   struct node *left;
  9.   struct node *right;
  10. };
  11.  
  12. struct node *tree;
  13. void create_tree(struct node *tree){
  14.     tree=NULL;
  15. }
  16. void menu(){
  17.   printf("Soniem Cellular Phone\n");
  18.   printf("~~~~~~~~~~~~~~~~~~~~~\n\n");
  19.   printf("1. add new phone\n");
  20.   printf("2. update phone price\n");
  21.   printf("3. Inorder,Preorder,Postorder\n");
  22.   printf("4. exit\n");
  23. }
  24.  
  25. struct node *add(struct node *tree){
  26.   struct node *newnode,*nodeptr,*parentptr;
  27.   int harga,kode;
  28.  
  29.   do{
  30.   printf("Input phone code S[1-9][0-9][0-9][0-9] : S");
  31.   scanf("%d",&kode);
  32.   }while(kode<1000||kode>9999);
  33.  
  34.   do {
  35.      printf("input phone price[$50...$999]: ");
  36.      scanf("%d",&harga);
  37.    } while(harga<50 || harga>999);
  38.  
  39.  
  40.   newnode=(struct node *)malloc(sizeof(struct node));
  41.   newnode->code=kode;
  42.   newnode->price=harga;
  43.   newnode->left=NULL;
  44.   newnode->right=NULL;
  45.  
  46.   if(tree==NULL){
  47.     tree=newnode;
  48.     tree->left=NULL;
  49.     tree->right=NULL;
  50.   }else{
  51.     parentptr=NULL;
  52.     nodeptr=tree;
  53.  
  54.     while(nodeptr!=NULL)
  55.     {
  56.       parentptr=nodeptr;
  57.       if(kode<=tree->code){
  58.         nodeptr=nodeptr->left;
  59.       }else{
  60.         nodeptr=nodeptr->right;
  61.       }
  62.     }
  63.     if(kode<=tree->code){
  64.       parentptr->left=newnode;
  65.     }else{
  66.       parentptr->right=newnode;
  67.     }
  68.   }
  69.   return tree;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. int main(){
  78.   int pilih;
  79.   do {
  80.   menu();
  81.   scanf("%d",&pilih );
  82.   getchar();
  83.   if (pilih == 1) {
  84.     tree=add(tree);
  85.   }else if(pilih == 2){
  86.   }else if(pilih == 3){
  87.  
  88.   }
  89. }while(pilih !=4);
  90.   return 0;
  91. }
RAW Paste Data
Top