Advertisement
apl-mhd

chain Hashing

Feb 19th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <climits>
  6. using namespace std;
  7.  
  8. struct  SymbolInfo{
  9.  
  10.     string name;
  11.     string type;
  12.  
  13.     SymbolInfo  *next;
  14.  
  15. };
  16.  
  17. typedef  SymbolInfo node;
  18.  
  19. int MAX = 50;
  20.  
  21. string inameType;
  22. string iname="";
  23. string itype="";
  24.  
  25. node *SymbolTable[50];
  26.  
  27. void init(){
  28.  
  29.     for (int i = 0; i <MAX; ++i) {
  30.  
  31.         //SymbolTable[i] = new node;
  32.         SymbolTable[i] = NULL;
  33.     }
  34. }
  35.  
  36.  
  37.  
  38. int  hashKey(string name);
  39. void  insert();
  40. void  dlt();
  41. void  search();
  42. void  update();
  43. void showALl();
  44. int stringSplit(string nameType);
  45. bool emptyOrNot();
  46. bool nameEmpty( string nm);
  47. void newLine();
  48.  
  49.  
  50. void menubar(){
  51.  
  52.     bool decision = true;
  53.  
  54.     while(decision){
  55.  
  56.         cout<<"1. Insert :\n";
  57.         cout<<"2. Search :\n";
  58.         cout<<"3. Delete :\n";
  59.         cout<<"4. Show :\n";
  60.         cout<<"5. Update :\n";
  61.         cout<<"6. exit :\n";
  62.         cout<<"Select option from menu :"<<endl;
  63.  
  64.         string n;
  65.         cin>>n;
  66.  
  67.         if(n == "1" ){
  68.  
  69.             insert();
  70.  
  71.         }
  72.  
  73.         else if(n == "2"){
  74.  
  75.             search();
  76.  
  77.         }
  78.         else if(n == "3"){
  79.  
  80.             dlt();
  81.  
  82.         }
  83.         else if(n == "4"){
  84.  
  85.             showALl();
  86.  
  87.  
  88.         }
  89.         else if(n == "5"){
  90.             update();
  91.  
  92.         }
  93.         else if (n == "6"){
  94.  
  95.             exit(0);
  96.  
  97.  
  98.         }
  99.         else{
  100.             cout<<"Invalid input, enter again \n";
  101.         }
  102.  
  103.     }
  104.  
  105.  
  106. }
  107.  
  108.  
  109. int main() {
  110.  
  111.  
  112.     init();
  113.     menubar();
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. void  insert() {
  123.  
  124.  
  125.     cout << "****Insert****\n";
  126.     cout << "Enter Name,Type: \n";
  127.     cin >> inameType;
  128.  
  129.  
  130.     int count = stringSplit(inameType);
  131.  
  132.     int len = inameType.length();  //string length
  133.     int comma = inameType.find(',');  //find comma ',' index other it return -1
  134.  
  135.  
  136.     if (count == 0) {  // id donot have any comma then return
  137.  
  138.         cout << "comma separator not found, enter again : " << endl;
  139.  
  140.         return;
  141.  
  142.  
  143.     } else if (count > 1) {
  144.  
  145.         cout << "More than one comma found, enter again : " << endl;
  146.  
  147.         return;
  148.  
  149.     } else {
  150.  
  151.         iname = inameType.substr(0, comma);
  152.         itype = inameType.substr(comma + 1, len);
  153.  
  154.         if(iname == itype){
  155.  
  156.             cout<<"Name and Type can not be same\n"<<endl;
  157.             return;
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.  
  164.     int key = hashKey(iname);
  165.  
  166.  
  167.     node *temp = new node();
  168.     node *root;
  169.  
  170.     if (SymbolTable[key] == NULL) { // if SymbolTable[key] == Null
  171.  
  172.         temp->name = iname;
  173.         temp->type = itype;
  174.         temp->next = NULL;   //assign next  == null
  175.         SymbolTable[key] = temp;   //assign SymbolTable[key] = newly crated node;
  176.  
  177.         cout <<"hashkey(" <<key <<") -> " << iname<< ":"<<itype<<" insert successfully"<<endl;
  178.  
  179.     } else {                         //if not SymbolTable[key] == null
  180.  
  181.         temp->name = iname;
  182.         temp->type = itype;
  183.         temp->next = NULL;
  184.  
  185.         root = SymbolTable[key];         //store SymbolTable[key] starting location
  186.  
  187.         //  cout<<root<<endl;
  188.  
  189.  
  190.         if (SymbolTable[key]->name == iname) {               //if start in SymbolTable[key] == iname then return
  191.             cout <<"'"<< iname << "' Already exists !" << endl;
  192.             return;
  193.         }
  194.  
  195.         while (root->next != NULL) {      //iterate until last node->next !=null
  196.  
  197.             root = root->next;           //like root++
  198.  
  199.             if (root->name == iname) {    //if already exists then return from loop;
  200.                 cout <<"'"<< iname << "' Already exists !" << endl;
  201.                 return;
  202.             }
  203.  
  204.         }
  205.  
  206.         root->next = temp;   //if no name found then temp assin to last node->next = temp;
  207.         cout <<"hashkey(" <<key <<") -> " << iname<< ":"<<itype<<" insert successfully"<<endl;
  208.  
  209.     }
  210.  
  211. }
  212.  
  213. }
  214.  
  215.  
  216. int hashKey(string key){
  217.  
  218.     int i=0;
  219.     int k=0;
  220.  
  221.     while(key[i] !='\0'){      //iterate hole string
  222.         k +=key[i];          //sumup asci value
  223.         i++;
  224.     }
  225.  
  226.     return k % 10;
  227. }
  228.  
  229. void  dlt(){
  230.  
  231.     if(emptyOrNot() == false){
  232.         cout<<"List is empty"<<endl<<endl;
  233.         return;
  234.     }
  235.  
  236.     cout<<"****Delete****\n";
  237.     cout<<"Enter Name: \n";
  238.     cin>>iname;
  239.  
  240.     int  key = hashKey(iname);  //iname return hashkey
  241.  
  242.  
  243.     node *temp,*root;
  244.     root = SymbolTable[key];
  245.  
  246.     if (root == NULL) {   //if SymbolTable[key] == null then return
  247.         cout  << iname << "' Not Found" << endl;
  248.     }
  249.     else if(SymbolTable[key]->name == iname){  //if at first node name found SymbolTable[key] = SymbolTable[key]->next
  250.         SymbolTable[key] = SymbolTable[key]->next;
  251.         cout << iname << " has successfully  delteted" << endl;
  252.  
  253.     }
  254.  
  255.     else{
  256.  
  257.         while(root->name != iname){  //iterate until name not found
  258.  
  259.             temp = root;  //store previous node addres, for join node
  260.             if(root->next == NULL){    //last node->next ==null means no name found and return form loop
  261.  
  262.                 cout << "No " << iname << "found" << endl;
  263.  
  264.                 return;
  265.             }
  266.  
  267.             root = root->next;
  268.         }
  269.  
  270.         cout << iname << " has successfully  delteted" << endl;
  271.  
  272.         temp->next = root->next;  // like 1,2,3, are nodesl. I want to delete 2 so need to join 1->3 node
  273.     }
  274. }
  275.  
  276. void showALl(){
  277.  
  278.  
  279.     if(emptyOrNot() == false){
  280.  
  281.         cout<<"List is empty"<<endl<<endl;
  282.  
  283.         return;
  284.     }
  285.  
  286.     cout<<"****Show All****\n";
  287.  
  288.  
  289.  
  290.     for (int i = 0; i<MAX; ++i) {  //this is simple
  291.  
  292.         if (SymbolTable[i] != NULL){   //if SymbolTable[key] == null then exit from if condition
  293.  
  294.             node *temp = SymbolTable[i];
  295.             cout<<"keyIndex("<<i<<")-> ";
  296.             while(temp != NULL)
  297.             {
  298.                 cout<<temp->name<<":"<<temp->type<<", ";
  299.                 temp = temp->next;
  300.             }
  301.             cout<<endl;
  302.         }
  303.  
  304.  
  305.  
  306.     }
  307.  
  308.     cout<<endl;
  309. }
  310.  
  311. void search(){
  312.  
  313.  
  314.     if(emptyOrNot() == false){
  315.         cout<<"List is empty"<<endl<<endl;
  316.         return;
  317.     }
  318.  
  319.     cout<<"****Search****\n";
  320.     cout<<"Enter Name: \n";
  321.     cin>>iname;
  322.  
  323.     int key = hashKey(iname);
  324.  
  325.  
  326.     cout<<"search result :"<<endl;
  327.  
  328.         if (SymbolTable[key] == NULL) {
  329.             cout << " '" << iname << "' not found" << endl;
  330.         }
  331.         else{
  332.  
  333.             node *temp = SymbolTable[key];
  334.  
  335.             cout<<"hashKey("<<key<<"): ";
  336.             while(temp != NULL)
  337.             {
  338.  
  339.                 if(temp->name == iname){
  340.                     cout<<"Name: "<<temp->name<<" Type: "<<temp->type<<endl;
  341.  
  342.                     return;
  343.                 }
  344.  
  345.                 temp = temp->next;
  346.  
  347.             }
  348.  
  349.             cout << " '" << iname << "' not found" << endl;
  350.  
  351.             cout<<endl;
  352.  
  353.         }
  354.  
  355. }
  356.  
  357. void  update(){
  358.  
  359.  
  360.     if(emptyOrNot() == false){
  361.         cout<<"List is empty"<<endl<<endl;
  362.         return;
  363.     }
  364.  
  365.  
  366.     cout<<"****Update****\n";
  367.     cout<<"Enter Name: \n";
  368.     cin>>iname;
  369.  
  370.  
  371.  
  372.     cout<<"Enter UpdateType: \n";
  373.     cin>>itype;
  374.  
  375.  
  376.  
  377.     int key = hashKey(iname);
  378.  
  379.  
  380.  
  381.     if (SymbolTable[key] == NULL) {    //if SymbolTable[key] == empty then return from this
  382.         cout << " '" << iname << "' not found" << endl;
  383.         return;
  384.     }
  385.     else{
  386.  
  387.         node *temp = SymbolTable[key];
  388.  
  389.         cout<<"key("<<key<<"): "<<endl;
  390.         while(temp != NULL)
  391.         {
  392.  
  393.             if(temp->name == iname){  //if name found then assign type=itype and return from loop
  394.                 cout<<"Name: "<<temp->name<<" Type: "<<itype<<" Updated Successfully "<<endl;
  395.                 temp->type = itype;
  396.  
  397.                 return;
  398.             }
  399.  
  400.             temp = temp->next; //temp++;
  401.  
  402.         }
  403.  
  404.         cout << " '" << iname << "' not found" << endl;
  405.  
  406.         cout<<endl;
  407.  
  408.     }
  409.  
  410. }
  411.  
  412. int stringSplit(string nameType){
  413.  
  414.     int len = nameType.length();  //string length
  415.     int comma =  nameType.find(',');  //find comma ',' index other it return -1
  416.     int count =0;
  417.  
  418.     for(int i=0; i<len; i++){
  419.  
  420.         if(nameType[i] == ',')
  421.             count++;
  422.  
  423.     }
  424.     return  count;
  425.  
  426. }
  427.  
  428. void newLine(){
  429.  
  430.     cout<<endl;
  431. }
  432.  
  433. bool emptyOrNot(){
  434.  
  435.     for (int i = 0; i <MAX ; ++i) {
  436.  
  437.         if(SymbolTable[i] != NULL)
  438.             return true;
  439.  
  440.     }
  441.     return  false;
  442. }
  443.  
  444. bool nameEmpty( string nm){
  445.  
  446.     cout<<nm;
  447.  
  448.     for (int i = 0; i <MAX ; ++i) {
  449.         if(SymbolTable[i]->name == nm)
  450.             return true;
  451.  
  452.     }
  453.  
  454.     return false;
  455.  
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement