Thursday, 2 May 2013

HashProbing

public class LinearProbHashing {
int[] table;
int size;

LinearProbHashing(int s){
size = s;
table = new int[size];
}

int rehash(int key){
return ((key+1)%size);
}

public void insert(int key){
int hash = key%size;
int RH = 0;
if(table[hash] == 0){
table[hash] = key;
}else{
RH = rehash(key);
while(RH != 0){
RH = rehash(RH++);
table[RH] = key;
}
}
}

public void printhashtable(){
for(int i=0; i<size; i++){
System.out.print(" "+table[i]);
}
}
}

=============================================

public class HashDemo {
public static void main(String[] args) {
// HashTable table1 = new HashTable(10);
LinearProbHashing table1 = new LinearProbHashing(10);
table1.insert(345);
table1.insert(245);
table1.insert(344);
table1.insert(312);
table1.insert(335); 
table1.printhashtable();
}
}

Hashing


class Node{
int data;
Node next;

Node(int d){
data=d;

}

}
public class HTable {
Node [] Table;
int size;
HTable(int s){
size=s;
Table =new Node[size];

}
public void insert(int key){
Node newNode= new Node(key);
int hash =key%size;

if(Table[hash]==null)
Table[hash] = newNode;
else{
Node HashRef=Table[hash];
newNode.next =HashRef;
Table[hash] =newNode;


}
}
public void printhashtable(){
Node href = null;
for(int i=0; i<size; i++){
href = Table[i];
System.out.print("  " + href.data);
href = href.next;

}
System.out.println(" ");

}

}

==================================================



public class HDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
         HTable table1= new HTable(15);
         table1.insert(345);
         table1.insert(445);
         table1.insert(245);
         table1.insert(045);
         table1.insert(145);
         table1.printhashtable();
       
}

}



Thursday, 11 April 2013

BubbleSort


public class bsorting {
public static void main(String a[]){
 int i;
 int array[] = {10,20,9,12,13,11,15,30,25};
 System.out.print("Before Sorting:");
 for(i = 0; i < array.length; i++)
 System.out.print( array[i]+"  ");
 System.out.println();
 bsort(array, array.length);
 System.out.print("After Sorting:");
 for(i = 0; i <array.length; i++)
 System.out.print(array[i]+"  ");
 System.out.println();

 }

 public static void bsort( int a[], int n ){
 int i, j,temp=0;
 for(i = 0; i < n; i++){
 for(j = 1; j < (n-i); j++){
 if(a[j-1] > a[j])
 {
 temp = a[j-1];
 a[j-1]=a[j];
 a[j]=temp;
 }
 }
 }
 }
}


Tuesday, 9 April 2013

Binary Tree


public void delete (int d){

TNode Temp = Root;
TNode Parent = Root;

while (Temp!=null) {
if (d<Temp.data && Temp.data!=d){
Today's DSA Code:

Parent = Temp;
Temp=Temp.L;
}
else
{
Temp= Temp.R;
}
}

if (Temp==null){
System.out.println ("Not Found");
}

else {
if (Temp.R == null && Temp.L == null){ //no child

if (Temp.data < Parent.data){
Parent.L = null;
}
else
{
Parent.R = null;

}
}
}

else
if ((Temp.R == null && Temp.L != null) || (Temp.R!=null && Temp.L == null)){
if(Temp.R == null) {
if(d<Parent.data)
Parent.L=Temp.L;

}

else {
Parent.R = Temp.L;

}
if (Temp.L == null){

}

}

Tuesday, 26 March 2013

PriortyQueue



public class Queue {

int [] Q;
int F;
int R;

Queue (int Qsize){
Q= new int[Qsize];
F=0;
R=-1;

}
public void insert (int v){
if (R==-1){
R++;
Q[R]=v;

}
else{
for (int i=F; i<=R&& Q[i]<=v; i++){

}
if(i==R){
R++;
Q[R]=v;

}
else if (i==F){
for(j=R;j>0;j--){
Q[j+1]= Q[j];

}
R++;
Q[i]=v;

}
}
}
}

Thursday, 21 March 2013

CircularQueue


public class CircularQueue {

int[] Q;
int R,F;

CircularQueue(int size)
{
Q=new int[size];
R=size-1;
F=size-1;
}

public boolean isFull(){
if (F==R)
return true;
else
return false;
}
 public boolean isEmpty()
 {
if (R==F)
return true;
else
return false;
 }


public void insertion(int item)
{
if(R==Q.length-1)
R=0;
else
{
R++;
if (isFull())
{
System.out.println("Queue overflow");
R--;
return;
}
}

Q[R]=item;

}

public int Delete()
{
if(isEmpty()){
System.out.println("Queue underflow");
return -1;
}
else{
if(F==Q.length-1)
F=0;
else
F++;
}
    return Q[F];

}

public void display(){
for(int i=0; i<Q.length; i++)
System.out.println(Q[i]);
}



}

////////////////////////////////////////////////////////////////////////



public class Queuedemo {

/**
* @param args
*/
public static void main(String[] args) {
CircularQueue q= new CircularQueue(10);
q.insertion(10);
q.insertion(11);
q.insertion(12);
q.insertion(13);
q.insertion(14);
q.insertion(15);
q.insertion(16);
q.Delete();

q.display();




}

}

///////////////////////////////////////////////////

Tuesday, 19 March 2013

Application Of Stack


LineStack
public class LineStack<T> {
StackNode <T> Top;

public Boolean isEmpty(){
if (Top==null)
return true;
else
return false;


}

public void push (T data) {
StackNode<T> newNode = new StackNode<T> (data);
if (isEmpty()){
Top=newNode;
else
newNode.next=Top;
Top=newNode;

}


}
}

////////////////////////////////


StackNode:

public class StackNode<T>  {

T data;
StackNode<T>next;

StackNode(T d){
data=d;

}

}

/////////////////////////////////////////


public class StackAppDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LineStack<Character> MyStk= new LineStack<Character>()

}

}


//////////////////////////////////////////////////


public class ArithematicExp {

public void infixToPostfix(String Exp){
String Postfix;
LinkStack<T> ExpStk=new LinkStack<Character>;
int i=0;
while (i<Exp.length()){
char ch=Exp.charAt(i);
switch(ch){
case ch='('||'{'||'['
{
}
}
}
i++;
}
}