forked from singhpratyush/algorithms-design-and-analysis-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc48bd2
commit dd257b0
Showing
20 changed files
with
1,376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Implementation of direct addressing. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
int main(void) | ||
{ | ||
char data[65535][10]; | ||
int choice; | ||
|
||
do | ||
{ | ||
int key; | ||
cout << "0. Exit\n1. Insert\n2. Delete\n3. Search\n"; | ||
cin >> choice; | ||
if(choice == 1) | ||
{ | ||
cout << "Enter key : " ; | ||
cin >> key; | ||
cout << "Enter data : " ; | ||
cin >> data[key]; | ||
} | ||
else if(choice == 2) | ||
{ | ||
cout << "Enter key : "; | ||
cin >> key; | ||
if(!strcmp(data[key],"")) | ||
cout << "Not found.\n"; | ||
else | ||
{ | ||
strcpy(data[key],""); | ||
cout << "Deleted.\n"; | ||
} | ||
} | ||
else if(choice == 3) | ||
{ | ||
cout << "Enter key : "; | ||
cin >> key; | ||
if(!strcmp(data[key],"")) | ||
cout << "Not found.\n"; | ||
else | ||
cout << "Data is " << data[key] << endl; | ||
} | ||
else if(choice == 0) | ||
return 0; | ||
else | ||
cout << "Invalid choice.. Try again.\n"; | ||
}while(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
Class definition for doubly linked list having a key and an integer data. | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class node | ||
{ | ||
unsigned int key; | ||
int data; | ||
node *left, *right; | ||
|
||
public: | ||
node(unsigned int keyArgument, int dataArgument) | ||
{ | ||
key = keyArgument; | ||
data = dataArgument; | ||
left = right = NULL; | ||
} | ||
|
||
node() | ||
{ | ||
node(0,0); | ||
} | ||
|
||
void setState(unsigned int keyArgument,int dataArgument) | ||
{ | ||
key = keyArgument; | ||
data = dataArgument; | ||
} | ||
|
||
void setLeft(node *ptr) | ||
{ | ||
left = ptr; | ||
} | ||
|
||
void setRight(node *ptr) | ||
{ | ||
right = ptr; | ||
} | ||
|
||
unsigned int getKey() | ||
{ | ||
return key; | ||
} | ||
|
||
int getData() | ||
{ | ||
return data; | ||
} | ||
|
||
node *getRight() | ||
{ | ||
return right; | ||
} | ||
|
||
node *getLeft() | ||
{ | ||
return left; | ||
} | ||
|
||
~node(){} | ||
}; | ||
|
||
class doublyLinkedList | ||
{ | ||
node *head, *tail; | ||
|
||
public: | ||
|
||
doublyLinkedList() | ||
{ | ||
head = tail = NULL; | ||
} | ||
|
||
bool push(unsigned int key, int data) | ||
{ | ||
if(hasKey(key)) | ||
return false; | ||
|
||
node *temp = new node(key, data); | ||
|
||
if(temp == NULL) | ||
return false; | ||
|
||
if(head == NULL) | ||
{ | ||
head = tail = temp; | ||
return true; | ||
} | ||
|
||
temp -> setRight(head); | ||
head -> setLeft(head); | ||
head = temp; | ||
return true; | ||
} | ||
|
||
bool pushBack(unsigned int key, int data) | ||
{ | ||
if(hasKey(key)) | ||
return false; | ||
|
||
node *temp = new node(key, data); | ||
|
||
if(temp == NULL) | ||
return false; | ||
|
||
if(head == NULL) | ||
{ | ||
head = tail = temp; | ||
return true; | ||
} | ||
|
||
temp -> setLeft(tail); | ||
tail -> setRight(temp); | ||
tail = temp; | ||
return true; | ||
} | ||
|
||
void display() | ||
{ | ||
node *temp; | ||
|
||
temp = head; | ||
|
||
while(temp != NULL) | ||
{ | ||
cout << temp ->getKey() << "( " << temp -> getData() << " )" << endl; | ||
temp = temp -> getRight(); | ||
} | ||
} | ||
|
||
bool deleteKey(unsigned int key) | ||
{ | ||
if(head == NULL) | ||
return false; | ||
|
||
node *temp = head; | ||
|
||
while(temp -> getKey() != key) | ||
{ | ||
temp = temp -> getRight() ; | ||
if(temp == NULL) | ||
return false; | ||
} | ||
|
||
if(temp -> getRight() == NULL && temp -> getLeft() == NULL) | ||
{ | ||
delete temp; | ||
head = tail = NULL; | ||
return true; | ||
} | ||
|
||
if(temp == head) | ||
{ | ||
head = head -> getRight(); | ||
temp -> setRight(NULL); | ||
delete temp; | ||
return true; | ||
} | ||
|
||
if(temp == tail) | ||
{ | ||
tail = tail -> getLeft(); | ||
temp -> setLeft(NULL); | ||
delete temp; | ||
return true; | ||
} | ||
|
||
node *right = temp -> getRight(), *left = temp -> getLeft(); | ||
right -> setLeft( left ); | ||
left -> setRight( right ); | ||
temp -> setRight(NULL); | ||
temp -> setLeft(NULL); | ||
delete temp; | ||
return true; | ||
} | ||
|
||
bool hasKey(unsigned int key) | ||
{ | ||
node *temp = head; | ||
do | ||
{ | ||
if(temp == NULL) | ||
return false; | ||
|
||
if(temp -> getKey() == key) | ||
return true; | ||
|
||
temp = temp -> getRight(); | ||
}while(true); | ||
} | ||
|
||
int getData(unsigned int key) | ||
{ | ||
node *temp = head; | ||
while(temp -> getKey() != key) | ||
temp = temp -> getRight(); | ||
return temp -> getData(); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Implementation of hashing using chaining. | ||
*/ | ||
|
||
#include "doublyLinkedList.hpp" | ||
|
||
using namespace std; | ||
|
||
class chainedHashTable | ||
{ | ||
int size; | ||
doublyLinkedList *array; | ||
|
||
public: | ||
|
||
chainedHashTable(int sizeArgument) | ||
{ | ||
size = sizeArgument; | ||
array = new doublyLinkedList[size]; | ||
} | ||
|
||
bool insert(unsigned int key, int data) | ||
{ | ||
return array[key % size].push(key , data); | ||
} | ||
|
||
void search(unsigned int key) | ||
{ | ||
int slot = key % size; | ||
if(array[slot].hasKey(key)) | ||
cout << "Data is : " << array[slot].getData(key) << endl; | ||
else | ||
cout << "Not found.\n"; | ||
} | ||
|
||
bool deleteKey(unsigned int key) | ||
{ | ||
return array[key % size].deleteKey(key); | ||
} | ||
}; | ||
|
||
int main(void) | ||
{ | ||
cout << "\nEnter size of hash table : "; | ||
int choice, data; | ||
unsigned int key; | ||
cin >> key; | ||
|
||
chainedHashTable table(key); | ||
|
||
do | ||
{ | ||
cout << "\n0. Exit\n1. Insert\n2. Search\n3. Delete\nEnter your choice : "; | ||
cin >> choice; | ||
switch(choice) | ||
{ | ||
case 1: | ||
cout << "\n***Insert Data***\n"; | ||
cout << "\nEnter Key : "; | ||
cin >> key; | ||
cout << "Enter Data : "; | ||
cin >> data; | ||
if(table.insert(key, data)) | ||
cout << "Success\n"; | ||
else | ||
cout << "Insertion failed.\n"; | ||
break; | ||
|
||
case 2: | ||
cout << "\n***Search Data***\n"; | ||
cout << "\nEnter Key : "; | ||
cin >> key; | ||
table.search(key); | ||
break; | ||
|
||
case 3: | ||
cout << "\n***Delete Data***\n"; | ||
cout << "\nEnter Key : "; | ||
cin >> key; | ||
if(table.deleteKey(key)) | ||
cout << "Success.\n"; | ||
else | ||
cout << "Deletion failed.\n"; | ||
} | ||
}while(choice != 0); | ||
return 0; | ||
} |
Oops, something went wrong.