Guest User

Untitled

a guest
May 18th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>.
  6. #include <windows.h>
  7.  
  8. struct node
  9. {
  10.     char name[50];
  11.     int id;
  12.     struct node *right;
  13.     struct node *left;
  14. }*name_root,*id_root;
  15.  
  16. struct Search
  17. {
  18.     struct node *dup_x;
  19.     struct Search *next;
  20. }*root;
  21. struct node* del_abdo(struct node**,int);
  22. struct Search * Insert_tail(struct node *ptr);
  23. struct Search * Find_Search_results(char *N,int I);
  24. int Count_nodes();
  25. int Search_Dup(struct node *id_ptr,struct node *name_ptr,char *N,int I,int c);
  26. void print_list (void);
  27. bool Save(struct node*temp);
  28. struct node *create_node(char *N,int I);
  29. struct node* Insert(struct node**,struct node**,char *,int ,int );
  30. bool load (struct node **id_ptr,struct node**name_ptr);
  31. void Print_inorder(struct node *);
  32. void Print_postorder(struct node *);
  33. void Print_preorder(struct node *);
  34. struct node *Find_min(struct node *id_ptr,struct node*name_ptr,int c);
  35. struct node* Find(struct node* id_ptr,struct node*name_ptr,char * N,int I,int c);
  36. struct node *Delete(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c);
  37. struct node * Delete_Name_AND_ID(struct node **name_ptr,char *N,int I);
  38.  
  39. int main()
  40. {
  41.     printf("\n************************************\nWelcome to Students data Organizer !\n************************************\n");
  42.     char C_temp[100];
  43.     int c;
  44.     int c_sub;
  45.     print_list();
  46.     int save_indic=0;
  47.     name_root=NULL;
  48.     root=NULL;
  49.     id_root=NULL;
  50.     int bol;
  51.     char name_temp[100];
  52.     int id_temp;
  53.     do
  54.     {
  55.         printf("\nChoice [Main Menu] :");
  56.         fgets(C_temp, sizeof (C_temp), stdin);
  57.         sscanf(C_temp, "%d", &c);
  58.         switch (c)
  59.         {
  60.         case 0:
  61.             system("cls");
  62.             print_list();
  63.             break;
  64.         case 1:
  65.             system("cls");
  66.             printf("\nLoad\n****\n");
  67.             bol=load(&id_root,&name_root);
  68.             if (bol==1)
  69.             {
  70.                 printf("\nFile loaded Successfully !\n");
  71.                 break;
  72.             }
  73.             else if (bol==0)
  74.             {
  75.                 printf("File could not be opened, Try Again.\n");
  76.                 break;
  77.             }
  78.             break;
  79.         case 2:
  80.             system("cls");
  81.             printf("\nInsert\n******\n");
  82.             printf("\nStudent name :");
  83.             fgets(name_temp,sizeof(name_temp)-1,stdin);
  84.             name_temp[strlen(name_temp)-1]='\0';
  85.             printf("\nStudent ID :");
  86.             fgets(C_temp, sizeof (C_temp), stdin);
  87.             sscanf(C_temp, "%d", &id_temp);
  88.             Insert(&id_root,&name_root,name_temp,id_temp,0);
  89.             Insert(&id_root,&name_root,name_temp,id_temp,1);
  90.             printf("\nAdded Successfully !\n");
  91.             break;
  92.         case 3:
  93.             system("cls");
  94.             root=NULL;
  95.             printf("\nSearch\n******\n");
  96.             int Count;
  97.             printf("\nSearch \n******\n");
  98.             printf("\n[1] By ID\n[2] By Name\nChoice [Search] :");
  99.             fgets(C_temp, sizeof (C_temp), stdin);
  100.             sscanf(C_temp, "%d", &c_sub);
  101.             if (c_sub==1)
  102.             {
  103.                 printf("\nStudent ID :");
  104.                 fgets(C_temp, sizeof (C_temp), stdin);
  105.                 sscanf(C_temp, "%d", &id_temp);
  106.                 Search_Dup(id_root,NULL,NULL,id_temp,0);
  107.                 if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  108.                 Print_Results();
  109.             }
  110.             else if (c_sub==2)
  111.             {
  112.                 printf("\nStudent Name :");
  113.                 fgets(name_temp,sizeof(name_temp)-1,stdin);
  114.                 name_temp[strlen(name_temp)-1]='\0';
  115.                 Search_Dup(NULL,name_root,name_temp,NULL,1);
  116.                 if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  117.                 Print_Results();
  118.             }
  119.             else
  120.             {
  121.                 printf("\nWrong Entry, Try Again.\n");
  122.                 break;
  123.             }
  124.  
  125.             break;
  126.         case 4:
  127.             system("cls");
  128.             printf("\nDelete\n******\n");
  129.             root=NULL;
  130.             printf("Choices :\n[1] By ID\n[2] By Name\n");
  131.             printf("\n\nChoice [D] :");
  132.             fgets(C_temp, sizeof (C_temp), stdin);
  133.             sscanf(C_temp, "%d", &c_sub);
  134.             if (c_sub==1)
  135.             {
  136.                 printf("\nStudent ID :");
  137.                 char temp_x[50];
  138.                 fgets(C_temp, sizeof (C_temp), stdin);
  139.                 sscanf(C_temp, "%d", &id_temp);
  140.                 Search_Dup(id_root,NULL,NULL,id_temp,0);
  141.                 if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  142.                 Print_Results();
  143.                 if (Count_nodes()==0); //msh la2y
  144.                 else if (Count_nodes()==1) //la2et wa7d bas
  145.                 {
  146.                     strcpy(temp_x,(root->dup_x)->name);
  147.                     printf("\nDelete Successfully !\n");
  148. //                    Delete(&id_root,NULL,NULL,id_temp,0);
  149. //                    Delete(NULL,&name_root,temp_x,NULL,1);
  150.                     del_abdo(&(id_root),(root->dup_x)->id);
  151.                     printf("%s",temp_x);
  152.                     break;
  153.                 }
  154.             }
  155.             else if (c_sub==2)
  156.             {
  157.  
  158.                 printf("\nStudent Name :");
  159.                 struct node*temp_search=NULL;
  160.                 fgets(name_temp,sizeof(name_temp)-1,stdin);
  161.                 name_temp[strlen(name_temp)-1]='\0';
  162.                 temp_search=Find(NULL,name_root,name_temp,NULL,1);
  163.                 int temp_x=temp_search->id;
  164.                 if (temp_search)
  165.                 {
  166.                     printf("\nFound: Delete Successfully !\n");
  167.                    // Delete(NULL,&name_root,name_temp,NULL,1);
  168.                    // Delete(&id_root,NULL,NULL,temp_x,0);
  169.                    del_abdo(&(id_root),temp_x);
  170.                     break;
  171.                 }
  172.                 else
  173.                 {
  174.                     printf("\nNot Found !\n");
  175.                     break;
  176.                 }
  177.             }
  178.             else
  179.             {
  180.                 printf("\nWrong Entry, Try Again.\n");
  181.                 break;
  182.             }
  183.             break;
  184.         case 5:
  185.             system("cls");
  186.             printf("\Search\n*******\n");
  187.             int x=10;
  188.             Insert_tail(id_root);
  189.             printf("\n\n");
  190.             Print_Results();
  191.             break;
  192.         case 6:
  193.             system("cls");
  194.             printf("\nDisplay\n*******\n");
  195.             if (id_root)
  196.             {
  197.                 printf("Choices :\n[1] Pre-order\n[2] Post-order\n[3] In-order\n");
  198.                 printf("\n\nChoice [Display] :");
  199.                 fgets(C_temp, sizeof (C_temp), stdin);
  200.                 sscanf(C_temp, "%d", &c_sub);
  201.                 if (c_sub==1)
  202.                 {
  203.                     printf("\n%24s\n%24s","According to ID","***************");
  204.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  205.                     Print_preorder(id_root);
  206.                     printf("\n==================================\n");
  207.                     printf("\n%25s\n%25s","According to Name","*****************");
  208.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  209.                     Print_preorder(name_root);
  210.                 }
  211.                 else if (c_sub==2)
  212.                 {
  213.                     printf("\n%24s\n%24s","According to ID","***************");
  214.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  215.                     Print_postorder(id_root);
  216.                     printf("\n==================================\n");
  217.                     printf("\n%25s\n%25s","According to Name","*****************");
  218.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  219.                     Print_postorder(name_root);
  220.                 }
  221.                 else if (c_sub==3)
  222.                 {
  223.                     printf("\n%24s\n%24s","According to ID","***************");
  224.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  225.                     Print_inorder(id_root);
  226.                     printf("\n==================================\n");
  227.                     printf("\n%25s\n%25s","According to Name","*****************");
  228.                     printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
  229.                     Print_inorder(name_root);
  230.                 }
  231.                 else
  232.                 {
  233.                     printf("\nWrong Entry, Try Again.\n");
  234.                     break;
  235.                 }
  236.                 break;
  237.             }
  238.             else
  239.             {
  240.                 printf("List is empty !\n");
  241.                 break;
  242.             }
  243.         case 7 :
  244. here1:
  245.             system("cls");
  246.             printf("\nSave\n****\n");
  247.             save_indic=Save(name_root);
  248.             if (bol)
  249.             {
  250.                 printf("\nSaved Successfully. \n");
  251.                 break;
  252.             }
  253.             else
  254.                 printf("\nCouldn't save to file, Try Again. \n");
  255.             break;
  256.         case 8:
  257.             system("cls");
  258.             printf("\nExit\n****\n");
  259.             if (!save_indic) printf("\nAre you sure you want to Exit without saveing changes ?\n[1] Yes\n[2] No, save now\n");
  260.             fgets(C_temp, sizeof (C_temp), stdin);
  261.             sscanf(C_temp, "%d", &c_sub);
  262.             if (c_sub==1) break;
  263.             else  goto here1;
  264.             break;
  265.         default :
  266.             printf("\nTry agian !\n");
  267.             fflush(stdout);
  268.             fflush(stdin);
  269.             break;
  270.         }
  271.     }
  272.     while (c!=8);
  273.     return 0;
  274. }
  275.  
  276. struct node* del_abdo(struct node** bst1,int I){
  277.     struct node *tmp=NULL;
  278.         if((*bst1)==NULL);
  279.         else if((*bst1)->id > I)(*bst1)->left=del_abdo(&(*bst1)->left,I);
  280.         else if((*bst1)->id < I)(*bst1)->right=del_abdo(&(*bst1)->right,I);
  281.         else if((*bst1)->id == I){
  282.             if((*bst1)->left==NULL && (*bst1)->right==NULL)(*bst1)=NULL;
  283.             else if((*bst1)->left==NULL)(*bst1)=(*bst1)->right;
  284.             else if((*bst1)->right==NULL)(*bst1)=(*bst1)->left;
  285.             else {
  286.                 tmp=Find_min((*bst1)->right,NULL,0);
  287.                 (*bst1)->id=tmp->id;
  288.                 strcpy((*bst1)->name,tmp->name);
  289.                 (*bst1)->right=del_abdo(&(*bst1)->right,tmp->id);
  290.             }
  291.         }
  292.         return (*bst1);
  293. }
  294. struct Search * Find_Search_results(char *N,int I)
  295. {
  296.     struct Search *current=root;
  297.  
  298.     if (root==NULL) return NULL;
  299.     while (current!=NULL)
  300.     {
  301.         if ((current->dup_x)->id==I) return current;
  302.         current=current->next;
  303.     }
  304.  
  305. }
  306.  
  307. void print_list (void)
  308. {
  309.     printf("\nChoices\n*******\n\n[1] Load a file \n[2] Insert a student Entry \n[3] Search for a student Entry\n[4] Delete a student Entry\n[5] GARB \n[6] Display Student Entries\n[7] Save Changes to File \n[8] Exit\nEnter 0 to reprint the list\n");
  310. }
  311.  
  312. struct node *create_node(char *N,int I)
  313. {
  314.     struct node *temp=(struct node*)malloc(sizeof(struct node));
  315.     strcpy(temp->name,N);
  316.     temp->id=I;
  317.     temp->left=NULL;
  318.     temp->right=NULL;
  319.     return temp;
  320. }
  321.  
  322. struct node* Insert(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c)
  323. {
  324.     if (c==0)
  325.     {
  326.         if (!(*id_ptr))
  327.         {
  328.             *id_ptr=create_node(N,I);
  329.         }
  330.         else if ((*id_ptr)->id > I) (*id_ptr)->left=Insert(&(*id_ptr)->left,&(*name_ptr),N,I,0);
  331.         else (*id_ptr)->right=Insert(&(*id_ptr)->right,&(*name_ptr),N,I,0);
  332.         return *id_ptr;
  333.     }
  334.     else
  335.     {
  336.  
  337.         if (!(*name_ptr)) (*name_ptr)=create_node(N,I);
  338.         else if (strcasecmp(N,(*name_ptr)->name)<=0) (*name_ptr)->left=Insert(NULL,&(*name_ptr)->left,N,I,1);
  339.         else (*name_ptr)->right=Insert(NULL,&(*name_ptr)->right,N,I,1);
  340.         return *name_ptr;
  341.     }
  342.  
  343. }
  344.  
  345. struct node *Delete(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c)
  346. {
  347.     if (c==0)
  348.     {
  349.         struct node *temp_node;
  350.         if ((*id_ptr)==NULL);
  351.         else if ((*id_ptr)->id>I)
  352.         {
  353.             (*id_ptr)->left=Delete(&(*id_ptr)->left,&(*name_ptr),N,I,c);
  354.         }
  355.         else if (I>(*id_ptr)->id)
  356.         {
  357.             (*id_ptr)->right=Delete(&(*id_ptr)->right,&(*name_ptr),N,I,c);
  358.         }
  359.         else if (I==(*id_ptr)->id)
  360.         {
  361.             if ((*id_ptr)->left==NULL&&(*id_ptr)->right==NULL)
  362.             {
  363.                 free((*id_ptr));
  364.                 (*id_ptr)=NULL;
  365.  
  366.                //return (*id_ptr);
  367.             }
  368.             else if ((*id_ptr)->left==NULL)
  369.             {
  370.                 temp_node=(*id_ptr);
  371.                 (*id_ptr)=(*id_ptr)->right;
  372.                 free(temp_node);
  373.             }
  374.             else if ((*id_ptr)->right==NULL)
  375.             {
  376.                 temp_node=(*id_ptr);
  377.                 (*id_ptr)=(*id_ptr)->left;
  378.                 free(temp_node);
  379.             }
  380.             else
  381.             {
  382.                 temp_node=Find_min((*id_ptr)->right,NULL,0);
  383.                 (*id_ptr)->id=temp_node->id;
  384.                 strcpy((*id_ptr)->name,temp_node->name);
  385.                 (*id_ptr)->right=Delete (&(*id_ptr)->right,&(*name_ptr),NULL,temp_node->id,0);
  386.             }
  387.             return (*id_ptr);
  388.         }
  389.  
  390.     }
  391.  
  392.     else
  393.     {
  394.         struct node *temp_node;
  395.         if ((*name_ptr)==NULL);
  396.         else if (strcasecmp(N,(*name_ptr)->name)<0)
  397.         {
  398.             (*name_ptr)->left=Delete(NULL,&(*name_ptr)->left,N,NULL,1);
  399.         }
  400.         else if  (strcasecmp(N,(*name_ptr)->name)>0)
  401.         {
  402.             (*name_ptr)->right=Delete(NULL,&(*name_ptr)->right,N,NULL,1);
  403.         }
  404.         else if (!strcasecmp(N,(*name_ptr)))
  405.         {
  406.             if ((*name_ptr)->left==NULL&&(*name_ptr)->right==NULL)
  407.             {
  408.                 free((*name_ptr));
  409.                 (*name_ptr)=NULL;
  410.  
  411.             }
  412.             else if ((*name_ptr)->left==NULL)
  413.             {
  414.                 temp_node=(*name_ptr);
  415.                 (*name_ptr)=(*name_ptr)->right;
  416.                 free(temp_node);
  417.             }
  418.             else if ((*name_ptr)->right==NULL)
  419.             {
  420.                 temp_node=(*name_ptr);
  421.                 (*name_ptr)=(*name_ptr)->left;
  422.                 free(temp_node);
  423.             }
  424.             else
  425.             {
  426.                 temp_node=Find_min(NULL,(*name_ptr)->right,1);
  427.                 (*name_ptr)->id=temp_node->id;
  428.                 strcpy((*name_ptr)->name,temp_node->name);
  429.                 (*name_ptr)->right=Delete (NULL,&(*name_ptr)->right,temp_node->name,NULL,1);
  430.             }
  431.             return (*name_ptr);
  432.         }
  433.     }
  434.  
  435. }
  436. struct node *Find_min(struct node *id_ptr,struct node*name_ptr,int c)
  437. {
  438.     if (c==0)
  439.     {
  440.         if (id_ptr==NULL)
  441.             return NULL;
  442.         else if (id_ptr->left==NULL)
  443.             return id_ptr;
  444.         else
  445.             return Find_min(id_ptr->left,NULL,0);
  446.     }
  447.     else
  448.     {
  449.         if (name_ptr==NULL)
  450.             return NULL;
  451.         else if (name_ptr->left==NULL)
  452.             return name_ptr;
  453.         else
  454.             return Find_min(NULL,name_ptr->left,1);
  455.     }
  456.  
  457. }
  458.  
  459. bool Save(struct node*temp)
  460. {
  461.     char loc_temp[200];
  462.     char name_temp[50];
  463.     int C3;
  464.     int id_temp;
  465.     FILE *cfPtr=NULL;
  466.  
  467.     void save_to_file(struct node *tempX)
  468.     {
  469.         if (tempX)
  470.         {
  471.             save_to_file(tempX->left);
  472.             fprintf(cfPtr,"%s,%d\n",tempX->name,tempX->id);
  473.             save_to_file(tempX->right);
  474.         }
  475.     }
  476.  
  477.     printf("\nEnter the location of the File or press enter to save to the pre-installed one\n");
  478.     printf("\n\nChoice [Save] :");
  479.     fgets(loc_temp,sizeof(loc_temp)-1,stdin);
  480.     loc_temp[strlen(loc_temp)-1]='\0';
  481.     if (!strcmp(loc_temp,""))
  482.     {
  483.         cfPtr=fopen("New_file.txt","w");
  484.         save_to_file(temp);
  485.         fclose(cfPtr);
  486.         return true;
  487.     }
  488.     else
  489.     {
  490.         cfPtr=fopen(loc_temp,"w");
  491.         if (cfPtr==NULL)return 0;
  492.         save_to_file(temp);
  493.         fclose(cfPtr);
  494.         return true;
  495.     }
  496.  
  497. }
  498.  
  499. bool load (struct node **id_ptr,struct node**name_ptr)
  500. {
  501.     char loc_temp[200];
  502.     char name_temp[50];
  503.     int id_temp;
  504.     int C3;
  505.     FILE *cfPtr;
  506.  
  507.     printf("\nEnter the location of the File or press enter to load the pre-installed one\n");
  508.     printf("\n\nChoice [Load] :");
  509.     fgets(loc_temp,sizeof(loc_temp)-1,stdin);
  510.     loc_temp[strlen(loc_temp)-1]='\0';
  511.     if (!strcmp(loc_temp,""))
  512.     {
  513.         cfPtr=fopen("New_file.txt","r");
  514.         do
  515.         {
  516.             fscanf(cfPtr,"%[^,]s",name_temp);
  517.             fscanf(cfPtr,",%d\n",&id_temp);
  518.             Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,0);
  519.             Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,1);
  520.         }
  521.         while (!feof(cfPtr));
  522.         fclose(cfPtr);
  523.         return true;
  524.     }
  525.     else
  526.     {
  527.         cfPtr=fopen(loc_temp,"r");
  528.         if (cfPtr==NULL)
  529.         {
  530.             return false;
  531.         }
  532.         do
  533.         {
  534.             fscanf(cfPtr,"%[^,]s",name_temp);
  535.             fscanf(cfPtr,",%d\n",&id_temp);
  536.             Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,0);
  537.             Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,1);
  538.         }
  539.         while (!feof(cfPtr));
  540.         fclose(cfPtr);
  541.         return true;
  542.     }
  543. }
  544.  
  545. void Print_preorder(struct node *ptr)
  546. {
  547.     if (ptr)
  548.     {
  549.         printf("\n%-30s%-30d\n",ptr->name,ptr->id);
  550.         Print_preorder(ptr->left);
  551.         Print_preorder(ptr->right);
  552.     }
  553. }
  554. void Print_postorder(struct node *ptr)
  555. {
  556.     if (ptr)
  557.     {
  558.         Print_postorder(ptr->left);
  559.         Print_postorder(ptr->right);
  560.         printf("\n%-30s%-30d\n",ptr->name,ptr->id);
  561.     }
  562. }
  563.  
  564. void Print_inorder(struct node *ptr)
  565. {
  566.     if (ptr)
  567.     {
  568.         Print_inorder(ptr->left);
  569.         printf("\n%-30s%-30d\n",ptr->name,ptr->id);
  570.         Print_inorder(ptr->right);
  571.     }
  572. }
  573.  
  574. int Count_nodes()
  575. {
  576.     int c=0;
  577.     struct Search *current=root;
  578.     while (current!=NULL)
  579.     {
  580.         c++;
  581.         current=current->next;
  582.     }
  583.     return c;
  584. }
  585.  
  586. struct node* Find(struct node* id_ptr,struct node*name_ptr,char * N,int I,int c)
  587. {
  588.     if (c==0)
  589.     {
  590.         if (!id_ptr) return NULL;
  591.         else if (I==id_ptr->id) return id_ptr;
  592.         else if (I<id_ptr->id) return Find(id_ptr->left,NULL,NULL,I,0);
  593.         else  return Find(id_ptr->right,NULL,NULL,I,0);
  594.     }
  595.     else
  596.     {
  597.         if (!name_ptr) return NULL;
  598.         else if (!strcasecmp(N,name_ptr->name)) return name_ptr;
  599.         else if (strcasecmp(N,name_ptr->name)<0) return Find(NULL,name_ptr->left,N,NULL,1);
  600.         else  return Find(NULL,name_ptr->right,N,NULL,1);
  601.     }
  602.  
  603. }
  604.  
  605. struct Search * Insert_tail(struct node *ptr)
  606. {
  607.     struct Search *current=root;
  608.     struct Search *temp=(struct Search *)malloc(sizeof (struct Search ));
  609.     temp->next=NULL;
  610.     temp->dup_x=ptr;
  611.     if (root==NULL)
  612.     {
  613.         root=temp;
  614.     }
  615.     else
  616.     {
  617.         while (current->next != NULL)
  618.         {
  619.             if (current->dup_x==ptr)return NULL;
  620.             else
  621.             {
  622.                 current=current->next;
  623.             }
  624.         }
  625.         if (current->dup_x==ptr)return NULL;
  626.         current->next=temp;
  627.     }
  628. }
  629.  
  630. void Print_Results(void)
  631. {
  632.     struct Search *current=root;
  633.     if (current==NULL) printf("\nNot Found.\n");
  634.     else
  635.     {
  636.         while (current!=NULL)
  637.         {
  638.             printf("\n%-30s%-30d\n",(current->dup_x)->name,(current->dup_x)->id);
  639.             current=current->next;
  640.         }
  641.     }
  642. }
  643.  
  644. int Search_Dup(struct node *id_ptr,struct node *name_ptr,char *N,int I,int c)
  645. {
  646.     if (c==0)
  647.     {
  648.         if (id_ptr)
  649.         {
  650.             if (id_ptr->id==I) Insert_tail(id_ptr);
  651.             Search_Dup (id_ptr->left,NULL,NULL,I,0);
  652.             Search_Dup (id_ptr->right,NULL,NULL,I,0);
  653.         }
  654.     }
  655.     else
  656.     {
  657.         if (name_ptr)
  658.         {
  659.             if (!strcasecmp(name_ptr->name,N))Insert_tail(name_ptr);
  660.             Search_Dup (NULL,name_ptr->left,N,NULL,1);
  661.             Search_Dup (NULL,name_ptr->right,N,NULL,1);
  662.         }
  663.     }
  664. }
  665.  
  666. struct node * Delete_Name_AND_ID(struct node **name_ptr,char *N,int I)
  667. {
  668.     struct node *temp_node;
  669.     if (name_ptr)
  670.     {
  671.         if (!strcasecmp((*name_ptr)->name,N)&&((*name_ptr)->id==I))
  672.         {
  673.             if ((*name_ptr)->left==NULL&&(*name_ptr)->right==NULL)
  674.             {
  675.                 free((*name_ptr));
  676.                 (*name_ptr)=NULL;
  677.             }
  678.             else if ((*name_ptr)->left==NULL)
  679.             {
  680.                 temp_node=(*name_ptr);
  681.                 (*name_ptr)=(*name_ptr)->right;
  682.                 free(temp_node);
  683.             }
  684.             else if ((*name_ptr)->right==NULL)
  685.             {
  686.                 temp_node=(*name_ptr);
  687.                 (*name_ptr)=(*name_ptr)->left;
  688.                 free(temp_node);
  689.             }
  690.             else
  691.             {
  692.                 temp_node=Find_min(NULL,(*name_ptr)->right,1);
  693.                 (*name_ptr)->id=temp_node->id;
  694.                 strcpy((*name_ptr)->name,temp_node->name);
  695.                 (*name_ptr)->right=Delete (NULL,&(*name_ptr)->right,temp_node->name,NULL,1);
  696.             }
  697.             return (*name_ptr);
  698.         }
  699.     }
  700.     Search_Dup (NULL,(*name_ptr)->left,N,NULL,1);
  701.     Search_Dup (NULL,(*name_ptr)->right,N,NULL,1);
  702. }
Advertisement
Add Comment
Please, Sign In to add comment