From dec0cc34bd6d1500d8b920ddd990f864ca45eec1 Mon Sep 17 00:00:00 2001 From: Rajiv Singh Date: Tue, 16 Mar 2021 04:21:16 +0530 Subject: [PATCH] Update linear-probing.cpp --- C-Plus-Plus/ds/linear-probing.cpp | 163 ++++++++++++++---------------- 1 file changed, 74 insertions(+), 89 deletions(-) diff --git a/C-Plus-Plus/ds/linear-probing.cpp b/C-Plus-Plus/ds/linear-probing.cpp index 1a77ffab0c..1ef2856f84 100644 --- a/C-Plus-Plus/ds/linear-probing.cpp +++ b/C-Plus-Plus/ds/linear-probing.cpp @@ -1,107 +1,92 @@ #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 element in HashTable. -void LinearProbe(int L[],int s,int x) -{ - int index=Hash(x,s); - int i=0; - while(L[index]!=0 && L[index]!=-1) - { - i++; - index=(Hash(x,s)+i)%s; +void LinearProbe(int L[], int s, int x) { + int index = Hash(x, s); + int i = 0; + while (L[index] != 0 && L[index] != -1) { + i++; + index = (Hash(x, s) + i) % s; - } - L[index]=x; + } + L[index] = x; } //Function to search for an element. -void LPSearch(int L[],int s,int x) -{ - int index= Hash(x,s); - int i=0; - while(L[index]!=0) - { - if(L[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 Linear probing "<>x; - LinearProbe(L,s,x); - } - for(int i=0;i>x; - LPSearch(L,s,x); - cout<<"Enter element to be deleted in LP : "; - cin>>x; - LPDelete(L,s,x); - cout<<"The hash table is"<>x; - LinearProbe(L,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 Linear probing " << endl; + for (int i = 0; i < n; i++) { + cin >> x; + LinearProbe(L, s, x); + } + for (int i = 0; i < s; i++) { + cout << L[i] << endl; + } + cout << "Enter element to be searched in LP : "; + cin >> x; + LPSearch(L, s, x); + cout << "Enter element to be deleted in LP : "; + cin >> x; + LPDelete(L, s, x); + cout << "The hash table is" << endl; + for (int i = 0; i < s; i++) { + cout << L[i] << endl; + } + cout << "Enter element to be added : "; + cin >> x; + LinearProbe(L, s, x); + cout << "The hash table is" << endl; + for (int i = 0; i < s; i++) { + cout << L[i] << endl; + } + return 0; }