Program:
import java.io.*;
class Node
{
int data;
Node next;
public Node(int x)
{
data=x;
next=null;
}
public void display()
{
System.out.print(" "+data);
}
}
class list
{
Node first;
public list()
{
first=null;
}
public void deletekey(int key)
{
Node current=first;
Node prev=null;
while(current.data!=key)
{
if(current.next==null)
{
System.out.println("\nNot found...");
return;
}
else
{
prev=current;
current=current.next;
}
}
if(current==first)
first=first.next;
else
prev.next=current.next;
}
public void insertlast(int x)
{
Node newnode=new Node(x);
if(first==null)
{
first=newnode;
}
else
{
Node current=first;
while(current.next!=null)
current=current.next;
current.next=newnode;
}
}
public boolean search(int key)
{
Node current=first;
Node prev=null;
while(current.data!=key)
{
if(current.next==null)
return false;
else
current=current.next;
}
return true;
}
public void display()
{
System.out.println("\n\n");
Node current=first;
while(current!=null)
{
current.display();
current=current.next;
}
}
}
class hashtable
{
int tablesize;
list[] H;
public hashtable(int size)
{
tablesize=size;
H= new list[tablesize];
for(int i=0;i<tablesize;i++)
H[i]=new list();
}
public int hashfunc(int key)
{
return key%tablesize;
}
public void insert(int key)
{
int hashval=hashfunc(key);
H[hashval].insertlast(key);
}
public void delete(int key)
{
int hashval=hashfunc(key);
H[hashval].deletekey(key);
}
public void display()
{
for(int i=0;i<tablesize;i++)
{
H[i].display();
System.out.print("\n");
}
}
public boolean search(int key)
{
int hashval=hashfunc(key);
if(H[hashval].search(key))
return true;
else
return false;
}
}
class chain
{
public static void main(String arg[])throws IOException
{
DataInputStream x= new DataInputStream(System.in);
System.out.println("Enter tablesize:");
int size=Integer.parseInt(x.readLine());
hashtable t=new hashtable(size);
System.out.println("Enter number of terms to insert:");
int num= Integer.parseInt(x.readLine());
System.out.println("Enter elements:");
for(int i=0;i<num;i++)
t.insert(Integer.parseInt(x.readLine()));
t.display();
System.out.println("\nEnter number to delete:");
t.delete(Integer.parseInt(x.readLine()));
t.display();
System.out.println("\nEnter number to search:");
if(t.search(Integer.parseInt(x.readLine())))
System.out.println("Present..");
else
System.out.println("Not present...");
}
}
Output:
nn@linuxmint ~ $ javac chain.java
Note: chain.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java chain
Enter tablesize:
10
Enter number of terms to insert:
12
Enter elements:
1
5
8
7
4
2
9
15
33
47
66
14
1
2
33
4 14
5 15
66
7 47
8
9
Enter number to delete:
15
1
2
33
4 14
5
66
7 47
8
9
Enter number to search:
5
Present..
nn@linuxmint ~ $
import java.io.*;
class Node
{
int data;
Node next;
public Node(int x)
{
data=x;
next=null;
}
public void display()
{
System.out.print(" "+data);
}
}
class list
{
Node first;
public list()
{
first=null;
}
public void deletekey(int key)
{
Node current=first;
Node prev=null;
while(current.data!=key)
{
if(current.next==null)
{
System.out.println("\nNot found...");
return;
}
else
{
prev=current;
current=current.next;
}
}
if(current==first)
first=first.next;
else
prev.next=current.next;
}
public void insertlast(int x)
{
Node newnode=new Node(x);
if(first==null)
{
first=newnode;
}
else
{
Node current=first;
while(current.next!=null)
current=current.next;
current.next=newnode;
}
}
public boolean search(int key)
{
Node current=first;
Node prev=null;
while(current.data!=key)
{
if(current.next==null)
return false;
else
current=current.next;
}
return true;
}
public void display()
{
System.out.println("\n\n");
Node current=first;
while(current!=null)
{
current.display();
current=current.next;
}
}
}
class hashtable
{
int tablesize;
list[] H;
public hashtable(int size)
{
tablesize=size;
H= new list[tablesize];
for(int i=0;i<tablesize;i++)
H[i]=new list();
}
public int hashfunc(int key)
{
return key%tablesize;
}
public void insert(int key)
{
int hashval=hashfunc(key);
H[hashval].insertlast(key);
}
public void delete(int key)
{
int hashval=hashfunc(key);
H[hashval].deletekey(key);
}
public void display()
{
for(int i=0;i<tablesize;i++)
{
H[i].display();
System.out.print("\n");
}
}
public boolean search(int key)
{
int hashval=hashfunc(key);
if(H[hashval].search(key))
return true;
else
return false;
}
}
class chain
{
public static void main(String arg[])throws IOException
{
DataInputStream x= new DataInputStream(System.in);
System.out.println("Enter tablesize:");
int size=Integer.parseInt(x.readLine());
hashtable t=new hashtable(size);
System.out.println("Enter number of terms to insert:");
int num= Integer.parseInt(x.readLine());
System.out.println("Enter elements:");
for(int i=0;i<num;i++)
t.insert(Integer.parseInt(x.readLine()));
t.display();
System.out.println("\nEnter number to delete:");
t.delete(Integer.parseInt(x.readLine()));
t.display();
System.out.println("\nEnter number to search:");
if(t.search(Integer.parseInt(x.readLine())))
System.out.println("Present..");
else
System.out.println("Not present...");
}
}
Output:
nn@linuxmint ~ $ javac chain.java
Note: chain.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java chain
Enter tablesize:
10
Enter number of terms to insert:
12
Enter elements:
1
5
8
7
4
2
9
15
33
47
66
14
1
2
33
4 14
5 15
66
7 47
8
9
Enter number to delete:
15
1
2
33
4 14
5
66
7 47
8
9
Enter number to search:
5
Present..
nn@linuxmint ~ $
0 comments:
Post a Comment