-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (79 loc) · 1.85 KB
/
main.cpp
File metadata and controls
82 lines (79 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
#define size1 3
struct DataItem{
int key;
int data;};
struct DataItem* hashArray[size1];//this is the table as an array
int hash1(int key)
{
return key%size1;
}
struct DataItem *search1(int key)//a program that returns a pointer of type DataItem
{
int index=hash1(key);//gets the index with the has1 function
while(hashArray[index]!=NULL)
{
if(hashArray[index]->key==key)
return hashArray[index];
//if not foound
index++;
index=index%size1;//look from 0
}
return NULL;
}
void insertInTable(struct DataItem *item)
{
int index=hash1(item->key);
int i=0;
while(i<size1&&hashArray[index]!=NULL)
{
index++;
index%=size1;
i++;
}
if(hashArray[index]==NULL)hashArray[index]=item;
else
{
cout<<"table is full\n";
return;
}
}
void Delete(struct DataItem *item)
{
int index=hash1(item->key);
while(hashArray[index]->key!=item->key)//key is unique not the data
{
index++;
index%=size1;
}
hashArray[index]=NULL;//data is deleted
}
void show()
{
for(int i=0;i<size1;i++)
if(hashArray[i]!=NULL)
printf("value: %d ,key %d\n",hashArray[i]->data,hashArray[i]->key);
}
int main()
{
int n;
cin>>n;
int i=0;
int key1,data1;
while(i<n)
{
cin>>data1>>key1;
struct DataItem * item =(struct DataItem*)malloc(sizeof(struct DataItem));
item->data=data1;
item->key=key1;
insertInTable(item);
i+=1;
}
show();
return 0;
}