Advertisement
Eternoseeker

DSA Assignment 1

Nov 7th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | Source Code | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class singlerecord
  5. {  public:
  6.    int key;
  7.    long int ph_num;
  8.    string name;
  9.    singlerecord()
  10.    {
  11.   key=-1;
  12.   ph_num=-1;
  13.   string name;
  14.    }
  15.    void upd(int k,string nam,long int phu)
  16.    {
  17.   key=k;
  18.   name=nam;
  19.   ph_num=phu;
  20.    }
  21. };
  22.  
  23.  class hashtable
  24. {
  25. public:
  26. singlerecord rec[10];
  27. long long int ph_num;
  28. string name;
  29. int index,i,j;
  30. int counter;
  31. long long int phone;
  32.  
  33. void insert()
  34. {
  35. cout<<"\nEnter student name:";
  36. cin>>name;
  37. cout<<"\nEnter phone number:";
  38. cin>>phone;
  39.  
  40. index=phone%10;
  41. int counter =0;
  42. while(rec[index].key!=-1)
  43. {
  44. index=(index+1)%10;
  45.  
  46. counter++;
  47. if(counter>10)
  48. return;
  49.  
  50. }
  51.  
  52. rec[index].upd(counter,name,phone);
  53.  
  54.   }
  55.   void display(){
  56.   for(i=0;i<10;i++)
  57.   {
  58.   cout<<rec[i].key<<" - "<<rec[i].name<<"  "<<rec[i].ph_num<<endl;
  59.   }
  60.   }
  61.  
  62.  
  63.  void searching(){
  64. cout<<" \n Enter phone Number";
  65. cin>>phone;
  66. int j=0;
  67. for(j=0;j<10;j++)
  68. {
  69. if(phone==rec[j].ph_num)
  70. {cout<<"\nrecord found at"<<endl;
  71.    cout<<rec[j].key<<" - "<<rec[j].name<<"  "<<rec[j].ph_num<<endl;}
  72. }
  73. if(j==10)
  74. cout<<"Record not found"<<endl;
  75.  }
  76.  
  77. void del(){
  78. cout<<"\n Enter phone number";
  79. cin>>phone;
  80. int j=0;
  81. for(j=0;j<10;j++)
  82. {
  83. if(phone==rec[j].ph_num)
  84. {
  85.   rec[j].key=-1;
  86.   rec[j].ph_num=-1;
  87.   rec[j].name="";
  88.   break;
  89. }
  90. if(j==10)
  91. cout<<"Record not found to delete"<<endl;
  92. }
  93. }
  94.  
  95. };
  96.  
  97. int main()
  98. {
  99. hashtable h;
  100.  
  101. int choice=0;
  102.  
  103. do
  104. {
  105. cout<<"Enter 1 to insert a data item";
  106. cout<<"\nEnter 2 to delete a data item";
  107. cout<<"\nEnter 3 to display data item";
  108. cout<<"\nEnter 4 to search data item";
  109. cout<<"\nEnter 0 to exit\n";
  110. cin>>choice;
  111.  
  112. switch(choice)
  113. {
  114. case 1:
  115. h.insert();
  116. break;
  117. case 2:
  118. h.del();
  119. break;
  120. case 3:
  121. h.display();
  122. break;
  123. case 4:
  124. h.searching();
  125. break;
  126. default:
  127. cout<<"You entered a wrong choice";
  128. break;
  129. }
  130.  
  131.     }while(choice!=0);
  132.  
  133. return 0;
  134. }
Tags: dsa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement