Advertisement
ramytamer

final program

Jun 1st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct Data {
  7.     int id;
  8.     char name[40];
  9. }Data;
  10.  
  11. typedef struct Node{
  12.     Data data;
  13.     struct Node *left, *right;
  14. }Node;
  15.  
  16. typedef Node Tree;
  17.  
  18.  
  19. // FUNCTION TO GET DATA OF NODE
  20. // #DATA
  21. int data(Node *node){
  22.     return node->data.id;
  23. }
  24. // FUNCTION TO GET DATA OF NODE
  25.  
  26. // START OF QUEUE STUFF
  27. typedef struct qNode{
  28.     Node* data;
  29.     struct qNode *next;
  30. }qNode;
  31.  
  32. typedef struct Queue{
  33.     qNode *head,*tail;
  34.     int numberOfElements;
  35. }Queue;
  36.  
  37. void reset(Queue *q){ q->head = q->tail = NULL; q->numberOfElements=0; }
  38. int isEmpty(Queue *q) { return !q->head; }
  39. void push(Queue *q,Node* val) {
  40.     qNode *node = (qNode*) malloc(sizeof(qNode));
  41.     node->data = val;
  42.     node->next = NULL;
  43.     if(!q->head)
  44.         q->tail = node;
  45.     else
  46.         q->head->next = node;
  47.     q->head = node;
  48.     q->numberOfElements++;
  49. }
  50.  
  51. Node* pop(Queue *q) {
  52.     if(!isEmpty(q)) {
  53.         if(!q->tail)
  54.             return NULL;
  55.         else{
  56.             Node* x = q->tail->data;
  57.             if(!q->tail->next)
  58.                 reset(q);
  59.             else
  60.                 q->tail = q->tail->next;
  61.             q->numberOfElements--;
  62.             return x;
  63.         }
  64.     }
  65.     return NULL;
  66. }
  67.  
  68. void empty(Queue *q){
  69.     while(!isEmpty(q))
  70.         pop(q);
  71. }
  72.  
  73. Node* getFirst(Queue q){
  74.     return pop(&q);
  75. }
  76.  
  77. void display(Queue q){
  78.     while(!isEmpty(&q)) {
  79.         Node * node = pop(&q);
  80.         printf("Name: %-20s,ID: %-4d,%p: \n",node->data.name,node->data.id,node );
  81.     }
  82. }
  83.  
  84. int count(Queue *q) { return q->numberOfElements;}
  85. // END   OF QUEUE STUFF
  86.  
  87. Node * newNode(int id,char *name){
  88.     Node* node = (Node*) malloc(sizeof(Node));
  89.     node->data.id = id;
  90.     strcpy(node->data.name, name);
  91.     node->left = node->right = NULL;
  92.     return node;
  93. }
  94.  
  95. Node * addRight(Node *root,int id,char *name){
  96.     if(root) {
  97.         Node* new = newNode(id,name);
  98.         root->right = new;
  99.         return new;
  100.     }
  101.     return NULL;
  102. }
  103.  
  104. Node * addLeft(Node *root,int id,char *name){
  105.     if(root) {
  106.         Node* new = newNode(id,name);
  107.         root->left = new;
  108.         return new;
  109.     }
  110.     return NULL;
  111. }
  112.  
  113.  
  114. void strtoupper(char *str){
  115.     int i=0; for(i=0;i<strlen(str);i++) str[i] = toupper(str[i]);
  116. }
  117.  
  118. // START OF TRAVERSING FUNCTIONS.
  119. // #PRETRAVERS
  120. void preTravers(Tree *root){
  121.     if(root) {
  122.         printf("Name: %-20s, ID: %d\t%p \n",root->data.name, root->data.id,root);
  123.         preTravers(root->left);
  124.         preTravers(root->right);
  125.     }
  126. }
  127.  
  128. // #POSTTRAVERS
  129. void postTravers(Tree *root){
  130.     if(root) {
  131.         postTravers(root->left);
  132.         postTravers(root->right);
  133.         printf("Name: %-20s, ID: %d\t%p \n",root->data.name, root->data.id,root);
  134.     }
  135. }
  136.  
  137. // #INTRAVERS
  138. void inTravers(Tree *root){
  139.     if(root) {
  140.         inTravers(root->left);
  141.         printf("Name: %-20s, ID: %d\t%p \n",root->data.name, root->data.id,root);
  142.         inTravers(root->right);
  143.     }    
  144. }
  145. // END OF TRAVERSING FUNCTIONS.
  146.  
  147.  
  148. // START FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  149. // #HASCHILD
  150. int hasChild(Node *node){
  151.     return node->right || node->left;
  152. }
  153. // END   FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  154.  
  155. // #SAVETOFILE
  156. void saveToFile(Tree *root,FILE *fp){
  157.     if(root) {
  158.         saveToFile(root->left,fp);
  159.         saveToFile(root->right,fp);
  160.         fprintf(fp,"%s,%d",root->data.name,root->data.id); fprintf(fp, "\n");
  161.     }
  162. }
  163.  
  164. // START SHOWING FUNCTIONS.
  165. // #INSHOW
  166. void inShow(Tree *root,int currentDepth){
  167.     if(root && data(root)!=~(1<<31)) {
  168.         inShow(root->right,currentDepth+1);
  169.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  170.         printf("Name: %s,ID: %3d::%p ",root->data.name,root->data.id,root);
  171.         inShow(root->left,currentDepth+1);
  172.     }else
  173.         printf("\n");
  174. }
  175. // END SHOWING FUNCTIONS.
  176.  
  177. // START OF INSERTING IN NST FUNCTION.
  178. // #INSERTINID
  179. void insertInId(Tree *root,int id,char *name){
  180.     Node * node = newNode(id,name);
  181.     if(root) {
  182.         if(id < root->data.id ){
  183.             // GO LEFT
  184.             // if there is no left node
  185.             if(!root->left)
  186.                 root->left = node;
  187.             else // there is a node insert to it
  188.                 insertInId(root->left,id,name);
  189.         }else{
  190.             // GO RIGHT
  191.             // if there is no right node
  192.             if(!root->right)
  193.                 root->right = node;
  194.             else
  195.                 insertInId(root->right,id,name);
  196.         }
  197.     }
  198.     // return node;
  199. }
  200.  
  201. // #INSERTINNAME
  202. void insertInName(Tree *root,int id,char *name){
  203.     Node *node = newNode(id,name);
  204.     if(root){
  205.         char fname[strlen(name)], froot[strlen(root->data.name)];
  206.         strcpy(fname,name); strcpy(froot,root->data.name);
  207.         strtoupper(fname); strtoupper(froot);
  208.         if(strcmp(froot,fname) > 0){ // root->data.name is larger than name
  209.             if(!root->left)
  210.                 root->left = node;
  211.             else
  212.                 insertInName(root->left,id,name);
  213.         }else{
  214.             if(!root->right)
  215.                 root->right = node;
  216.             else
  217.                 insertInName(root->right,id,name);
  218.         }
  219.     }
  220. }
  221. // END   OF INSERTING IN BST FUNCTION.
  222.  
  223. // START OF FUNCTION TO GET SMALLEST ELEMENT.
  224. // #GETSMALLEST
  225. Node * getSmallest(Tree *root){
  226.     if(root->left || root->right) {
  227.         if(root->left) return getSmallest(root->left);
  228.         if(root->right && !root->left) return root;
  229.     }
  230.     return root;
  231.  
  232. }
  233. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  234.  
  235. Queue q; int dummy=0;
  236.  
  237. // START OF SEARCHING IN BST FUNCTION.
  238. // #SEARCHBYID
  239. Node * searchById (Tree *root,int id){
  240.     if (!root)
  241.         return NULL;
  242.     if (root->data.id == id)
  243.         return root;
  244.     if (id < root->data.id)
  245.         return searchById(root->left,id);
  246.     else
  247.         return searchById(root->right,id);
  248. }
  249.  
  250. // #SEARCHBYNAME
  251. Node * searchByName (Tree *root,char *name,int id){
  252.     char fname[strlen(name)], froot[strlen(root->data.name)];
  253.     strcpy(fname,name); strcpy(froot,root->data.name);
  254.     strtoupper(fname); strtoupper(froot);
  255.     if (!root)
  256.         return NULL;
  257.     if(strstr(froot,fname)){
  258.         // printf("%-20s:%-4d:%p\n",root->data.name,data(root),root );
  259.         if(id!=-1 && ((root->data.id)==id)){
  260.             empty(&q);
  261.             dummy = 1;
  262.             push(&q,root);
  263.             return root;
  264.             exit(0);
  265.         }else if(!dummy)
  266.             push(&q,root);
  267.         if(root->left)
  268.             searchByName(root->left,name,id);
  269.         if(root->right)
  270.             searchByName(root->right,name,id);
  271.     }else{
  272.         /*if(strcmp(froot,fname) < 0){ // root->data.name is larger than name (go right)
  273.             // printf("comparing: %s,%s...go right\n",froot,fname);
  274.             if(root->left)
  275.                 return searchByName(root->left,name,id);
  276.         }else if(strcmp(froot,fname) > 0){
  277.             // printf("comparing: %s,%s...go left\n",froot,fname);
  278.             if(root->right)
  279.                 return searchByName(root->right,name,id);
  280.         }*/
  281.             if(root->right)
  282.                 searchByName(root->right,name,id);
  283.             if(root->left)
  284.                 searchByName(root->left,name,id);
  285.     }
  286.     return NULL;
  287. }
  288.  
  289. // END   OF SEARCHING IN BST FUNCTION.
  290.  
  291. // START OF FUNCTION TO GET PARENT OF NODE.
  292. // #GETPARENT
  293. Node * getParent(Tree *root,Node *node){
  294.     if(root) {
  295.         if(root->left == node || root->right == node) {
  296.             return root;
  297.         }else{
  298.             if(node->data.id < root->data.id)
  299.                 root = root->left;
  300.             else
  301.                 root = root->right;
  302.             return getParent(root,node);
  303.         }
  304.     }
  305.     return NULL;
  306. }
  307.  
  308. char * name(Node* node){
  309.     strtoupper(node->data.name);
  310.     return (node->data.name);
  311. }
  312.  
  313.  
  314. // #fast
  315. Node * getParentName(Tree *root,Node *node){
  316.     if(root) {
  317.         if( root->left == node || root->right == node)
  318.             return root;
  319.         else{
  320.             char fname[strlen(root->data.name)]; strcpy(fname,root->data.name) ;
  321.             char lname[strlen(node->data.name)]; strcpy(lname,node->data.name);
  322.             strtoupper(fname); strtoupper(lname);
  323.             if(strcmp(fname,lname) < 0 ){
  324.                 root = root->right;
  325.             }else{
  326.                 root = root->left;
  327.             }
  328.             return getParentName(root,node);
  329.         }
  330.     }
  331.     return NULL;
  332. }
  333. // END   OF FUNCTION TO GET PARENT OF NODE.
  334.  
  335. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  336. // #GETMAX
  337. Node * getMax(Tree *root){
  338.     return !root->right ? root : getMax(root->right);
  339. }
  340. // #GETMIN
  341. Node * getMin(Tree *root){
  342.     return !root->left  ? root : getMin(root->left);
  343. }
  344.  
  345. Node * getMaxName(Tree *root){
  346.     return !root->right ? root : getMaxName(root->right);
  347. }
  348. Node * getMinName(Tree *root){
  349.     return !root->left ? root : getMinName(root->left);
  350. }
  351.  
  352. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  353.  
  354. // START OF DELETING FROM BST FUNCTION.
  355. // #DELETE
  356. void delete(Tree ** root, Node * parent, Node * node){
  357.     if(!parent && node) {
  358.         if(!node->left && !node->right){ // Not a single child  
  359.             (*root)->data.id = ~(1<<31);
  360.         }else if(!node->left && node->right){ // only RIGHT child
  361.             Node *minNode = getMin((*root)->right); Node *parentMinNode = getParent((*root),minNode);
  362.             (parentMinNode->left == minNode) ? (parentMinNode->left = minNode->right) : (parentMinNode->right =  minNode->right);
  363.             minNode->right = (*root)->right;
  364.             (*root) = minNode;
  365.         } else if(node->left && !node->right){ // only LEFT child
  366.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  367.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = maxNode->left) : (parentMaxNode->right = maxNode->left);
  368.             maxNode->left = (*root)->left;
  369.             (*root) = maxNode;
  370.         } else if(node->left && node->right){ // have BOTH children
  371.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  372.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  373.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  374.             (*root) = maxNode;
  375.         }
  376.         return ;
  377.     }else if(parent && node){
  378.         if(!node->left && !node->right){ // Not a single child
  379.             (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  380.         }else if(!node->left && node->right){ // only RIGHT child
  381.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  382.         }else if(node->left && !node->right){ // only LEFT child
  383.             (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  384.         }else if(node->left && node->right){ // have BOTH children
  385.             Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent((*root),maxNode);
  386.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  387.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  388.             maxNode->left = node->left; maxNode->right = node->right;
  389.         }
  390.         free(node);
  391.     }
  392. }
  393.  
  394. void deleteFromName(Tree **root,Node *parent,Node * node){
  395.     if(!parent && node) {
  396.         if(!node->left && !node->right) {
  397.             (*root)->data.id = ~(1<<31);
  398.         }else if(!node->left && node->right){
  399.             Node *minNode = getMinName((*root)->right);
  400.             Node *parentMinNode = getParentName((*root),minNode);
  401.             (parentMinNode->left == minNode) ? (parentMinNode->left = minNode->right) : (parentMinNode->right =  minNode->right);
  402.             minNode->right = (*root)->right;
  403.             (*root) = minNode;
  404.         }else if(node->left && !node->right){
  405.             Node *maxNode = getMaxName((*root)->left);
  406.             Node * parentMaxNode = getParentName((*root),maxNode);
  407.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = maxNode->left) : (parentMaxNode->right = maxNode->left);
  408.             maxNode->left = (*root)->left;
  409.             (*root) = maxNode;
  410.         } else if(node->left && node->right){
  411.             Node *maxNode = getMaxName((*root)->left);
  412.             Node * parentMaxNode = getParentName((*root),maxNode);
  413.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  414.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  415.             (*root) = maxNode;
  416.         }
  417.         return ;
  418.     }else if(parent && node) {
  419.         if(!node->left && !node->right) {
  420.             (parent->left == node ) ? (parent->left=NULL) : (parent->right=NULL);
  421.         }else if(!node->left && node->right){
  422.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right);
  423.         } else if(node->left && !node->right){
  424.             (parent->left == node ) ? (parent->left = node->left) : (parent->right = node->left);
  425.         }else if(node->left && node->right){
  426.             Node * maxNode = getMaxName((*root)->left);
  427.             Node * parentMaxNode = getParentName((*root),maxNode);
  428.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);            
  429.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  430.             maxNode->left = node->left; maxNode->right = node->right;
  431.         }
  432.         free(node);
  433.     }
  434. }
  435. // END   OF DELETING FROM BST FUNCTION.
  436.  
  437. // START OF FUNCTION THAT LOADS NAMES FROM FILE
  438. // #LOADIDTREE
  439. void loadIdTree(Tree **root){
  440.     char line[50]; int id;
  441.     FILE *fp = fopen("students_info.txt", "r");
  442.     while (fgets(line,50,fp)){
  443.         char name[40];
  444.         strcpy(name,strtok(line,","));
  445.         id = atoi(strtok(NULL,"\n")); // convert string to number
  446.         (!*root) ? *root = (newNode(id,name)) : (insertInId((*root),id,name));
  447.     }
  448.     fclose(fp);
  449. }
  450.  
  451. // #LOADNAMETREE
  452. void loadNameTree(Tree **root){
  453.     char line[50]; int id;
  454.     FILE *fp = fopen("students_info.txt", "r");
  455.     while (fgets(line,50,fp)){
  456.         char name[40];
  457.         strcpy(name,strtok(line,","));
  458.         id = atoi(strtok(NULL,"\n")); // convert string to number
  459.         (!*root) ? *root = (newNode(id,name)) : (insertInName((*root),id,name));
  460.     }
  461.     fclose(fp);
  462. }
  463. // END   OF FUNCTION THAT LOADS NAMES FROM FILE
  464.  
  465. void displaySortedTree(Tree *root){ inTravers(root); }
  466.  
  467. void edit(Tree **idTree,Tree **nameTree,Node * idNode,Node *nameNode,char *name,int id){
  468.     deleteFromName(nameTree,getParentName(*nameTree,nameNode),nameNode);
  469.     delete(idTree,getParent(*idTree,idNode),idNode);
  470.     insertInId(*idTree,id,name);
  471.     insertInName(*nameTree,id,name);
  472. }
  473.  
  474. Tree *idTree=NULL ,*nameTree=NULL;
  475. int main(){
  476.     reset(&q);
  477.     char menu[2];
  478.     do{
  479.         system("cls");
  480.         printf("1.Load Students info (from file).\n");
  481.         printf("2.Add new student.\n");
  482.         printf("3.Update student info.\n");
  483.         printf("4.Remove student.\n");
  484.         printf("5.Search for a student.\n");
  485.         printf("6.Save what you did in file.\n");
  486.         printf("7.Show all info (inOrder).\n");
  487.         printf("8.Show all info (preOrder).\n");
  488.         printf("9.Show all info (postOrder).\n");
  489.         printf("10.Show all info (TreeShape).\n");
  490.         printf("11.Exit\n");
  491.         printf("\n\nChoice: "); scanf("%s",menu);
  492.     }while(atoi(menu)<=0 || atoi(menu)>=12);
  493.     int choice = atoi(menu);
  494.  
  495.     system("cls");
  496.     switch(choice){
  497.         case 1:
  498.             printf("Loading from file...\n");
  499.             loadNameTree(&nameTree); loadIdTree(&idTree);
  500.             printf("Loaded successfully.\n");
  501.             system("pause");
  502.             break;
  503.         case 2:
  504.             if(!idTree || !nameTree){
  505.                 printf("Error ! , you didn't load the info first.\n");
  506.                 system("pause");
  507.                 printf("Importing data for you.\n");
  508.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  509.                 printf("\n\nLoaded successfully.\n");
  510.             }
  511.             printf("Adding student:\n");
  512.             printf("Enter his name: "); char name[40]; getchar(); gets(name);
  513.             char idString[10];
  514.             do{
  515.                 printf("Enter valid id: "); scanf("%s",idString);
  516.             }while(atoi(idString)<=0);
  517.             int id= atoi(idString);
  518.             insertInName(idTree,id,name);
  519.             insertInName(nameTree,id,name);
  520.             printf("Successfully added\n");
  521.             inTravers(nameTree);
  522.             system("pause");
  523.             break;
  524.         case 3:
  525.             if(!idTree || !nameTree){
  526.                 printf("Error ! , you didn't load the info first.\n");
  527.                 system("pause");
  528.                 printf("Importing data for you.\n");
  529.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  530.                 printf("\n\nLoaded successfully.\n\n");
  531.             }
  532.             printf("Updating student info : \n");
  533.             printf("How do you want to search ?\n");
  534.             printf("1.By ID.\n");
  535.             printf("2.By Name.\n");
  536.             printf("Choice: "); scanf("%s",menu);
  537.             if(atoi(menu)==1) { // search by id
  538.                 char idString[10];
  539.                 do{
  540.                     printf("Enter valid id : "); scanf("%s",idString);
  541.                 }while(atoi(idString)<=0);
  542.                 int id = atoi(idString);
  543.                 Node * idNode = searchById(idTree,id);
  544.                 if(idNode){
  545.                     printf("Name: %-20s,ID: %-4d,%p: \n\n",idNode->data.name,idNode->data.id,idNode );
  546.                     // i have the node
  547.                     // go edit it , but first get node of name tree
  548.                     printf("Enter his new name: "); char newname[40]; getchar(); gets(newname);
  549.                     char newidString[10];
  550.                     do{
  551.                         printf("Enter his new valid id: "); scanf("%s",newidString);
  552.                     }while(atoi(newidString)<=0);
  553.                     int newid = atoi(newidString);
  554.                     searchByName(nameTree,"",id);
  555.                     Node *namenode = pop(&q);
  556.                     edit(&idTree,&nameTree,idNode,namenode,newname,newid);
  557.                 }else
  558.                     printf("No search results !\n");
  559.             }else if(atoi(menu)==2){
  560.                 char name[40];
  561.                 printf("Enter valid name : "); scanf("%s",name);
  562.                 searchByName(nameTree,name,-1);
  563.                 if(!isEmpty(&q)){
  564.                     display(q);
  565.                     printf("Choose specific id.\n");
  566.                     char newidString[10];
  567.                     do{
  568.                         printf("Enter valid id you want to edit : "); scanf("%s",newidString);
  569.                     }while(atoi(newidString)<=0);
  570.                     int id = atoi(newidString);
  571.                     searchByName(nameTree,name,id);
  572.                     if(!isEmpty(&q)) {
  573.                         Node *node = pop(&q);
  574.                         printf("Name: %-20s,ID: %-4d,%p: \n\n",node->data.name,node->data.id,node );
  575.                         char newname[40];
  576.                         printf("Enter new name: "); getchar(); gets(newname);
  577.                         char newidString[10];
  578.                         Node * idnode = searchById(idTree,id);
  579.                         do{
  580.                             printf("Enter valid id: "); scanf("%s",newidString);
  581.                         }while(atoi(newidString)<=0);
  582.                         int newid = atoi(newidString);
  583.                         edit(&idTree,&nameTree,idnode,node,newname,newid);
  584.                     }else
  585.                         printf("No search results !\n");
  586.                 }
  587.                 else
  588.                     printf("No search results\n");
  589.             }
  590.             inTravers(idTree);
  591.             system("pause");
  592.             break;
  593.         case 5:
  594.             if(!idTree || !nameTree){
  595.                 printf("Error ! , you didn't load the info first.\n");
  596.                 system("pause");
  597.                 printf("Importing data for you.\n");
  598.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  599.                 printf("\n\nLoaded successfully.\n");
  600.             }
  601.             printf("How do you want to search ?\n");
  602.             printf("1.By ID.\n");
  603.             printf("2.By Name.\n");
  604.             printf("Choice: "); scanf("%s",menu);
  605.             if(atoi(menu)==1) {
  606.                 char idString[10];
  607.                 do{
  608.                     printf("Enter valid id : "); scanf("%s",idString);
  609.                 }while(atoi(idString)<=0);
  610.                 int id = atoi(idString);
  611.                 Node * node = searchById(idTree,id);
  612.                 if(node)
  613.                     printf("Name: %-20s,ID: %-4d,%p: \n",node->data.name,node->data.id,node );
  614.                 else
  615.                     printf("No search results !\n");
  616.             }else if(atoi(menu)==2){
  617.                 char name[40];
  618.                 printf("Enter valid name : "); scanf("%s",name);
  619.                 searchByName(nameTree,name,-1);
  620.                 if(!isEmpty(&q))
  621.                     display(q);
  622.                 else
  623.                     printf("No search results\n");
  624.             }
  625.             system("pause");
  626.             break;
  627.         case 6:
  628.             printf("Saving changes..\n");
  629.             FILE *fp = fopen("students_info.txt","w");
  630.             saveToFile(idTree,fp);
  631.             fclose(fp);
  632.             printf("Saved successfully.\n");
  633.             system("pause");
  634.             break;
  635.         case 7:
  636.             if(!idTree || !nameTree){
  637.                 printf("Error ! , you didn't load the info first.\n");
  638.                 system("pause");
  639.                 printf("Importing data for you.\n");
  640.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  641.                 printf("\n\nLoaded successfully.\n");
  642.             }
  643.             printf("In which way you want it to be sorted ? \n");
  644.             printf("1.Sorted By ID.\n");
  645.             printf("2.Sorted By Name.\n");
  646.             printf("choice: "); scanf("%s",menu);
  647.             printf("%d\n", atoi(menu));
  648.             atoi(menu)==1 ? inTravers(idTree) : inTravers(nameTree);
  649.             system("pause");
  650.             break;
  651.         case 8:
  652.             if(!idTree || !nameTree){
  653.                 printf("Error ! , you didn't load the info first.\n");
  654.                 system("pause");
  655.                 printf("Importing data for you.\n");
  656.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  657.                 printf("\n\nLoaded successfully.\n");
  658.             }
  659.             printf("In which way you want it to be sorted ? \n");
  660.             printf("1.Sorted By ID.\n");
  661.             printf("2.Sorted By Name.\n");
  662.             printf("choice: "); scanf("%s",menu);
  663.             printf("%d\n", atoi(menu));
  664.             atoi(menu)==1 ? preTravers(idTree) : preTravers(nameTree);
  665.             system("pause");
  666.             break;
  667.         case 9:
  668.             if(!idTree || !nameTree){
  669.                 printf("Error ! , you didn't load the info first.\n");
  670.                 system("pause");
  671.                 printf("Importing data for you.\n");
  672.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  673.                 printf("\n\nLoaded successfully.\n");
  674.             }
  675.             printf("In which way you want it to be sorted ? \n");
  676.             printf("1.Sorted By ID.\n");
  677.             printf("2.Sorted By Name.\n");
  678.             printf("choice: "); scanf("%s",menu);
  679.             printf("%d\n", atoi(menu));
  680.             atoi(menu)==1 ? postTravers(idTree) : postTravers(nameTree);
  681.             system("pause");
  682.             break;
  683.         case 10:
  684.             if(!idTree || !nameTree){
  685.                 printf("Error ! , you didn't load the info first.\n");
  686.                 system("pause");
  687.                 printf("Importing data for you.\n");
  688.                 loadNameTree(&nameTree); loadIdTree(&idTree);
  689.                 printf("\n\nLoaded successfully.\n");
  690.             }
  691.             printf("In which way you want it to be sorted ? \n");
  692.             printf("1.Sorted By ID.\n");
  693.             printf("2.Sorted By Name.\n");
  694.             printf("choice: "); scanf("%s",menu);
  695.             printf("%d\n", atoi(menu));
  696.             atoi(menu)==1 ? inShow(idTree,0) : inShow(nameTree,0);
  697.             system("pause");
  698.             break;
  699.         case 11:
  700.             exit(0);
  701.             break;
  702.  
  703.     }
  704.  
  705.     // main();
  706.     return 0;
  707. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement