jaOjaa

pusing anjneg

May 27th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. void clrscr() {
  8. system("@cls||clear");
  9. }
  10.  
  11. struct node{
  12. int key;
  13. char name[50];
  14. struct node *left, *right;
  15. };
  16.  
  17. struct node *newnode (int item, char nama) {
  18. struct node *temp = (struct node*)malloc(sizeof(struct node));
  19. temp->key=item;
  20. temp->left=temp->right=NULL;
  21. temp->name = nama;
  22. return temp;
  23. }
  24.  
  25. void inorder(struct node *root) {
  26. if (root!=NULL) {
  27. inorder(root->left);
  28. printf("%d \n", root->key);
  29. inorder(root->right);
  30. }
  31. }
  32. struct node* insert(struct node* node, int key) {
  33. if (node==NULL) return newnode(key);
  34. if (key<node->key) node->left = insert(node->left, key);
  35. else if(key>node->key) node->right=insert(node->right,key);
  36. return node;
  37. }
  38.  
  39. void menu() {
  40. printf("PINK LIBRARY\n");
  41. printf("************\n\n");
  42. printf("1. View all book\n");
  43. printf("2. Add book\n");
  44. printf("3. Remove book\n");
  45. printf("4. Inorder, Preorder, Postorder\n");
  46. printf("5. Exit and remove all\n\n");
  47. }
  48.  
  49. int main() {
  50. struct node *root = NULL;
  51. int pil, number;
  52. char name[50];
  53. do {
  54. clrscr();
  55. menu();
  56. root = insert(root, 50);
  57. printf("> Input choice : ");
  58. scanf("%d", &pil);
  59. switch(pil) {
  60. case 1: {
  61. if (root==NULL) {
  62. clrscr();
  63. printf("\n\n\n\t\t--- There is no node available !!! ---");
  64. getch();
  65. }
  66. else {
  67. clrscr();
  68. printf("BOOK LIST\n");
  69. printf("*********\n");
  70. printf("- Senja di ufuk malam <1>\n");
  71. printf("- Srikandi barat <2>\n");
  72. getch();
  73. }
  74. break;
  75. }
  76. case 2: {
  77. do{
  78. printf("\nInput book name [3..50] : ");
  79. scanf("%s",&name);
  80. }while(strlen(name)<3 || strlen(name)>50);
  81. do{
  82. printf("\nInput book number [0..100] : ");
  83. scanf("%d",&number);
  84. }while(number<0 || number>100);
  85.  
  86. newnode(number, name);
  87. break;
  88. }
  89. case 3: {
  90.  
  91. break;
  92. }
  93. case 4: {
  94.  
  95. break;
  96. }
  97. }
  98. }while(pil!=5);
  99. root = insert(root, 50);
  100. insert(root,30);
  101. insert(root,40);
  102. insert(root,50);
  103. insert(root,10);
  104. inorder(root);
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment