Chaining C++ Code v1
#include<iostream>
using namespace std;
int n;
struct node{
int data;
node* next;
};
class Chaining{
public:
node *head,*tail;
Chaining(){
head=NULL;
tail=NULL;
}
};
int hash(int val){
return val%n;
}
int main(){
int ch,key,val;
cout<<"Enter Hash Table Size : ";cin>>n;
if(n%2==0) n = n+1;
Chaining *arr = new Chaining[n];
cout<<"-----------------------------\nMenu\n-----------------------------"<<endl;
cout<<"1.Insert\n2.Delete\n3.Display\n4.search\n5.Exit\n";
do{
cout<<"Enter your choice:";cin>>ch;
switch(ch){
case 1:{
cout<<"Enter Element to Insert :";cin>>val;
key=hash(val);
node* temp = new node;
temp->data=val;
temp->next=NULL;
if(arr[key].head==NULL){
arr[key].head=temp;
arr[key].tail=temp;
break;
}else{
arr[key].tail->next=temp;
arr[key].tail=temp;
}
break;
}
case 2:{
cout<<"Enter value to be Deleted :";cin>>val;
key = hash(val);
node* prev = arr[key].head;
node* current = arr[key].head->next;
while(current != NULL) {
if(current->data == val) {
break;
}
else {
cout << "Value " << current->data << " does not match " << val << ".\n";
prev = current;
current = current->next;
}
}
if(current == NULL) {
cout << "Can't remove value: no match found.\n";
}else {
cout << "Deleting: " << current << "\n";
prev->next = current->next;
delete current;
}
break;
}
case 3:{
cout<<"Displaying Hash Table:-"<<endl;
for(int i=0;i<n;i++){
cout<<i<<" : ";
if(arr[i].head==NULL) cout<<"Empty"<<endl;
else{
node *temp = new node;
temp = arr[i].head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
}
}
break;
case 4:{
cout<<"Enter Element To Search:";cin>>val;
key=hash(val);
node *temp = arr[key].head;
while(temp!=NULL){
if(temp->data==val){
cout<<"Search Successful"<<endl<<"Value Found in Bucket "<<key<<endl;
break;
}
temp=temp->next;
}
cout<<"Value not found"<<endl;
break;
}
case 5:
break;
default:
cout<<"Re-Enter...."<<endl;
break;
}
}while(ch!=4);
}
using namespace std;
int n;
struct node{
int data;
node* next;
};
class Chaining{
public:
node *head,*tail;
Chaining(){
head=NULL;
tail=NULL;
}
};
int hash(int val){
return val%n;
}
int main(){
int ch,key,val;
cout<<"Enter Hash Table Size : ";cin>>n;
if(n%2==0) n = n+1;
Chaining *arr = new Chaining[n];
cout<<"-----------------------------\nMenu\n-----------------------------"<<endl;
cout<<"1.Insert\n2.Delete\n3.Display\n4.search\n5.Exit\n";
do{
cout<<"Enter your choice:";cin>>ch;
switch(ch){
case 1:{
cout<<"Enter Element to Insert :";cin>>val;
key=hash(val);
node* temp = new node;
temp->data=val;
temp->next=NULL;
if(arr[key].head==NULL){
arr[key].head=temp;
arr[key].tail=temp;
break;
}else{
arr[key].tail->next=temp;
arr[key].tail=temp;
}
break;
}
case 2:{
cout<<"Enter value to be Deleted :";cin>>val;
key = hash(val);
node* prev = arr[key].head;
node* current = arr[key].head->next;
while(current != NULL) {
if(current->data == val) {
break;
}
else {
cout << "Value " << current->data << " does not match " << val << ".\n";
prev = current;
current = current->next;
}
}
if(current == NULL) {
cout << "Can't remove value: no match found.\n";
}else {
cout << "Deleting: " << current << "\n";
prev->next = current->next;
delete current;
}
break;
}
case 3:{
cout<<"Displaying Hash Table:-"<<endl;
for(int i=0;i<n;i++){
cout<<i<<" : ";
if(arr[i].head==NULL) cout<<"Empty"<<endl;
else{
node *temp = new node;
temp = arr[i].head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
}
}
break;
case 4:{
cout<<"Enter Element To Search:";cin>>val;
key=hash(val);
node *temp = arr[key].head;
while(temp!=NULL){
if(temp->data==val){
cout<<"Search Successful"<<endl<<"Value Found in Bucket "<<key<<endl;
break;
}
temp=temp->next;
}
cout<<"Value not found"<<endl;
break;
}
case 5:
break;
default:
cout<<"Re-Enter...."<<endl;
break;
}
}while(ch!=4);
}
Comments
Post a Comment