Tuesday, July 18, 2017

C++ program for Hash table using Linear Probing

#include<iostream>
using namespace std;
class Hash
{
public:
int *a,sz,count;
Hash(int n)
{
a=new int[sz=n];
for(int i=0;i<sz;i++)
{
a[i]=-1;
}
count=0;
}
void insert(int);
int delete1(int);
int search(int);
void display();
};
void Hash::insert(int x)
{
if(count==sz)
{
cout<<"\nHash table Full";
}
else
{
int key=x%sz;
int t=key;
if(a[key]==-1)
{
a[key]=x;
count++;
cout<<"\n"<<x<<" Element Inserted at "<<key;
}
else
{
xyz:
if(key==sz-1)
key=-1;
key=key+1;
if(key==t)
{
return;
}
else
{
if(a[key]==-1)
{
a[key]=x;
count++;
cout<<"\n"<<x<<"  Element Inserted at  "<<key;
return;
}
}
goto xyz;
}
}
}
int Hash::delete1(int m)
{
int i;
int key=m%sz;
if(count==0)
{
return 0;
}
if(a[key]==m)
{
a[key]=-1;
count--;
return key;
}
else
{
for(i=0;i<sz;i++)
{
if(a[i]==m)
{
a[i]==-1;
count--;
return i;
}
}
if(i==sz)
{
return 100;
}

}
}
int Hash::search(int m)
{
int i;
int key=m%sz;
if(count==0)
{
return 0;
}
if(a[key]==m)
{
return key;
}
else
{
for(i=0;i<sz;i++)
{
if(a[i]==m)
{
return i;
}
}
if(i==sz)
{
return 100;
}

}

}
void Hash::display()
{
for(int i=0;i<sz;i++)
{
if(a[i]!=-1)
{
cout<<endl<<i<<"  "<<a[i];
}
}
}
int main()
{
int n,x,y,z,ch,fg;
cout<<"\n Enter size of hash table\n";
cin>>n;
Hash ob(n);
while(1)
{
cout<<"\n1. insert\n2. Delete\n3. search\n4. display\n5. Exit\n enter ur choice\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter an Element to insert\n";
cin>>x;
ob.insert(x);
break;
case 2:
cout<<"\n Enter an Element to Delete\n";
cin>>y;
fg=ob.delete1(y);
if(fg==100)
{

cout<<"\nElement not in the List\n";
   }
else if(fg==0)
{
cout<<"\n hash table Empty\n";
}
else
{
cout<<"\n deleted element "<<y<<" at position "<<fg<<endl;
}
break;
case 3:
cout<<"\n Enter an Element to Search\n";
cin>>z;
fg=ob.search(z);
if(fg==100)
{

cout<<"\nElement not in the List\n";
   }
else if(fg==0)
{
cout<<"\n hash table Empty\n";
}
else
{
cout<<"\n"<<z<<" element Searched at position "<<fg<<endl;
}
break;
case 4:
ob.display();
break;
case 5:
exit(0);
default:
cout<<"\n Wrong Choice\n";

}
}

return 0;

}

Labels:

Monday, July 3, 2017

Multi Stack using single Array

#include<iostream>
using namespace std;
class Stack_ADT
{
public:
int *a,*t,*s,*c;
Stack_ADT(int n,int m)
{
int i;
a=new int[n];
t=new int[m];
s=new int[m+1];
for(i=0;i<m;i++)
{
t[i]=(n/m)*i-1;
s[i]=(n/m)*i-1;
}
s[i]=n-1;
}
void m_push(int i,int x);
void m_pop(int i);
void m_display(int);
};
void Stack_ADT::m_push(int i,int x)
{
if(t[i]==s[i+1])
cout<<"\nStack "<<i+1<<" is FULL\n";
else
{
t[i]=t[i]+1;
a[t[i]]=x;
c[i]=c[i]+1;
cout<<"\n Element "<<a[t[i]]<<" Pushed into the Stack ";
}
}
void Stack_ADT::m_pop(int i)
{
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
t[i]=t[i]-1;
c[i]=c[i]-1;
cout<<"\nPopped Element of the Stack : "<<a[t[i]+1];
}
}
void Stack_ADT::m_display(int i)
{
int j=s[i];
if(t[i]==s[i])
cout<<"\nStack "<<i+1<<" is EMPTY\n";
else
{
cout<<"\n Elements of the Stack "<<i+1<<" : \n";
while(t[i]>j)
{
cout<<a[j+1]<<"\n";
j++;
  }
}
}

int main()
{
int i,ch,x,n,m;
cout<<"\n Enter number of Stacks  ";
cin>>m;
cout<<"\n Enter size of array  ";
cin>>n;
Stack_ADT ob(n,m);
while(1)
{
cout<<"\n Enter Your choice";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter element to push  ";
cin>>x;
cout<<"\n enter stack number 1 to "<<m<"  ";
cin>>i;
if(i<=m)
ob.m_push(i-1,x);
else
{
cout<<"Entered wrong stack";
}
break;
case 2:
cout<<"\n enter stack number 1 to "<<m<<" to Pop the element  ";
cin>>i;
if(i<=m)
ob.m_pop(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 3:
cout<<"\n enter stack number 1 to "<<m<<" to display the elements  ";
cin>>i;
if(i<=m)
ob.m_display(i-1);
else
{
cout<<"Entered wrong stack";
}
break;
case 4:
exit(0);
default:
cout<<"\n Entered wrong choice";
}
}
return 0;
}

Labels: