Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<stdio.h>
- using namespace std;
- int SIZE;
- struct Node
- {
- int val;
- Node * next;
- };
- struct LinkedList
- {
- private:
- Node ** hashTable;
- public:
- void init(int s)
- {
- SIZE = s;
- hashTable = (Node**)calloc(SIZE,sizeof(Node*));
- }
- int hashFunc(int key)
- {
- return key%SIZE;
- }
- void insertValue(int v)
- {
- Node * newnode = new Node;
- newnode->val = v;
- newnode->next = NULL;
- int index = hashFunc(v);
- if(hashTable[index] == NULL)
- {
- hashTable[index]=(Node*)malloc(sizeof(Node*));
- hashTable[index]=newnode;
- }
- else
- {
- Node* temp = new Node;
- temp = hashTable[index];
- newnode->next=temp;
- hashTable[index]=newnode;
- }
- }
- int searchValue(int v)
- {
- }
- void display()
- {
- for(int i=0;i<SIZE;i++)
- {
- cout<<i<<"-->";
- if(hashTable[i] != NULL)
- {
- Node* temp=new Node;
- temp=hashTable[i];
- while(temp != NULL)
- {
- cout<<temp->val<<"-->";
- temp=temp->next;
- }
- }
- cout<<"\n";
- }
- }
- };
- int main()
- {
- LinkedList l1;
- l1.init(10);
- l1.insertValue(22);
- l1.insertValue(32);
- l1.insertValue(21);
- l1.insertValue(25);
- l1.insertValue(27);
- l1.display();
- l1.searchValue(32);
- l1.searchValue(2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement