Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdbool.h>.
- #include <windows.h>
- struct node
- {
- char name[50];
- int id;
- struct node *right;
- struct node *left;
- }*name_root,*id_root;
- struct Search
- {
- struct node *dup_x;
- struct Search *next;
- }*root;
- struct node* del_abdo(struct node**,int);
- struct Search * Insert_tail(struct node *ptr);
- struct Search * Find_Search_results(char *N,int I);
- int Count_nodes();
- int Search_Dup(struct node *id_ptr,struct node *name_ptr,char *N,int I,int c);
- void print_list (void);
- bool Save(struct node*temp);
- struct node *create_node(char *N,int I);
- struct node* Insert(struct node**,struct node**,char *,int ,int );
- bool load (struct node **id_ptr,struct node**name_ptr);
- void Print_inorder(struct node *);
- void Print_postorder(struct node *);
- void Print_preorder(struct node *);
- struct node *Find_min(struct node *id_ptr,struct node*name_ptr,int c);
- struct node* Find(struct node* id_ptr,struct node*name_ptr,char * N,int I,int c);
- struct node *Delete(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c);
- struct node * Delete_Name_AND_ID(struct node **name_ptr,char *N,int I);
- int main()
- {
- printf("\n************************************\nWelcome to Students data Organizer !\n************************************\n");
- char C_temp[100];
- int c;
- int c_sub;
- print_list();
- int save_indic=0;
- name_root=NULL;
- root=NULL;
- id_root=NULL;
- int bol;
- char name_temp[100];
- int id_temp;
- do
- {
- printf("\nChoice [Main Menu] :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &c);
- switch (c)
- {
- case 0:
- system("cls");
- print_list();
- break;
- case 1:
- system("cls");
- printf("\nLoad\n****\n");
- bol=load(&id_root,&name_root);
- if (bol==1)
- {
- printf("\nFile loaded Successfully !\n");
- break;
- }
- else if (bol==0)
- {
- printf("File could not be opened, Try Again.\n");
- break;
- }
- break;
- case 2:
- system("cls");
- printf("\nInsert\n******\n");
- printf("\nStudent name :");
- fgets(name_temp,sizeof(name_temp)-1,stdin);
- name_temp[strlen(name_temp)-1]='\0';
- printf("\nStudent ID :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &id_temp);
- Insert(&id_root,&name_root,name_temp,id_temp,0);
- Insert(&id_root,&name_root,name_temp,id_temp,1);
- printf("\nAdded Successfully !\n");
- break;
- case 3:
- system("cls");
- root=NULL;
- printf("\nSearch\n******\n");
- int Count;
- printf("\nSearch \n******\n");
- printf("\n[1] By ID\n[2] By Name\nChoice [Search] :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &c_sub);
- if (c_sub==1)
- {
- printf("\nStudent ID :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &id_temp);
- Search_Dup(id_root,NULL,NULL,id_temp,0);
- if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_Results();
- }
- else if (c_sub==2)
- {
- printf("\nStudent Name :");
- fgets(name_temp,sizeof(name_temp)-1,stdin);
- name_temp[strlen(name_temp)-1]='\0';
- Search_Dup(NULL,name_root,name_temp,NULL,1);
- if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_Results();
- }
- else
- {
- printf("\nWrong Entry, Try Again.\n");
- break;
- }
- break;
- case 4:
- system("cls");
- printf("\nDelete\n******\n");
- root=NULL;
- printf("Choices :\n[1] By ID\n[2] By Name\n");
- printf("\n\nChoice [D] :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &c_sub);
- if (c_sub==1)
- {
- printf("\nStudent ID :");
- char temp_x[50];
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &id_temp);
- Search_Dup(id_root,NULL,NULL,id_temp,0);
- if (root!=NULL)printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_Results();
- if (Count_nodes()==0); //msh la2y
- else if (Count_nodes()==1) //la2et wa7d bas
- {
- strcpy(temp_x,(root->dup_x)->name);
- printf("\nDelete Successfully !\n");
- // Delete(&id_root,NULL,NULL,id_temp,0);
- // Delete(NULL,&name_root,temp_x,NULL,1);
- del_abdo(&(id_root),(root->dup_x)->id);
- printf("%s",temp_x);
- break;
- }
- }
- else if (c_sub==2)
- {
- printf("\nStudent Name :");
- struct node*temp_search=NULL;
- fgets(name_temp,sizeof(name_temp)-1,stdin);
- name_temp[strlen(name_temp)-1]='\0';
- temp_search=Find(NULL,name_root,name_temp,NULL,1);
- int temp_x=temp_search->id;
- if (temp_search)
- {
- printf("\nFound: Delete Successfully !\n");
- // Delete(NULL,&name_root,name_temp,NULL,1);
- // Delete(&id_root,NULL,NULL,temp_x,0);
- del_abdo(&(id_root),temp_x);
- break;
- }
- else
- {
- printf("\nNot Found !\n");
- break;
- }
- }
- else
- {
- printf("\nWrong Entry, Try Again.\n");
- break;
- }
- break;
- case 5:
- system("cls");
- printf("\Search\n*******\n");
- int x=10;
- Insert_tail(id_root);
- printf("\n\n");
- Print_Results();
- break;
- case 6:
- system("cls");
- printf("\nDisplay\n*******\n");
- if (id_root)
- {
- printf("Choices :\n[1] Pre-order\n[2] Post-order\n[3] In-order\n");
- printf("\n\nChoice [Display] :");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &c_sub);
- if (c_sub==1)
- {
- printf("\n%24s\n%24s","According to ID","***************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_preorder(id_root);
- printf("\n==================================\n");
- printf("\n%25s\n%25s","According to Name","*****************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_preorder(name_root);
- }
- else if (c_sub==2)
- {
- printf("\n%24s\n%24s","According to ID","***************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_postorder(id_root);
- printf("\n==================================\n");
- printf("\n%25s\n%25s","According to Name","*****************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_postorder(name_root);
- }
- else if (c_sub==3)
- {
- printf("\n%24s\n%24s","According to ID","***************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_inorder(id_root);
- printf("\n==================================\n");
- printf("\n%25s\n%25s","According to Name","*****************");
- printf("\n%-30s%-30s\n%-40s\n","Name","ID","__________________________________");
- Print_inorder(name_root);
- }
- else
- {
- printf("\nWrong Entry, Try Again.\n");
- break;
- }
- break;
- }
- else
- {
- printf("List is empty !\n");
- break;
- }
- case 7 :
- here1:
- system("cls");
- printf("\nSave\n****\n");
- save_indic=Save(name_root);
- if (bol)
- {
- printf("\nSaved Successfully. \n");
- break;
- }
- else
- printf("\nCouldn't save to file, Try Again. \n");
- break;
- case 8:
- system("cls");
- printf("\nExit\n****\n");
- if (!save_indic) printf("\nAre you sure you want to Exit without saveing changes ?\n[1] Yes\n[2] No, save now\n");
- fgets(C_temp, sizeof (C_temp), stdin);
- sscanf(C_temp, "%d", &c_sub);
- if (c_sub==1) break;
- else goto here1;
- break;
- default :
- printf("\nTry agian !\n");
- fflush(stdout);
- fflush(stdin);
- break;
- }
- }
- while (c!=8);
- return 0;
- }
- struct node* del_abdo(struct node** bst1,int I){
- struct node *tmp=NULL;
- if((*bst1)==NULL);
- else if((*bst1)->id > I)(*bst1)->left=del_abdo(&(*bst1)->left,I);
- else if((*bst1)->id < I)(*bst1)->right=del_abdo(&(*bst1)->right,I);
- else if((*bst1)->id == I){
- if((*bst1)->left==NULL && (*bst1)->right==NULL)(*bst1)=NULL;
- else if((*bst1)->left==NULL)(*bst1)=(*bst1)->right;
- else if((*bst1)->right==NULL)(*bst1)=(*bst1)->left;
- else {
- tmp=Find_min((*bst1)->right,NULL,0);
- (*bst1)->id=tmp->id;
- strcpy((*bst1)->name,tmp->name);
- (*bst1)->right=del_abdo(&(*bst1)->right,tmp->id);
- }
- }
- return (*bst1);
- }
- struct Search * Find_Search_results(char *N,int I)
- {
- struct Search *current=root;
- if (root==NULL) return NULL;
- while (current!=NULL)
- {
- if ((current->dup_x)->id==I) return current;
- current=current->next;
- }
- }
- void print_list (void)
- {
- 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");
- }
- struct node *create_node(char *N,int I)
- {
- struct node *temp=(struct node*)malloc(sizeof(struct node));
- strcpy(temp->name,N);
- temp->id=I;
- temp->left=NULL;
- temp->right=NULL;
- return temp;
- }
- struct node* Insert(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c)
- {
- if (c==0)
- {
- if (!(*id_ptr))
- {
- *id_ptr=create_node(N,I);
- }
- else if ((*id_ptr)->id > I) (*id_ptr)->left=Insert(&(*id_ptr)->left,&(*name_ptr),N,I,0);
- else (*id_ptr)->right=Insert(&(*id_ptr)->right,&(*name_ptr),N,I,0);
- return *id_ptr;
- }
- else
- {
- if (!(*name_ptr)) (*name_ptr)=create_node(N,I);
- else if (strcasecmp(N,(*name_ptr)->name)<=0) (*name_ptr)->left=Insert(NULL,&(*name_ptr)->left,N,I,1);
- else (*name_ptr)->right=Insert(NULL,&(*name_ptr)->right,N,I,1);
- return *name_ptr;
- }
- }
- struct node *Delete(struct node**id_ptr,struct node**name_ptr,char *N,int I,int c)
- {
- if (c==0)
- {
- struct node *temp_node;
- if ((*id_ptr)==NULL);
- else if ((*id_ptr)->id>I)
- {
- (*id_ptr)->left=Delete(&(*id_ptr)->left,&(*name_ptr),N,I,c);
- }
- else if (I>(*id_ptr)->id)
- {
- (*id_ptr)->right=Delete(&(*id_ptr)->right,&(*name_ptr),N,I,c);
- }
- else if (I==(*id_ptr)->id)
- {
- if ((*id_ptr)->left==NULL&&(*id_ptr)->right==NULL)
- {
- free((*id_ptr));
- (*id_ptr)=NULL;
- //return (*id_ptr);
- }
- else if ((*id_ptr)->left==NULL)
- {
- temp_node=(*id_ptr);
- (*id_ptr)=(*id_ptr)->right;
- free(temp_node);
- }
- else if ((*id_ptr)->right==NULL)
- {
- temp_node=(*id_ptr);
- (*id_ptr)=(*id_ptr)->left;
- free(temp_node);
- }
- else
- {
- temp_node=Find_min((*id_ptr)->right,NULL,0);
- (*id_ptr)->id=temp_node->id;
- strcpy((*id_ptr)->name,temp_node->name);
- (*id_ptr)->right=Delete (&(*id_ptr)->right,&(*name_ptr),NULL,temp_node->id,0);
- }
- return (*id_ptr);
- }
- }
- else
- {
- struct node *temp_node;
- if ((*name_ptr)==NULL);
- else if (strcasecmp(N,(*name_ptr)->name)<0)
- {
- (*name_ptr)->left=Delete(NULL,&(*name_ptr)->left,N,NULL,1);
- }
- else if (strcasecmp(N,(*name_ptr)->name)>0)
- {
- (*name_ptr)->right=Delete(NULL,&(*name_ptr)->right,N,NULL,1);
- }
- else if (!strcasecmp(N,(*name_ptr)))
- {
- if ((*name_ptr)->left==NULL&&(*name_ptr)->right==NULL)
- {
- free((*name_ptr));
- (*name_ptr)=NULL;
- }
- else if ((*name_ptr)->left==NULL)
- {
- temp_node=(*name_ptr);
- (*name_ptr)=(*name_ptr)->right;
- free(temp_node);
- }
- else if ((*name_ptr)->right==NULL)
- {
- temp_node=(*name_ptr);
- (*name_ptr)=(*name_ptr)->left;
- free(temp_node);
- }
- else
- {
- temp_node=Find_min(NULL,(*name_ptr)->right,1);
- (*name_ptr)->id=temp_node->id;
- strcpy((*name_ptr)->name,temp_node->name);
- (*name_ptr)->right=Delete (NULL,&(*name_ptr)->right,temp_node->name,NULL,1);
- }
- return (*name_ptr);
- }
- }
- }
- struct node *Find_min(struct node *id_ptr,struct node*name_ptr,int c)
- {
- if (c==0)
- {
- if (id_ptr==NULL)
- return NULL;
- else if (id_ptr->left==NULL)
- return id_ptr;
- else
- return Find_min(id_ptr->left,NULL,0);
- }
- else
- {
- if (name_ptr==NULL)
- return NULL;
- else if (name_ptr->left==NULL)
- return name_ptr;
- else
- return Find_min(NULL,name_ptr->left,1);
- }
- }
- bool Save(struct node*temp)
- {
- char loc_temp[200];
- char name_temp[50];
- int C3;
- int id_temp;
- FILE *cfPtr=NULL;
- void save_to_file(struct node *tempX)
- {
- if (tempX)
- {
- save_to_file(tempX->left);
- fprintf(cfPtr,"%s,%d\n",tempX->name,tempX->id);
- save_to_file(tempX->right);
- }
- }
- printf("\nEnter the location of the File or press enter to save to the pre-installed one\n");
- printf("\n\nChoice [Save] :");
- fgets(loc_temp,sizeof(loc_temp)-1,stdin);
- loc_temp[strlen(loc_temp)-1]='\0';
- if (!strcmp(loc_temp,""))
- {
- cfPtr=fopen("New_file.txt","w");
- save_to_file(temp);
- fclose(cfPtr);
- return true;
- }
- else
- {
- cfPtr=fopen(loc_temp,"w");
- if (cfPtr==NULL)return 0;
- save_to_file(temp);
- fclose(cfPtr);
- return true;
- }
- }
- bool load (struct node **id_ptr,struct node**name_ptr)
- {
- char loc_temp[200];
- char name_temp[50];
- int id_temp;
- int C3;
- FILE *cfPtr;
- printf("\nEnter the location of the File or press enter to load the pre-installed one\n");
- printf("\n\nChoice [Load] :");
- fgets(loc_temp,sizeof(loc_temp)-1,stdin);
- loc_temp[strlen(loc_temp)-1]='\0';
- if (!strcmp(loc_temp,""))
- {
- cfPtr=fopen("New_file.txt","r");
- do
- {
- fscanf(cfPtr,"%[^,]s",name_temp);
- fscanf(cfPtr,",%d\n",&id_temp);
- Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,0);
- Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,1);
- }
- while (!feof(cfPtr));
- fclose(cfPtr);
- return true;
- }
- else
- {
- cfPtr=fopen(loc_temp,"r");
- if (cfPtr==NULL)
- {
- return false;
- }
- do
- {
- fscanf(cfPtr,"%[^,]s",name_temp);
- fscanf(cfPtr,",%d\n",&id_temp);
- Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,0);
- Insert(&(*id_ptr),&(*name_ptr),name_temp,id_temp,1);
- }
- while (!feof(cfPtr));
- fclose(cfPtr);
- return true;
- }
- }
- void Print_preorder(struct node *ptr)
- {
- if (ptr)
- {
- printf("\n%-30s%-30d\n",ptr->name,ptr->id);
- Print_preorder(ptr->left);
- Print_preorder(ptr->right);
- }
- }
- void Print_postorder(struct node *ptr)
- {
- if (ptr)
- {
- Print_postorder(ptr->left);
- Print_postorder(ptr->right);
- printf("\n%-30s%-30d\n",ptr->name,ptr->id);
- }
- }
- void Print_inorder(struct node *ptr)
- {
- if (ptr)
- {
- Print_inorder(ptr->left);
- printf("\n%-30s%-30d\n",ptr->name,ptr->id);
- Print_inorder(ptr->right);
- }
- }
- int Count_nodes()
- {
- int c=0;
- struct Search *current=root;
- while (current!=NULL)
- {
- c++;
- current=current->next;
- }
- return c;
- }
- struct node* Find(struct node* id_ptr,struct node*name_ptr,char * N,int I,int c)
- {
- if (c==0)
- {
- if (!id_ptr) return NULL;
- else if (I==id_ptr->id) return id_ptr;
- else if (I<id_ptr->id) return Find(id_ptr->left,NULL,NULL,I,0);
- else return Find(id_ptr->right,NULL,NULL,I,0);
- }
- else
- {
- if (!name_ptr) return NULL;
- else if (!strcasecmp(N,name_ptr->name)) return name_ptr;
- else if (strcasecmp(N,name_ptr->name)<0) return Find(NULL,name_ptr->left,N,NULL,1);
- else return Find(NULL,name_ptr->right,N,NULL,1);
- }
- }
- struct Search * Insert_tail(struct node *ptr)
- {
- struct Search *current=root;
- struct Search *temp=(struct Search *)malloc(sizeof (struct Search ));
- temp->next=NULL;
- temp->dup_x=ptr;
- if (root==NULL)
- {
- root=temp;
- }
- else
- {
- while (current->next != NULL)
- {
- if (current->dup_x==ptr)return NULL;
- else
- {
- current=current->next;
- }
- }
- if (current->dup_x==ptr)return NULL;
- current->next=temp;
- }
- }
- void Print_Results(void)
- {
- struct Search *current=root;
- if (current==NULL) printf("\nNot Found.\n");
- else
- {
- while (current!=NULL)
- {
- printf("\n%-30s%-30d\n",(current->dup_x)->name,(current->dup_x)->id);
- current=current->next;
- }
- }
- }
- int Search_Dup(struct node *id_ptr,struct node *name_ptr,char *N,int I,int c)
- {
- if (c==0)
- {
- if (id_ptr)
- {
- if (id_ptr->id==I) Insert_tail(id_ptr);
- Search_Dup (id_ptr->left,NULL,NULL,I,0);
- Search_Dup (id_ptr->right,NULL,NULL,I,0);
- }
- }
- else
- {
- if (name_ptr)
- {
- if (!strcasecmp(name_ptr->name,N))Insert_tail(name_ptr);
- Search_Dup (NULL,name_ptr->left,N,NULL,1);
- Search_Dup (NULL,name_ptr->right,N,NULL,1);
- }
- }
- }
- struct node * Delete_Name_AND_ID(struct node **name_ptr,char *N,int I)
- {
- struct node *temp_node;
- if (name_ptr)
- {
- if (!strcasecmp((*name_ptr)->name,N)&&((*name_ptr)->id==I))
- {
- if ((*name_ptr)->left==NULL&&(*name_ptr)->right==NULL)
- {
- free((*name_ptr));
- (*name_ptr)=NULL;
- }
- else if ((*name_ptr)->left==NULL)
- {
- temp_node=(*name_ptr);
- (*name_ptr)=(*name_ptr)->right;
- free(temp_node);
- }
- else if ((*name_ptr)->right==NULL)
- {
- temp_node=(*name_ptr);
- (*name_ptr)=(*name_ptr)->left;
- free(temp_node);
- }
- else
- {
- temp_node=Find_min(NULL,(*name_ptr)->right,1);
- (*name_ptr)->id=temp_node->id;
- strcpy((*name_ptr)->name,temp_node->name);
- (*name_ptr)->right=Delete (NULL,&(*name_ptr)->right,temp_node->name,NULL,1);
- }
- return (*name_ptr);
- }
- }
- Search_Dup (NULL,(*name_ptr)->left,N,NULL,1);
- Search_Dup (NULL,(*name_ptr)->right,N,NULL,1);
- }
Advertisement
Add Comment
Please, Sign In to add comment