Friday, April 27, 2012

DNS Lookup in Java

A simple DNS lookup in Java
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DNSLookup {
    
    public static void main(String... args) {
        InetAddress inet = null;
        try {
            final String host = "eradicus.blogspot.com";
            inet = InetAddress.getByName(host);
            System.out.println("DNS Lookup: " + host);
            System.out.println("IP Adress: " + inet.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
By using the InetAddress API you will be able to obtain the IP address of the target.

Thursday, April 26, 2012

Reversing a linked list in C++

Linked list is one of the most popular data structures in Computer Science. Read more about linked lists [here]. In this entry we will write a function to reverse a linked list in C++ efficiently.
Link* reverse_list(Link* p)
{
    if (p == NULL)
    {
        return NULL;
    }

    Link* h = p;
    p = p->next;
    h->next = NULL;

    while(p != NULL)
    {
        Link* t = p->next;
        p->next = h;
        h = p;
        p = t;
    }

    return h;
}
Enjoy.