Saturday, March 13, 2010

Reverse traversal of Linkedlist

package com.newproject.model;

class NodeObj
{
NodeObj(int argVal, String argStr)
{
iVal = argVal;
nodeName = argStr;
}
int iVal;
String nodeName;
NodeObj linkedRef;
}

public class ReverseList
{
private static NodeObj rootNode = null;
private static NodeObj tempNodeObj = null;

public static void buildLinkedList(NodeObj argNode)
{
boolean flag = false;
if (rootNode == null)
{
rootNode = argNode;
tempNodeObj = rootNode;
flag = true ;
}

while (tempNodeObj.linkedRef != null )
{
tempNodeObj = tempNodeObj.linkedRef;
}
if (!flag)
tempNodeObj.linkedRef = argNode;
}

public static void reverseLink(NodeObj argNode)
{
if (argNode.linkedRef != null)
{
reverseLink(argNode.linkedRef);
}
System.out.println(argNode.iVal);
}

public static void main(String[] args)
{
NodeObj no1 = new NodeObj(1, "Emp No 1");
buildLinkedList(no1);
NodeObj no2 = new NodeObj(2, "Emp No 2");
buildLinkedList(no2);
NodeObj no3 = new NodeObj(3, "Emp No 3");
buildLinkedList(no3);
NodeObj no4 = new NodeObj(4, "Emp No 4");
buildLinkedList(no4);
reverseLink(rootNode);

}

}

Merge Sort

package com.newproject.model;

public class MergeSort
{

private static int[] arryOne = { 3, 8, 10, 10, 12, 90, 111, 222, 333, 444, 555, 777, 888, 999, 1111, 2222, 3333 };
private static int[] arryTwo = { 1, 2 };
private static int arryOneCounter = 0;
private static int arryTwoCounter = 0;
private static boolean endOfSmallArrayFlag = false;
private static int[] resultArry = new int[arryOne.length + arryTwo.length];

public static void main(String[] args)
{
for (int iCnt = 0; iCnt < resultArry.length; iCnt++)
{
if (arryOne[arryOneCounter] < arryTwo[arryTwoCounter])
{
if (!endOfSmallArrayFlag)
resultArry[iCnt] = arryOne[arryOneCounter];
if (arryOneCounter < arryOne.length - 1)
arryOneCounter++;
else
{
if (arryTwoCounter <= arryTwo.length - 1)
{
if (!endOfSmallArrayFlag)
iCnt++;
resultArry[iCnt] = arryTwo[arryTwoCounter];
arryTwoCounter++;
endOfSmallArrayFlag = true;
}
}
}
else
{
if (!endOfSmallArrayFlag)
resultArry[iCnt] = arryTwo[arryTwoCounter];
if (arryTwoCounter < arryTwo.length - 1)
arryTwoCounter++;
else
{
if (arryOneCounter <= arryOne.length - 1)
{
if (!endOfSmallArrayFlag)
iCnt++;
resultArry[iCnt] = arryOne[arryOneCounter];
arryOneCounter++;
endOfSmallArrayFlag = true;
}
}
}
}
System.out.println("Total expected values are " + (arryOne.length + arryTwo.length));
for (int iCnt = 0; iCnt < resultArry.length; iCnt++)
{
System.out.println("resultArray at poosition " + iCnt + " is " + resultArry[iCnt]);
}
}
}

Not like HashMap ..yes..but basic utility of Hashing

package com.newproject.model;

class Hash
{
T key;
V value;
Hash hashLink;

Hash(T argOne, V argTow)
{
key = argOne;
value = argTow;
}

public String toString()
{
return " - key : " + key + " and value : " + value;
}
}

interface IHashStore
{
public void put(T argStrKey, V argStrValue);
public Hash get(T argStrKey);
public void remove(T argStrKey);
}

public class HashStorage
implements IHashStore

{
Hash[] hashArry = null;
Hash[] head = null;
static int[] iCnt = new int[10];

HashStorage()
{
hashArry = new Hash[10];
head = new Hash[10];
}

public String toString()
{
StringBuffer sb = new StringBuffer();
for (int iCnt = 0; iCnt < 10; iCnt++)
{
if (head[iCnt] == null)
{
continue;
}
if (head[iCnt] != null)
{
sb.append("\n");
sb.append("head[iCnt].key is " + head[iCnt].key);
sb.append("\n");
}
while (head[iCnt].hashLink != null)
{
head[iCnt] = head[iCnt].hashLink;
sb.append("Travelling through links inside a array .... key is - " + head[iCnt].key);
sb.append("\n");

}
}
return sb.toString();
}

public void elementInsert(T argKey, V argValue)
{
int lenth = String.valueOf(argKey.hashCode()).length();
if (argKey != null)
{
if (hashArry[lenth] == null)
{
hashArry[lenth] = new Hash(argKey, argValue);
head[lenth] = hashArry[lenth];
System.out.println("Inserted Data inside Array at position [" + lenth + "] and It's value is " + head[lenth]);
}
else
{
System.out.println("Already Element is available at position [" + lenth + "]");
while (hashArry[lenth].hashLink != null)
{
iCnt[lenth]++;
System.out.print("After Node data key :" + hashArry[lenth].hashLink.key + " ");
hashArry[lenth] = hashArry[lenth].hashLink;
}
hashArry[lenth].hashLink = new Hash(argKey, argValue);
System.out.println(", next data inserted is - " + hashArry[lenth].hashLink.key);
}
}
}

public Hash elementFetch(T argKey)
{
int lenth = String.valueOf(argKey.hashCode()).length();
if (argKey != null)
{
if (head[lenth].hashLink == null)
{
return hashArry[lenth];
}
else
{
System.out.println("Already Element is available at position [" + lenth + "]");
while (head[lenth].hashLink != null)
{

if (argKey instanceof Integer || argKey instanceof Long || argKey instanceof Double || argKey instanceof Float )
{
if (head[lenth].key == argKey)
{
System.out.println("**** return value is - " + head[lenth].value);
return head[lenth];
}
}
else if (argKey instanceof String)
{
if (((String) head[lenth].key).equalsIgnoreCase((String) argKey))
{
System.out.println("**** return value is - " + head[lenth].value);
return head[lenth];
}
}
head[lenth] = head[lenth].hashLink;
}
}
}
return null;
}

public void put(T argKey, V argValue)
{
elementInsert(argKey, argValue);
}

public Hash get(T argKey)
{
return elementFetch(argKey);
}

public static void main(String[] args)
{
IHashStore hs = new HashStorage();
hs.put(112l, 11);
hs.put(221l, 11);
hs.put(12l, 14);
hs.put(44l, 15);
hs.put(24l, 12);
hs.put(34l, 22);
hs.put(45l, 44);
hs.put(234l, 66);
hs.put(345l, 12);
Object obj = hs.get(112l);
System.out.println("Obj " + obj);
System.out.println(hs);
}
@Override
public void remove(T argStrKey)
{
// To Do...
}

}

Generic Singleton blog Java

package com.newproject.model;

import java.util.HashMap;
import java.util.Map;

class Test0
{
int i = 100;
String name = "TestMe0";
}

class Test1
{
int i = 101;
String name = "TestMe1";
}

class Test2
{
int i = 102;
String name = "TestMe2";
}

class Test3
{
int i = 103;
String name = "TestMe3";
}

public class GenericSingleTon
{
T instance;
static Map map = new HashMap();
public T getInstance(Class clazz)
{
if (instance != null)
{
instance = clazz.cast(map.get(clazz));
}
else
{
try
{
instance = clazz.newInstance();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
map.put(clazz, instance);
System.out.println(map);
}
return instance;
}

public static void main(String[] args)
{
GenericSingleTon gs0 = new GenericSingleTon();
gs0.getInstance(Test0.class);
GenericSingleTon gs1 = new GenericSingleTon();
gs1.getInstance(Test1.class);
GenericSingleTon gs2 = new GenericSingleTon();
gs2.getInstance(Test2.class);
GenericSingleTon gs3 = new GenericSingleTon();
gs3.getInstance(Test3.class);
gs0.getInstance(Test0.class);
gs1.getInstance(Test1.class);
System.out.println("**** map" + map);
}
}

Bubble sort (actually not good in performance)

package com.newproject.model;

public class BubbleSort
{
static int[] values = { 12, 3, 2, 1, 34, 54, 22, 11 , 1000, 24};

public static void main(String[] args)
{
for (int iteration = 1; iteration < values.length; iteration++)
{
for (int iCnt = 0; iCnt < values.length - iteration; iCnt++)
{
if (values[iCnt] > values[iCnt + 1])
{
int temp = values[iCnt + 1];
values[iCnt + 1] = values[iCnt];
values[iCnt] = temp;
}

}
}
for (int i = 0; i < values.length; i++)
{
System.out.println(values[i]);

}
}
}

Experiment with Binary tree.

package com.newproject.model;

import java.util.ArrayList;
import java.util.List;

class Node
{
int iVal;
String nodeName;
Node leftRef;
Node rightRef;

Node(int argVal, String argStr)
{
this.iVal = argVal;
this.nodeName = argStr;
}
}

public class BNTree
{
static Node rootNode = new Node(100, "Root Node");
static List list = new ArrayList();
static Node tempNode = rootNode;
static public void buildTree(Node argNode)
{
Node traverseNode = rootNode;
boolean initFlag = false;
boolean isAssign = false;
if (rootNode.leftRef == null || rootNode.leftRef == null)
{
if (argNode.iVal < rootNode.iVal)
{
rootNode.leftRef = argNode;
}
else
{
rootNode.rightRef = argNode;
}
isAssign = true;

}

do
{
if (argNode.iVal < traverseNode.iVal && traverseNode.leftRef != null)
{
traverseNode = traverseNode.leftRef;
}
else if (argNode.iVal < traverseNode.iVal && traverseNode.leftRef == null)
{
traverseNode.leftRef = argNode;
isAssign = true;

}
if (argNode.iVal > traverseNode.iVal && traverseNode.rightRef != null)
{
traverseNode = traverseNode.rightRef;
}
else if (argNode.iVal > traverseNode.iVal && traverseNode.rightRef == null)
{
traverseNode.rightRef = argNode;
isAssign = true;
}
} while (!initFlag && !isAssign);
}

static void preOrder(Node argRootNode)
{
if (argRootNode != null)
{
if(argRootNode.leftRef != null)
{
preOrder(argRootNode.leftRef);
}
System.out.println(argRootNode.iVal);
if(argRootNode.rightRef!=null)
{
preOrder(argRootNode.rightRef);

}
}
}

public static void main(String[] args)
{
Node nodeOne = new Node(10, "Ten");
Node nodeTwo = new Node(200, "TwoH");
Node nodeThree = new Node(30, "Thirty");
Node nodeFour = new Node(2, "Two");
Node nodeFive = new Node(20, "Twenty");
Node nodeSix = new Node(150, "150");
Node nodeThreeH = new Node(300, "150");
Node nodeS = new Node(1, "150");
buildTree(nodeOne);
buildTree(nodeTwo);
buildTree(nodeThree);
buildTree(nodeFour);
buildTree(nodeFive);
buildTree(nodeSix);
buildTree(nodeThreeH);
buildTree(nodeS);
preOrder(rootNode);
}
}