Data Structure Lab Record
Data Structure Lab Record
Aim:
To implement the linked lst concept with insert,delete,and reverse function.
Description: A linked list is one of the linear data structure that is used to sort the data.The
linked list is the collection of nodes each node is used to to store data. A node consists of two
points the first point stores the data and the second will contain the address of the next node.
Each node is dynamically created memory.the head is used to store the address of the the
first node. We can traverse through the linked list using only head node. The last node will be
have null in address point.
Algorithm
Create: This algorithm creates a list for a given N nodes.
Step 1: start
Step 4: If list empty read data to x and assign T-> data=x and t->next=null. If Head =NULL
then head=temp [first node created]
Step 5: else
Step 7: stop
Display: this algorithm traverse the linked list and display data of each node.
Step 1: start
Step 6: stop.
Insert: This function inserts a new node it given position
Step 1: start
Step 3: create a new node and store the data and assign link(temp)=NULL
Step 4: If pos=1
4.2: Go to step 7
5.1: Repeat step 5.2 until p< pos and link (T)!=NULL
5.2: Increment p by 1.
5.8: Go to step 7
Stop 6: Else
Step 7: Stop
Step 1: start
Step 3: if (pos==)
3.4 : Go to step 6
Step 4: Else
4.4 : Increment p by 1
Step 5: Else
Step 6: Stop
Step 1: start
Step 6: stop
PROGRAM FOR LINKED LIST OPERATIONS
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct Node
{
int info;
Node *link;
};
class List
{
Node *p,*tail,*q,*head;
int count ;
public:
List();
void display();
void insert();
void delet();
void Reverse();
};
List::List()
{
int n,i=1,x;
count =0;
cout<<”\n Enter No of the nodes:”;
cin>>n;
while(i<=n)
{
p=new node();
cout<<”\n Enter data”;
cin>>x;
p->info=x;
p->link=NULL;
count++;
if(i==1)
{
head=tail=p;
else
{
tail->link=p;
tail=p;
}
i++;
}
}
void List::delet()
{
int i=1,pos;
cout<<”\n Enter the position”;
cin>>pos;
p=head;
if(pos==1)
{
head=head->link;
delet(p);
}
else
{
while(i<pos)
{
q=p;
p=p->link;
i++;
}
}
if(p->link==NULL)
{
q->link=NULL;
delet(p);
}
else
{
q->link=p->link;
delet(p);
}
display();
}
void List::insert()
{
int pos,i;
p=new node();
cout<<”\n Enter data”;
cin>>p->info;
p->link=NULL;
cout<<”\n Enter the position”;
cin>>pos;
if(pos==1)
{
p->link=head;
head=p;
}
else
{
i=2;
q=head;
while(i<pos)
{
q=q->link;
i++;
}
if(q->link==NULL)
{
q->link=p;
}
else
{
p->link= q->link;
q->link=p;
}
display();
}
void List:: reverse()
{
Node *ptr=head,*temp,*pn=NULL;
cout<<endl;
while(ptr!=NULL)
{
temp=ptr->link;
ptr->link=pn;
pn=ptr;
ptr=temp;
}
head=pn;
}
void List::display()
{
cout<<endl<<”\n The List is :”;
for(p=head;p!=NULL;p= p->link)
cout<<p->info<<” ”;
}
void main()
{
List l;
int ch;
do
{
cout<<endl<<”\t\t\t The linked List”;
cout<<endl<<”\t\t\t +++++++++++”;
cout<<endl<<”\t\t\t 1.display”;
cout<<endl<<”\t\t\t 2.Insert”;
cout<<endl<<”\t\t\t 3. Delet”;
cout<<endl<<”\t\t\t 4. Reverse”;
cout<<endl<<”\t\t\t 5. Exit”;
cout<<endl<<endl<<”\t\t\t Enter Your Choice”;
cin>>ch;
switch(ch)
{
case 1:
l.Display();
break;
case 2:
l.insert();
break;
case 3: l.delet();
break;
case 4: l.Reverse();
l.display();
break;
default:
cout<<endl<<”Invalid
choice”;
} }
while(ch<5); }
OUTPUT :
Enter No of Nodes 4
Enter Data 2 4 6 8
Linked List
1. Display
2. Insert
3. Delete
4. Reverse
5. Exit
Enter Data 10
Enter position 3
The List is 2 4 10 6 8
Linked List
1. Display
2. Insert
3. Delete
4. Reverse
5. Exit
The List is 8 10 4 2
Linked List
1. Display
2. Insert
3. Delete
4. Reverse
5. Exit
CIRCULAR QUEUE
AIM:
Circular array implementation of queue with menu options like insert, delete,display and
exit.
Procedure:
Circular queue are implemented sequentially using array zero to max-1 elements can be
sorted in circular queue where max size of the array used is insertion and deletion are two
primitive operations performed on a circular queue initially when c queue is empty then front
=0 and rear=0.
Insertion:
If the circular queue is full, then it prints overflow message otherwise find next rear
locations by statement “rear=(rear+1)%max”.
Deletion:
1) It checks the queue for empty by the condition front==-1 and rear=-1 which is
initial condition of empty queue. If it is, it prints circular queue underflow
message.
2) It check if there is any one item in the queue by condition front=rear.
3) If there is only one element then initialize front=0 and rear=0. Otherwise
increment front by 1”front=(front+1)%max” and returns deleted item.
Step 1 : [queue already filled?] if front ==0 and rear==(s-1) or if front –rear==1 then write
overflow and return.
Step 2: else
Delete : The procedure deletes an element from a queue and assigns it to the variables.
Step 1 : [if queue already empty ] if front=0 and rear=-1 then write underflow and return.
Step 2 : else
Step 3: return
Step 2: else
Step 3: return
class Cirque
{
int *p,s;
int front ,rear;
public:
Cirque()
{
front=-1;
rear=-1;
}
void create();
void display();
void insert();
void delet();
};
void Cirque:: create()
{
cout<<”Enter the size of the circular queue”;
cin>>s;
p=new int[s];
cout<<”circular queue is created :”<<s<<endl;
}
void Cirque::display()
{
int i;
cout<<”\n front->”;
if(rear<front)
{
for(i=front;i<s;i++)
cout<<p[i]<<” ”;
for(i=0;i<=rear;i++)
cout<<p[i]<<” ”;
}
else
{
for(i=front;i<=rear;i++)
cout<<p[i]<<” ”;
}
cout<<”<-rear”;
}
void Cirque::insert ()
{
int a;
cout<<”\ Enter value to push in the circular
queue:”;
cin>>a;
if(front==-1)
{
front=0;
p[++rear]=a;
}
Else if(front==0&&rear==(s-1)||(front-rear==1))
cout<<”circular queue is full:”<<endl;
else
if(rear==(s-1)&&(front!=0)
{
rear=-1;
p[++rear]=a;
}
else
p[++rear]=a;
}
void Cirque::delet()
{
if(front==0&&rear==-1)
cout<<”circular queue is underflow:”;
if(front==(s-1)&&rear!=(s-1))
front=0;
else
front=front+1;
}
void main()
{
Cirque c;
int ch;
clrscr();
cout<<” menu”<<endl;
cout<<endl<<”\t\t\t +++++++++++”;
cout<<endl<<”\t\t\t 1.CREATE”;
cout<<endl<<”\t\t\t 2.push”;
cout<<endl<<”\t\t\t 3. pop”;
cout<<endl<<”\t\t\t 4. display”;
cout<<endl<<”\t\t\t 5. Exit”<<endl;
do
{
cout<<endl<<endl<<”\t\t\t Enter Your Choice”;
cin>>ch;
switch(ch)
{
case 1:
c.create();
break;
case 2:
c.insert();
break;
case 3: l.delet();
break;
case 4: c.display();
break;
default:
cout<<endl<<”Invalid
choice”;
}
}
while(ch<5);
Output
Menu
1. Create
2. Insert
3. Delete
4. Display
5. Exit
Front->9 5 7 3 <-rear
Front->9 5 7 3 <-rear
Front-> 5 7 3 2<-rear
INFIX-POSTFIX EVALUATION
Aim:
Array implementation of stack to evaluate a given postfix expression after accepting
values of single character operands at run time
Procedure:
Simple arithmetic expression can be notated in three ways, namely, infix, postfix, and prefix
notations.
In infix notation, the operation symbol is between two operands. Eg: A+b
In Postfix notation, the operation symbol is placed after two operands. Eg: AB+
In Prefix notation, the operation symbol is placed before two operands. E.g: +AB
char opt;
stakpostfix ip;
clrscr();
do
{
ip.infixe();
ipconversion();
cout<<”the post fix conversion
is:”<<ip.postfix<<endl;
ip.result();
cout<<”do you want to continue y/n:”;
cin>>opt;
}
while(opt=='y');
}
Output:
Enter the infix expression: a+ (b/c)*(d*e)
The postfix expression is: abc/de**+
A binary tree is said to be binary search tree if each node of tree as following
property:
1. The value of node ‘n’ is greater than every value of the left sub-tree of N
2. The value of node ‘n’ is less than every value in the right sub-tree of N
Analysis:
In the binary search sub-tree there are three standard ways of traversing a binary tree
with root R.
->In preorder traversal the root node is processed first traverse the left sub-tree ‘p’ and
tracers the sub-tree of R,
->In in order traversal the left sub-tree of ‘r’ process the root traversal the left sub-tree
R,
->In postorder traversal the left sub-tree of R is process first then traverse the right
sub-tree of r and then process the rot ‘r’
Example
B C
D F
E
F G
Preorder: ABCDGHCEF
Inorder: GDHBCAECF
Postorder: GHDBEFCA
Algorithm
Create( ): this algorithm is used to construct a binary search tree ‘r’ in a pointer to the
node and temp this pointer variable
Step6: return
Add(r, n): This algorithm is use k to add the element in the current location of binary tree r, n
are received variables at tem and ele from the function
Step 4: return
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int no;
node *left;
node *right;
};
class bst
{
public:
node *root;
bst()
{
root= new node;
}
void create();
void add(node**,int);
void disp(node*,int);
void display();
};
void bst::create()
{
int ele;
node **temp=&root;
*temp=NULL;
while(1)
{
cout<<"\n enter a no at last enter 0:";
cin>>ele;
if(ele==0) break;
add(temp,ele);
}
}
void bst::add(node **r,int n)
{
if(*r==NULL)
{
*r=new node;
(*r)->no=n;
(*r)->left=NULL;
(*r)->right=NULL;
}
if((*r)->no>n)
{
clrscr();
cout<<"\n left side of the tree";
add(&(*r)->left,n);
}
if((*r)->no<n)
{
clrscr();
cout<<"\n right side of the tree";
add(&(*r)->right,n);
} }
void bst::display()
{
int ch;
while(1)
{
cout<<"\n\n\n";
cout<<"\n1.preorder"<<"\n2inorder"<<"\
n3postorder"<<"\n4exit";
cout<<"enter your choice";
cin>>ch;
if(ch==4)
break;
disp(root,ch);
}
clrscr();
}
void bst::disp(node *t,int n)
{
if(t!=NULL)
switch(n)
{
case 1:
cout<<t->no<<"->";
disp(t->left,n);
disp(t->right,n);
break;
case 2:
disp(t->left,n);
cout<<t->no<<"->";
disp(t->right,n);
break;
case 3:
disp(t->left,n);
disp(t->right,n);
cout<<t->no<<"->";
break;
default:
cout<<"\n invalid choice";
}
}
void main()
{
clrscr();
bst b1;
b1.create();
b1.display();
}
Output
Enter a number at last enter 0:100
MERGE SORT
Aim:
Description:
We use divide and conquer method to sort a file in the n sub files of size and merge
adjacent pair of files we then have approximately n/2 files sizes. Repeat this process until
there is only one file of remaining size the worst case and average are complexity of merge
sort is nlogn.
Algorithm:
Step 1: Start
U2 =l2+siz-1
U2= n-1
Step 7: initialize i=l1 else and j2=l2 and repeat 7.1 and until 7.2 until i<u1 and j<=u2
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
class merge
{
int *a,n,temp,I;
public:
merge();
void read();
void sort();
void print();
};
merge:: merge()
{
cout<<”enter size\t”;
cin>>n;
a=new int[n];
}
void merge::read()
{
cout<<”enter elements into array\n”;
for(int i=0;i<n;i++)
{
cout<<”enter”<<1+i<<”no”;
cin>>a[i];
}
void merge::print()
{
static int y=0;
cout<<”\n\n in pass”<<++y<<”the array is”;
for(int z=0;z<n;z++)
cout<<a[z]<<”\t”;
}
void merge:: sort()
{
int j,k,i,l1,l2,siz,u1,u2;
int aux[50];
siz=1;
while(siz<n)
{
l1=k=0;
while(l1+siz<n)
{
l2=l1+siz;
u1=l2-1;
u2=(l2+siz-1<n)? l2+siz-1:n-1;
for(i=l1,j=l2;i<u1&&j<=u2;k++)
{
if(a[i]<a[j])
aux[k]=a[i++];
else
aux[k]=a[j++];
}
for(;i<=u1;k++)
aux[k]=a[i++];
for(;j<=u2;k++)
aux[k]=a[j++];
l1 =u2+1;
}
for(i=l1;k<n;i++)
aux[k++]=a[i];
for(i=0;i<n;i++)
a[i]=aux[i];
siz*=2;
print();
}
}
void main()
{
clrscr();
merge b;
b.read();
b.sort();
cout<<”\n\n Finally the sorted array is”;
b.print();
getch();
}
OUTPUT
Enter size: 5
Aim:
To implement the quick sort algorithm for the given elements in array to re arrange
the elements is array so they are in ascending order.
Description:
Quick sort is a fast sorting algorithm, which is used not only for educational purposes,
but widely applied in practice. On the average, it has O(n log n) complexity, making quick
sort suitable for sorting big data volumes. The Idea of the algorithm is quite as fast as bubble
sort.
We use the divide and conquer approach to perform quick sort. In this method the ‘n’
elements to be sorted are partitioned into 3 segments they are left segment middle segment
and right segment. The middle segment contains only one element. No element in left has a
key layer then the key of the element in middle and no element in the right has a key that is
smaller than the middle element so that the elements in left and middle can be sorted
independently. No merge is following the sorting of the element. the element in middle called
as pivot or partitioning element. The worst complexity of is n2 and average complexity is n
log n.
Theory:
The divided and conquer strategy is used in quick sort. Below the recursion step is described:
1. Choose a pivot value. We take the value of the middle element as pivot value, but it
can be any value, which is in range of sorted values, even if it does not present in the
array.
2. Partition. Rearrange elements in such a way, that all elements which are lesser than
the pivot go to the left part of the array and all elements greater than the pivot, go to
the right part of the array. Values equal to the pivot can stay in any part of the array.
Notice, that array may be divided in non-equal parts.
3. Sort both parts. Apply quick sort algorithm recursively to the left and the right parts.
Algorithm:
Step 1: Call the function as sort (arr,0,n-1) where arr is array, 0 is beginning, and n-1 is size
of given array.
Step2: exit
5.1 : i=i+1
6.1: j=j-1
There are two indices I and j and at the very beginning of the partition algorithm I
points to the first element in the array and j points to the last one. Then algorithm moves I
forward, until an element with value greater or equal to the pivot found. Index j is moved
backward, until an element with value lesser or equal to the pivot is found. If i<= j then they
are swapped and I steps to the next position (i+1), j steps to the previous one (j-1). Algorithm
stops, when I become greater than j.
After partition, all values before ith element are less or equal than the pivot and all
values after j-th element are greater or equal to the pivot.
1 12 5 26 7 14 3 7 2
Pivot
value=7
Pivot value
12>=7>=2,swap 12 and 2
1 2 5 26 7 14 3 7 12
26>=7>=7,swap 26 and 7
1 2 5 7 7 14 3 26 12
7>=7>=3,swap 7 and 3
OUTPUT:
Enter size 7
AIM:
Procedure:
Heap sort is a sorting algorithm which was originally described by floyd the method
sorting by building a head and then repeatedly extracting the maximum item.
Heap is a complete binary tree with the property that a parent is always greater than or
equal to either of its children (if they exist) thus the root node always contains the largest
element.
The heap sort method takes the input array of elements to be sorted and then builds a
heap based on elements finally produces consist of two phases
Sorting:
Algorithm:
i) T [j/2] =t[j];
j) j=2*j
step 8: t[j/2]=r
step 9: return
heap ():
step 1: set i=n/2
step 2: repeat step 3 until i>=1
step 3: arrange (I,n) and i—
step 4: set i=n-1
step 5: repeat step 6 to 10 until i>= 1
step 6: set a=t[i+1]
step 7: set t[i]=a
step 8: arrange (1,i)
step 9: i=i-1
step 10: return
OUT PUT:
Enter number of elements 6
3 8 9 5 2 4
234589
DIJKSTRA’S ALGORITHM
AIM:
To implement Dijkstra’s algorithm to find the shortest path between any two vertices in a
weighted graph.
Definition:
Let us consider the general problem of finding the length of a shortest path between a
and z. In an undirected connected simple weighted graph. Dijkastra’s algorithm produces by
the finding length of shortest path from a to z is found.
The shortest path is defined as path from a to z such that the sum of weights of arcs on
the paths is minimized. To represent the network. We assume weight of arc from i to j. If
there is no arc from i to j, weight (I,J) is a set to an arbitrarily large value to indicate. The
infinite cost of going directly from I to J.
Algorithm
L(vs)=0
made permanent vs = vr
L(vi )>L(vk)+w(vk,vi )
Step 5 : vk=vi
Step 8 : Exit.
PROGRAM FOR DIJKSTRA’S ALGORTHIM
#include<iostream.h>
#include<conio.h>
#define size 20
#define p 1
#define T 0
#define INFINITY 9999
struct Label
{
int predecessor, length,flag;
};
class dijkstra
{
int k,min,count,vist_path[size],n;
Label state[size];
public:
int short_path(int a[size]
[size],int,int,int,int,path[size],int*);
void input(int,int a[size][size]);
void output(int, int a[size][size]);
};
int dijkstra::short_path(int a[size][size],int
n,int s,int t,int
path[size],int *dist)
{
*dist=0;
for(int i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].length=INFINITY;
state[i].flag=T;
}
//make source vertex permanent
state[s].predecessor=0;
state[s].length =0;
state[s].flag=p;
//state from source vertex
k=s;
do
{
//check all the paths from kth vertex and find
their distance from k vertex
For(i=1;i<=n;i++)
{
if(a[k][i]>0 &&(state[i].flag==T)
if((state[k].length+a[k][i])
<state[i].length)
{
state[i].predecessor=k;
state[i].length=state[k].length+a[k]
[i];
}
}
min=INFINITY;
k=0;
for(i=1;i<=n;i++)
{
if((state[i].flag==T)&&(state[i].length<m
in)
{
Min=state[i].length;
K=i;
}
if(k==0)
Return (0);
State[k].flag=p;
}while(k!=t);
k=t;
count=1;
do
{
vist_path[count]=k;
count++;
k=state[k].predecessor;
}
while(k!=0);
//reverse the vertices since algorithm stores
path in reverse
direction
count--;
for(i=1;i<i<=count;i++)
{
t1=path[i];
t2=path[i+1];
*dist+=a[t1][t2];
}
return(count);
//input function
void dijkstra::input(int n,int a[size][size])
{
cout<<”n\input adjancey matrix\n”;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>a[i][j];
cout<<”\n”;
}
}
//output function
void dijkstra::output(int n,int a[size][size])
{
cout<<”\n Adjacency matrix:\n”;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<” “<<a[i][j];
cout<<”\n”;
}
}
//main Function
void main()
{
clrscr();
dijkstra dij;
int a[size][size],path[size],org,dest,dist,count,n;
cout<<”\n Input the number of vertices in the graph
:”;
cin>>n;
dij.input(n,a);
dij.output(n,a);
cout<<”\n input starting vertex :”;
cin>>org;
cout<<”input destination “;
cin>>dest;
cout<<dij.short_path(a,n,org,path,&dist);
cout<<”\n\n No of paths:”<<count;
if(dist)
{
cout<<”\n shortest path:”<<path[1];
for(int i=2;i<=count;i++)
cout<<”->”<<path[i];
cout<<”\n minimum distance :”<<dist;
}
else
cout<<”\n path does not exit \n”;
getch();
}
Output
0 2 0 5 7
0 0 0 3 0
8 5 0 2 0
0 0 0 0 1
0 0 9 0 0
Adjacency Matrix
0 2 0 6 7
0 0 0 3 0
8 5 0 2 0
0 0 0 0 1
0 0 9 0 0
Input destination : 5
No of paths : 4
Minimum distance : 6
Kruskal’s Algorithm
Aim:
Kruskal algorithm for computing the minimum spanning tree is directly based on generic
MST algorithm .it builds the MST in forest .initially ,each vertices is in its own tree in forest .
then algorithm considered each edge in turn ,ordered by increasing weight .if an edge (U,V)
connects two different tree,then (U,V) is added to the set of edges of MST and two trees
connected by an edge (U,V) are merged into a single tree on the other hand ,if and edge
(U,V) connects two vertices in the same tree ,then edge(U,V) is discarded.
Make _set(V)
Create a new set whose only member is pointed to by V.
Note that for this operation V must already be in a set
FIND set
Return a pointer to the set containing V
UNION(U,V)
Unites the dynamic set that contain U and V into a new set that is union of these two
sets
Algorithm
MST_KRUSKAL(G,W)
Step 6: do if FIND_SET(U)≠FIND_SET(V)
Step 9: Return
noe=0;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(graph[i][j]!=0)
{
noe++;
edges[noe][1]=i;
edges[noe][2]=j;
edges[noe][3]=graph[i][j];
}
}
}
/*************************** sort edge*********/
for(i=1;i<=noe;i++)
{
for(j=i;j<=noe-1;j++)
{
if(edges[j][3]>edges[j+1][3])
{
int t=edges[j][1];
edges[j][1]=edges[j+1][1];
edges[j+1][1]=t;
t=edges[j][2];
edges[j][2]=edges[j+1][2];
edges[j+1][2]=t;
t=edges[j][3];
edges[j][3]=edges[j+1][3];
edges[j+1][3]=t;
}
}
}
/******************** print edge**********/
cout<<endl;
for(i=1;i<=noe;i++)
cout<<edges[i][1]<<edges[i][2]<<edges[i][3]<<endl;
}
void kruskal::intialize()
{
for(int i=1;i<=n;i++)
for(int j= 1;j<=n;j++)
tree[i][j]=0;
}
void kruskal::algorithm()
{
// make sor set each node
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
for(i=1;i<=noe;i++)
{
int p1=find_node(edges[i][1]);
int p2=find_node(edges[i][2]);
cout<<p1<<"\t"<<p2<<endl;
if(p1!=p2)
{
tree[edges[i][1]][edges[i][2]]=edges[i][3];
tree[edges[i][2]][edges[i][1]]=edges[i][3];
// mix the two set
for(int j=1;j<=top[p2];j++)
{
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
}
}
int kruskal::find_node(int n)
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}
void kruskal :: print_min_span_t()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<tree[i][j];
cout<<endl;
}
}
}
void main()
{
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.intialize();
obj.print_min_span_t();
}
Output
Enter the no.of nodes in the graph 3
1 2 3
4 5 6
7 8 9
111
122
133
225
236
339
1 1
1 2
1 3
1 1
1 1
1 1
Print MST
0 2 3
2 0 0
3 0 0
Simple Hashing
Aim:
Analysis:
A hash table or hash map is data structure that uses a hash function to map identifying
values, known as keys (e.g. A person’s name), to their associated values (e.g, their telephone
number). The hash function is used to transform the key into the index (the hash) of an array
element (the slot or bucket) where the corresponding value is to be sought.
A hash function is a function h(k) which transforms a key k into an address. Hashing is
like indexing in that it involves associating a key with a relative record address. Hashing
however, is different from indexing in two important ways:
With hashing, there is no obvious connection between the key and the location
With hashing two different keys may be transformed to the same address
Algorithm for simple hashing:
Step 3: Divide by a prime number (say 19937) and use the remainder as the address.
Step 4: Stop
IMPEMENTATION HASH TABLE
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
class std
{
public:
char name[10];
int num;
void get()
{
cout<<”Enter no and name”;
cin>.num>>name;
}
};
int key(char x[12])
{
int sum=0;
for(int i=0;x[i]!=’\0’;i++)
{
sum=sum+(x[i]*100+x[i++]);
sum=sum%19937;
}
return (sum%100);
}
void main()
{
int n;
clrscr();
std s;
cout<<”Enter no of records”;
cin>>n;
cfstream f(“a.txt”);
for(int i=0;i<n;i++)
{
s.get();
f.write((char *)&s,sizeof(s));
}
f.close();
OUTPUT:
Enter no of records: 3
Enter no and name 14
Rama
Enter no and name15
Krishna
Enter no and name13
Hari