diff --git a/C-Plus-Plus/ds/quadratic-probing.cpp b/C-Plus-Plus/ds/quadratic-probing.cpp index 29477e00f5..d1c645e583 100644 --- a/C-Plus-Plus/ds/quadratic-probing.cpp +++ b/C-Plus-Plus/ds/quadratic-probing.cpp @@ -1,114 +1,96 @@ #include using namespace std; -int Hash(int x,int s) -{ - return x%s; +int Hash(int x, int s) { + return x % s; } //Function to insert in hashtable. -void QuadProbe(int Q[],int s,int x) -{ - int index= Hash(x,s); - int i=0; - while(Q[index]!=0 && Q[index]!=-1) - { - i++; - index=(Hash(x,s)+(i*i))%s; +void QuadProbe(int Q[], int s, int x) { + int index = Hash(x, s); + int i = 0; + while (Q[index] != 0 && Q[index] != -1) { + i++; + index = (Hash(x, s) + (i * i)) % s; - } - Q[index]=x; + } + Q[index] = x; } //Function to Search for an element in hashtable. -void QPSearch(int Q[],int s,int x) -{ - int index= Hash(x,s); - int i=0; - while(Q[index]!=0) - { - if(Q[index]==x) - { - cout<<"Element found at index "<>n; - int s=2*n; - int p=s; - while(p>0) - { - if( ( ((p-1)%6==0) && ((p+1)%6!=0) ) || ( ((p+1)%6==0) && ((p-1)%6!=0) )) - break; - p--; - } - int L[s]={0},Q[s]={0},D[s]={0}; - cout<<"Enter elements for quad probing "<>x; - - QuadProbe(Q,s,x); - - } - cout<<"The hash table is"<> n; + int s = 2 * n; + int p = s; + while (p > 0) { + if ( ( ((p - 1) % 6 == 0) && ((p + 1) % 6 != 0) ) || ( ((p + 1) % 6 == 0) && ((p - 1) % 6 != 0) )) + break; + p--; + } + int L[s] = {0}, Q[s] = {0}, D[s] = {0}; + cout << "Enter elements for quad probing " << endl; + for (int i = 0; i < n; i++) { + cin >> x; - cout<<"Enter element to be searched in QP : "; - cin>>x; - QPSearch(Q,s,x); - cout<<"Enter element to be deleted in QP : "; - cin>>x; - LPDelete(Q,s,x); - cout<<"The hash table is "<>x; - QuadProbe(Q,s,x); - cout<<"The hash table is "<> x; + QPSearch(Q, s, x); + cout << "Enter element to be deleted in QP : "; + cin >> x; + LPDelete(Q, s, x); + cout << "The hash table is " << endl; + for (int i = 0; i < s; i++) { + cout <<< < Q[i] << endl; + } + cout << "Enter element to be added : "; + cin >> x; + QuadProbe(Q, s, x); + cout << "The hash table is " << endl; + for (int i = 0; i < s; i++) { + cout <<< < Q[i] << endl; + } + return 0; }