acJavaFreak
BAN USERA Small Implementation of what " PseudoLife " meant to suggested :
O/P :
1->2->3->4->5->6->7->8->9
5 is the middle node
Code :
/*
you have a circular linked list,
how do you find the middle. You can only go through it once.
-Another question from "Cracking the Code".
*/
class Node {
Node its_childNode;
Node grandChildNode;
Integer val; // this is needed to show the answer of the example.
private static Node ROOT_NODE = null;
public static Node initRootAndChildNode(Integer initRootVal, Integer rootChildNodeVal) {
if (Node.ROOT_NODE == null) {
Node.ROOT_NODE = new Node(initRootVal);
Node.ROOT_NODE.its_childNode = new Node(rootChildNodeVal); // no grandparent of this child i.e. Node.ROOT_NODE.grandChildNode is null;
return Node.ROOT_NODE;
}
throw new RuntimeException("No. You cannot Initilise it Twice");
}
public static Node getRootNode() {
return Node.ROOT_NODE;
}
private Node(int _val) {
val = _val;
}
Node init_its_ChildNode(Integer next_val, Node grandParentNode) {
its_childNode = new Node(next_val);
grandParentNode.grandChildNode = its_childNode;
return this;
}
}
public class FindMidPointOfLinkedList {
public static void main(String[] args) {
initiliseNode();
}
private static void initiliseNode() {
Node myFirstNode = Node.initRootAndChildNode(1,2);
Node grandParentNode = null;
grandParentNode = myFirstNode.its_childNode.init_its_ChildNode(3, myFirstNode); // no grandparent for the first child.
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(4, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(5, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(6, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(7, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(8, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(9, grandParentNode);
grandParentNode = grandParentNode.its_childNode.init_its_ChildNode(10, grandParentNode);
Node answer = Node.getRootNode();
Node loopingNode = Node.getRootNode();
// racing to the last Node
while (loopingNode.grandChildNode != null) {
answer = answer.its_childNode;
loopingNode = loopingNode.grandChildNode;
}
// just to display all the created node in UI
Node nodeIterator = Node.getRootNode();
while (nodeIterator.its_childNode != null) {
if (nodeIterator.grandChildNode != null) {
System.out.print(nodeIterator.val + "->");
} else {
System.out.print(nodeIterator.val);
}
nodeIterator = nodeIterator.its_childNode;
}
while (loopingNode.grandChildNode != null) {
answer = answer.its_childNode;
loopingNode = loopingNode.grandChildNode;
}
System.out.println("\n"+answer.val + " is the middle node");
}
}
Quoting from Kate Serrias SCJP /OCJP book :
This issue can lead to a scenario known as the "Deadly Diamond of Death," because of the shape of the class diagram that can be created in a multiple inheritance design. The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond."
Hence it was avoided in Java
Depending on the parameter which are required for comparing the Objects(CAT and DOG in this case). Equals can be manipulated to see what we wish it to see:) . .
Code: -
import java.util.Arrays;
/*
Q) Sort the array(have to implement the equals method in Cat and Dog).
Cat c = new Cat("Snow Ball");
Dog d = new Dog("Max");
array = {c, d, ... };
*/
class Dog implements Comparable<Object> {
String name;
// other dog attributes
// Only Constructor
Dog(String name) {
this.name = name;
}
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
// can compare other common attributes if required
return name.equals(otherObj.toString());
}
return false;
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
return name.compareTo(otherObj.toString());
}
throw new RuntimeException("Only works for Cats and Dogs Class.");
}
}
class Cat implements Comparable<Object> {
String name;
// other cat attributes
// Only Constructor
Cat(String name) {
this.name = name;
}
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
// can compare other common attributes if required
return name.equals(otherObj.toString());
}
return false;
}
@Override
public int compareTo(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
return name.compareTo(otherObj.toString());
}
throw new RuntimeException("Only works for Cats and Dogs Class.");
}
@Override
public String toString() {
return name;
}
}
public class Main {
public static void main(String arg[]) {
Object[] pets = { new Dog("Max"), new Cat("Snow Ball"),
new Dog("Brownie"), new Cat("Not so Brownie") };
System.out.println("\nBefore Sorting:\n");
displayHouse(pets);
Arrays.sort(pets);
System.out.println("\nAfter Sorting:\n");
displayHouse(pets);
}
private static void displayHouse(Object[] pets) {
for (Object pet : pets) {
System.out.println(pet);
}
}
}
We Can DO Something Like This:
- acJavaFreak September 04, 2013Physical Data Logical Data
2 GB --> Lines of File [1-10]
2 GB --> Lines of File [11-20]
2 GB --> Lines of File [21-30]
2 GB --> Lines of File [31-40]
2 GB --> Lines of File [41-50]
2 GB --> Lines of File [51-60]
2 GB --> Lines of File [61-70]
2 GB --> Lines of File [71-80]
2 GB --> Lines of File [81-90]
2 GB --> Lines of File [91-100]
And We can Have a HASH MAP , storing the Key Range and The Physical Contents it Has IN a LOGICAL MANNER.
MAP Eg:-
{1$10 , Lines 1-10},
{11$20 , Lines 11-20} . etc.....
In This Manner , no need to load the entire file at one go , Instead we can Have OUR Map Decide WHich Portion of FIle to Load based on the Line to be Read.
Moreover we can index words that are non redundant, to help us in better searching phrases or form meanings from the sentences.
Eg: "Chemicals" can be index to be containing in the the following keys of the Hash Map
"31$40" & "82$90"
weapons is another word that is found in key "82$90". Such combination of non redundant and import words can be used to search important pharases from the file without loading the entire File.
- ARgho Chatterjee.